Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения
KANSoftWare

Получить Primary Domain Controller (PDC)

Delphi , Интернет и Сети , Сеть

Получить Primary Domain Controller (PDC)

Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch

{ 
  The NetGetDCName function returns the name of the primary domain controller (PDC). 
  It does not return the name of the backup domain controller (BDC) for the specified domain. 
  Also, you cannot remote this function to a non-PDC server. 
  Windows 2000/XP: Applications that support DNS-style names should call the  function. 
  Domain controllers in this type of environment have a multi-master 
  directory replication relationship. 
  Therefore, it may be advantageous for your application to use a DC that is not the PDC. 
  You can call the DsGetDcName function to locate any DC in the domain; 
  NetGetDCName returns only the name of the PDC. 
}

 type
   EAccessDenied = Exception;
   EInvalidOwner = Exception;
   EInsufficientBuffer = Exception;
   ELibraryNotFound = Exception;

   NET_API_STATUS = Integer;

 const
   NERR_Success = 0;

 var
   NTNetGetDCName: function (Server, Domain: pWideChar; var DC: pWideChar): NET_API_STATUS; stdcall;
   NTNetGetDCNameA: function (Server, Domain: PChar; var DC: PChar): NET_API_STATUS; stdcall;
   NTNetApiBufferFree: function (lpBuffer: Pointer): NET_API_STATUS; stdcall;

 procedure NetCheck(ErrCode: NET_API_STATUS);
 begin
   if ErrCode <> NERR_Success then
   begin
     case ErrCode of
       ERROR_ACCESS_DENIED:
         raise EAccessDenied.Create('Access is Denied');
       ERROR_INVALID_OWNER:
         raise EInvalidOwner.Create('Cannot assign the owner of this object.');
       ERROR_INSUFFICIENT_BUFFER:
         raise EInsufficientBuffer.Create('Buffer passed was too small');
       else
         raise Exception.Create('Error Code: ' + IntToStr(ErrCode) + #13 +
           SysErrorMessage(ErrCode));
     end;
   end;
 end;

 function GetPDC(szSystem: string): string;
   { if szSystem = '' return the PDC else return DC for that domain }
 const
   NTlib = 'NETAPI32.DLL';
   Win95lib = 'RADMIN32.DLL';
 var
   pAnsiDomain: PChar;
   pDomain: PWideChar;
   System: array[1..80] of WideChar;
   ErrMode: Word;
   LibHandle: THandle;
 begin
   Result := '';
   LibHandle := 0;
   try
     if Win32Platform = VER_PLATFORM_WIN32_NT then
     begin
       ErrMode := SetErrorMode(SEM_NOOPENFILEERRORBOX);
       LibHandle := LoadLibrary(NTlib);
       SetErrorMode(ErrMode);
       if LibHandle = 0 then
         raise ELibraryNotFound.Create('Unable to map library: ' +
           NTlib); @NTNetGetDCName := GetProcAddress(Libhandle, 'NetGetDCName');
         @NTNetApiBufferFree       := GetProcAddress(Libhandle,
         'NetApiBufferFree');
       try
         if szSystem <> '' then
           NetCheck(NTNetGetDCName(nil, StringToWideChar(szSystem, @System, 80), pDomain))
         else
           NetCheck(NTNetGetDCName(nil, nil, pDomain));
         Result := WideCharToString(pDomain);
       finally
         NetCheck(NTNetApiBufferFree(pDomain));
       end;
     end
     else
     begin
       ErrMode := SetErrorMode(SEM_NOOPENFILEERRORBOX);
       LibHandle := LoadLibrary(Win95lib);
       SetErrorMode(ErrMode);
       if LibHandle = 0 then
         raise ELibraryNotFound.Create('Unable to map library: ' +
           Win95lib); @NTNetGetDCNameA := GetProcAddress(Libhandle, 'NetGetDCNameA');
         @NTNetApiBufferFree := GetProcAddress(LibHandle, 'NetApiBufferFree');
       try
         if szSystem <> '' then
           NetCheck(NTNetGetDCNameA(nil, PChar(szSystem), pAnsiDomain))
         else
           NetCheck(NTNetGetDCNameA(nil, nil, pAnsiDomain));
         Result := StrPas(pAnsiDomain);
       finally
         NetCheck(NTNetApiBufferFree(pAnsiDomain));
       end;
     end;
   finally
     if LibHandle <> 0 then
     begin
       FreeLibrary(Libhandle); // free handle if it has been allocated 
    end;
   end;
 end;

 // Example call, Beispielaufruf: 

procedure TForm1.Button1Click(Sender: TObject);
 begin
   try
     Screen.Cursor  := crHourGlass;
     label1.Caption := GetPDC('');
   finally
     Screen.Cursor := crDefault;
   end;
 end;

Статья Получить Primary Domain Controller (PDC) раздела Интернет и Сети Сеть может быть полезна для разработчиков на Delphi и FreePascal.


Комментарии и вопросы


Ваше мнение или вопрос к статье в виде простого текста (Tag <a href=... Disabled). Все комментарии модерируются, модератор оставляет за собой право удалить непонравившейся ему комментарий.

заголовок

e-mail

Ваше имя

Сообщение

Введите код




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: Сеть ::


реклама



©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru Rambler's Top100

Время компиляции файла: 2024-04-24 22:55:34
2024-04-25 01:50:30/0.0065550804138184/2