2009. január 2., péntek

How to get workgroup/domain name under NT4/2k/XP


Problem/Question/Abstract:

How do I get the current workgroup?

Answer:

function GetWorkgroupName: string;
type
  P_WKSTA_INFO_100 = ^T_WKSTA_INFO_100;
  _WKSTA_INFO_100 = record
    wki100_platform_id: LongInt;
    wki100_computername: PWideChar;
    wki100_langroup: PWideChar;
    wki100_ver_major: LongInt;
    wki100_ver_minor: LongInt;
  end;
  T_WKSTA_INFO_100 = _WKSTA_INFO_100;
var
  DLLHandle: THandle;
  Info: P_WKSTA_INFO_100;
  NetWkstaGetInfo: function(servername: PAnsiChar; Level: DWord; var Buf:
    P_WKSTA_INFO_100): DWORD; Stdcall;
begin
  Result := '';
  DLLHandle := LoadLibrary('NETAPI32.DLL');
  if DLLHandle = 0 then //Can't load DLL
    exit;
  @NetWkstaGetInfo := GetProcAddress(DLLHandle, 'NetWkstaGetInfo');
  if @NetWkstaGetInfo = nil then //Exported function not found
  begin
    FreeLibrary(DLLHandle);
    exit;
  end;
  if NetWkstaGetInfo(nil, 100, Info) = 0 then
    Result := Info^.wki100_langroup;
  FreeLibrary(DLLHandle);
end;

Nincsenek megjegyzések:

Megjegyzés küldése