Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何判断计算机是否是域的一部分_Delphi_Networking - Fatal编程技术网

Delphi 如何判断计算机是否是域的一部分

Delphi 如何判断计算机是否是域的一部分,delphi,networking,Delphi,Networking,delphixe,Win7 我需要以下功能: Function isPcInDomain:bool; 我对域名不感兴趣,msdn LsaQueryInformationPolicy也不感兴趣,或者在Delphi上实现。我想有一个功能,不需要查询网络。 此外,如果计算机的本地名称包含一个 试图定义域名,但在不在域中的个人计算机上-因为域名给出了本地工作组的名称:您可以使用: uses LSAApi; function isPcInDomain : Boolean; var Comput

delphixe,Win7

我需要以下功能:

Function isPcInDomain:bool;
我对域名不感兴趣,msdn LsaQueryInformationPolicy也不感兴趣,或者在Delphi上实现。我想有一个功能,不需要查询网络。 此外,如果计算机的本地名称包含一个

试图定义域名,但在不在域中的个人计算机上-因为域名给出了本地工作组的名称:

您可以使用:

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;
您可以使用:


你的问题很难理解。你能再说一遍吗?有必要简单地定义一下,Windows Server 2008上的个人计算机在域中还是不在网络域中。我不知道该如何解释。例如,我们有一个网络域MyCompany。地方的有一台计算机具有名称测试。我们进入这个领域。计算机名称因测试而异。我的公司。地方的有一台名为Test2.pc的计算机和本地组Home。在函数体isPcInDomain中写什么在1例中返回True,在最后一例中返回False?@Gu-你是在问如何判断当前计算机是否在域中,还是不是域的一部分的独立PC?你的问题很难理解。你能再说一遍吗?有必要简单地定义一下,Windows Server 2008上的个人计算机在域中还是不在网络域中。我不知道该如何解释。例如,我们有一个网络域MyCompany。地方的有一台计算机具有名称测试。我们进入这个领域。计算机名称因测试而异。我的公司。地方的有一台名为Test2.pc的计算机和本地组Home。在函数体isPcInDomain中写入什么在1种情况下返回True,在最后一种情况下返回False?@Gu-你是在问如何判断当前计算机是否在域中,还是它是一台独立的PC,不属于域的一部分?谢谢,可以在LsaApi.pas上写入引用或在何处下载?好的,找到:谢谢,可以在LsaApi.pas或下载位置写入参考?确定,找到:
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.