Delphi Xe,Win7
我需要以下功能:
Function isPcInDomain:bool;
域名对我不感兴趣,msdn LsaQueryInformationPolicy也是如此(或者在Delphi上实现)。我想要一个不需要查询网络的功能
如果计算机的本地名称包含.
尝试定义域名,但在不在域中的个人计算机上 - 作为域名给出了本地工作组的名称:(
答案 0 :(得分:7)
您可以使用NetGetJoinInformation:
program test;
{$APPTYPE CONSOLE}
{$MINENUMSIZE 4}
uses
Windows, SysUtils;
const
netapi = 'netapi32.dll';
type
TNetSetupJoinStatus = (NetSetupUnknownStatus, NetSetupUnjoined, NetSetupWorkgroupName, NetSetupDomainName);
function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external netapi;
function NetGetJoinInformation(lpServer: PWideChar; out lpNameBuffer: PWideChar;
out BufferType: TNetSetupJoinStatus): Cardinal; stdcall; external netapi;
procedure NetApiCheck(RetValue: Cardinal);
begin
if RetValue <> ERROR_SUCCESS then
RaiseLastOSError(RetValue);
end;
function GetJoinInfo(out JoinStatus: TNetSetupJoinStatus): WideString;
var
P: PWideChar;
begin
NetApiCheck(NetGetJoinInformation(nil, P, JoinStatus));
Result := P;
NetApiBufferFree(P);
end;
procedure Main;
const
JoinStatusStrings: array[TNetSetupJoinStatus] of string = ('Unknown', 'Unjoined', 'Workgroup', 'Domain');
var
Info: TNetSetupJoinStatus;
S: WideString;
begin
S := GetJoinInfo(Info);
Writeln(Format('%s %s', [JoinStatusStrings[Info], S]));
end;
begin
try
Main;
except
on E: Exception do
begin
ExitCode := 1;
Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
end;
end;
end.
答案 1 :(得分:2)
uses
LSAApi;
function isPcInDomain : Boolean;
var
ComputerName : TLSAUnicodeStr;
Attributes : TLsaObjectAttributes;
PolicyHandle : LSA_HANDLE;
Status : NTStatus;
Buffer : Pointer;
PolicyAccountDomainInfo : PPolicyAccountDomainInfo;
begin
ComputerName := TLsaUnicodeStr.CreateFromStr('');
try
FillChar(Attributes, SizeOf(Attributes), 0);
Status := LsaOpenPolicy(ComputerName.Value, Attributes, POLICY_VIEW_LOCAL_INFORMATION, PolicyHandle);
if Status <> STATUS_SUCCESS then
raise Exception.Create('LsaOpenPolicy Failed: '+SysErrorMessage(LsaNtStatusToWinError(Status)));
try
Status := LsaQueryInformationPolicy(PolicyHandle, PolicyPrimaryDomainInformation, Buffer);
if Status <> STATUS_SUCCESS then
raise Exception.Create('LsaQueryInformationPolicy Failed: '+SysErrorMessage(LsaNtStatusToWinError(Status)));
try
PolicyAccountDomainInfo := Buffer;
Result := PolicyAccountDomainInfo.DomainSID <> nil
finally
LsaFreeMemory(Buffer)
end;
finally
LsaClose(PolicyHandle)
end;
finally
ComputerName.Free;
end;
end;