- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi 获取计算机名和用户名
function CurrentUserName: String;
var
nSize: DWord;
begin
nSize := 1024;
SetLength(Result, nSize);
if GetUserName(PChar(Result), nSize) then
SetLength(Result, nSize - 1)
else
RaiseLastOSError;
end;
function CurrentDomain: String;
const
DNLEN = 255;
var
sid: PSID;
sidSize: DWord;
sidNameUse: DWord;
domainNameSize: DWord;
domainName: array [0 .. DNLEN] of Char;
begin
Result := '';
sidSize := 65536;
GetMem(sid, sidSize);
domainNameSize := DNLEN + 1;
sidNameUse := SidTypeUser;
try
if LookupAccountName(nil, PChar(CurrentUserName), sid, sidSize, domainName,
domainNameSize, sidNameUse) then
Result := strpas(domainName);
finally
FreeMem(sid);
end;
end;
EdtUsername.Text := CurrentUserName();
EdtDomain.Text := CurrentDomain();