我已经进行了大量搜索,并找到了一些有用的技巧right here on Stackoverflow来获取用户名和计算机名-甚至我将Norrit张贴在Delphipages上的found some code都做了我需要做的事情-除了在Win64目标平台上运行它时,获取全名不起作用-它返回一个空字符串。运行Win32构建时,它会返回全名。

GetCurrentUser和GetDomainServerName函数可在Win32和Win64中使用。

用Google搜索“ delphi netusergetinfo”会带来很多已有10多年历史的结果-即使在Delphi拥有Win64之前。它还提出了一个Experts-Exchange答案,该答案链接到我身上的404兰曼代码示例,所以我有点卡在那里。

有人知道我必须进行哪些更改才能使GetDomainFullName在Win64下工作吗?或者有人可以使用替代代码来获取当前登录用户的全名吗?

链接腐烂时,下面显示完整代码。

unit NetAPI32;

interface

uses
  Windows, SysUtils;

  function GetCurrentUser(): String;
  function GetDomainServerName(): String;
  function GetDomainFullName(ServerName, UserName: String): String;

implementation

  function NetUserGetInfo(ServerName, UserName: PWideChar; Level: DWORD; var Buffer: Pointer): DWORD; stdcall; external 'netapi32.dll' name 'NetUserGetInfo';
  function NetApiBufferFree(Buffer: pointer): DWORD; stdcall; external 'netapi32.dll' name 'NetApiBufferFree';
  function NetWkstaUserGetInfo(ServerName: PWideChar; Level: DWORD; var Buffer: Pointer): Longint; stdcall; external 'netapi32.dll' name 'NetWkstaUserGetInfo';

type
  TUserInfo1 = packed record
    UserName: PWideChar;
    DomainName : PWideChar;
    OtherDomainNames: PWideChar;
    ServerName: PWideChar;
  end;
  PUserInfo1 = ^TUserInfo1;

  TUserInfo2 = packed record
    Name: PWideChar;
    Password: PWideChar;
    PasswordAge: DWORD;
    Priv: DWORD;
    HomeDir: PWideChar;
    Comment: PWideChar;
    Flags: DWORD;
    ScriptPath: PWideChar;
    AuthorFlags: DWORD;
    FullName: PWideChar;
    UserComment: PWideChar;
    Params: PWideChar;
    WorkStations: PWideChar;
    LastLogon: DWORD;
    LastLogoff: DWORD;
    AccountExpires: DWORD;
    MaxStorage: DWORD;
    UnitsPerWeek: DWORD;
    LogonHours: DWORD;
    BadPasswordCount: DWORD;
    LogonCount: DWORD;
    Server: PWideChar;
    CountryCode: DWORD;
    Codepage: DWORD;
  end;
  PUserInfo2 = ^TUserInfo2;

function GetCurrentUser(): String;
var
  username: String;
  size: DWORD;
begin
  size := 255;
  SetLength(username, size) ;
  if GetUserName(PChar(username), size) then
    Result := Copy(username, 1, size - 1)
  else
    Result := '';
end;

function GetDomainServerName(): String;
var
  PUI1: PUserInfo1;
begin
  Result := '';
  if NetWkstaUserGetInfo(nil, 1, Pointer(PUI1)) = 0 then
  begin
    try
      Result := WideCharToString(PUI1^.ServerName);
    finally
      NetApiBufferFree(PUI1);
    end;
  end;
end;

function GetDomainFullName(ServerName, UserName: String): String;
var
  PUI2: PUserInfo2;
begin
  Result := '';
  if NetUserGetInfo(PWideChar(WideString(ServerName)), PWideChar(WideString(UserName)), 2, Pointer(PUI2)) = 0 then
    try
      Result := WideString(PUI2^.FullName);
    finally
      NetApiBufferFree(PUI2);
    end;
end;

end.


用法示例:

ShowMessage(GetDomainFullName(GetDomainServerName(), GetCurrentUser()));


在Windows 2012 R2上运行的Delphi XE6

最佳答案

packed的使用是错误的,并且一直是错误的。您之所以放弃它,是因为它碰巧使32位的布局与对齐的布局相同。

从结构声明中删除packed修饰符,您的代码将起作用。

由于您已移至Unicode Delphi,因此现在可以停止使用WideString

function GetDomainFullName(const ServerName, UserName: string): string;
var
  PUI2: PUserInfo2;
begin
  if NetUserGetInfo(PChar(ServerName), PChar(UserName), 2, Pointer(PUI2)) = 0 then
    try
      Result := PUI2^.FullName;
    finally
      NetApiBufferFree(PUI2);
    end
  else
    Result := '';
end;

10-08 08:29