function Get_User_Name: string;
var
LogName: PChar;
Len: Cardinal;
begin
GetMem(LogName, 255);
Len := 255;
GetUserName(LogName, Len); // WNetGetUser(nil, LogName, Len);也可以
Result := string(LogName);
FreeMem(LogName);
end;
{------------------------------------------------------------------------}
{98下面}
function GetCurrentUserName: string;
const
cnMaxUserNameLen = 254;
var
sUserName: string;
dwUserNameLen: DWORD;
begin
dwUserNameLen := cnMaxUserNameLen - 1;
SetLength(sUserName, cnMaxUserNameLen);
GetUserName(PChar(sUserName), dwUserNameLen);
SetLength(sUserName, dwUserNameLen);
Result := sUserName;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetCurrentUserName);
end;
{------------------------------------------------------------------------}
{2000下面}
const
NameUnknown = 0; // Unknown name type.
NameFullyQualifiedDN = 1; // Fully qualified distinguished name
NameSamCompatible = 2; // Windows NT? 4.0 account name
NameDisplay = 3; // A "friendly" display name
NameUniqueId = 6; // GUID string that the IIDFromString function returns
NameCanonical = 7; // Complete canonical name
NameUserPrincipal = 8; // User principal name
NameCanonicalEx = 9;
NameServicePrincipal = 10; // Generalized service principal name
DNSDomainName = 11; // DNS domain name, plus the user name
procedure GetUserNameEx(NameFormat: DWORD;
lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
external 'secur32.dll' Name 'GetUserNameExA';
function LoggedOnUserNameEx(fFormat: DWORD): string;
var
UserName: array[0..250] of char;
Size: DWORD;
begin
Size := 250;
GetUserNameEx(fFormat, @UserName, @Size);
Result := UserName;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := LoggedOnUserNameEx(NameSamCompatible);
end;