2010年7月20日

参加しているドメインまたはワークグループの種類と名前を取得する

前回のNetWkstaGetInfoを使用する方法ではNTドメイン(AD)に参加しているのかワークグループに参加しているのかを知ることができませんでした。そこでNetGetJoinInformation(ja)で参加しているNTドメインまたはワークグループの名前と参加ステータスを取得してみます。ここでもNetWkstaGetInfoで取得したドメインまたはワークグループの名前の領域はNetApiBufferFree(ja)で解放する必要があることに注意が必要です。
まずこれらのネットワーク管理APIと列挙を定義します。
uses
  Windows;

type
  NET_API_STATUS = DWORD;
  {$EXTERNALSYM NET_API_STATUS}

  _NETSETUP_JOIN_STATUS = (NetSetupUnknownStatus = 0,  // The status is unknown.
                           NetSetupUnjoined,           // The computer is not joined.
                           NetSetupWorkgroupName,      // The computer is joined to a workgroup.
                           NetSetupDomainName);        // The computer is joined to a domain.
  {$EXTERNALSYM _NETSETUP_JOIN_STATUS}
  NETSETUP_JOIN_STATUS  = _NETSETUP_JOIN_STATUS;
  {$EXTERNALSYM NETSETUP_JOIN_STATUS}
  PNETSETUP_JOIN_STATUS = ^NETSETUP_JOIN_STATUS;
  {$EXTERNALSYM PNETSETUP_JOIN_STATUS}
  TNetsetupJoinStatus   = NETSETUP_JOIN_STATUS;

const
  NERR_Success = 0;
  {$EXTERNALSYM NERR_Success}

function NetGetJoinInformation(lpServer: PWideChar;
                               var lpNameBuffer: PWideChar;
                               BufferType: PNETSETUP_JOIN_STATUS): NET_API_STATUS; stdcall;
  external 'netapi32.dll' Name 'NetGetJoinInformation';
  {$EXTERNALSYM NetGetJoinInformation}

function NetApiBufferFree(Buffer: Pointer): NET_API_STATUS; stdcall;
  external 'netapi32.dll' Name 'NetApiBufferFree';
  {$EXTERNALSYM NetApiBufferFree}

あとはこれを呼び出すだけです。
function GetNetworkJoinInformation(var NetworkName: String;
                                   var Status: TNetsetupJoinStatus): Boolean;
var
  Buffer: PWideChar;
begin

  if NetGetJoinInformation(nil,Buffer,@Status) <> NERR_Success then
  begin
    Result := False;
    Exit;
  end;

  NetworkName := Buffer;

  NetApiBufferFree(Buffer);

  Result := True;

end;

0 件のコメント: