如何在Delphi Win64中获取当前用户的全名

时间:2014-09-03 20:23:12

标签: delphi

我已经进行了大量的搜索,并找到了一些有用的提示right here on Stackoverflow来获取用户名和计算机名称 - 我甚至在{@ 3}}上发布了Norrit关于Delphipages的信息完全按照我的需要去做 - 除了在Win64目标平台下运行它时,获取全名不起作用 - 它返回一个空字符串。它在运行Win32版本时返回全名。

GetCurrentUser和GetDomainServerName函数在Win32和Win64中都有效。

Google搜索" delphi netusergetinfo"带来了很多超过10年的结果 - 甚至在Delphi使用Win64之前。它还提出了一个专家交流答案,该答案链接到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

1 个答案:

答案 0 :(得分:7)

使用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;