Delphi获取计算机名,IP地址,登录用户。
得到本地计算机名
function GetComputerName: AnsiString;
var
lpBuffer: array [0 .. MAX_PATH] of char;
dwSize: DWORD;
begin
dwSize := MAX_PATH;
if not Windows.GetComputerName(lpBuffer, dwSize) then
raise Exception.Create(SysErrorMessage(GetLastError()));
Result := StrPas(lpBuffer);
end;
得到系统登录名
procedure TForm1.btn4Click(Sender: TObject);
var
Username: pChar;
nSize: Cardinal;
begin
Username := StrAlloc(30);
nSize := 30;
GetUserName(Username, nSize);
ShowMessage(Username);
StrDispose(Username);
end;
得到系统的IP地址
function ComputerLocalIp: string;
var
ch: array [1 .. 32] of char;
wsData: TWSAData;
myHost: phostEnt;
i: Integer;
begin
Result := '';
if wsastartup(2, wsData) <> 0 then // can't start winsock
exit;
try
if gethostname(@ch[1], 32) <> 0 then
exit; // gethost fiald;
except
exit;
end;
myHost := gethostbyname(@ch[1]); //
if myHost = nil then
exit;
for i := 1 to 4 do
begin
Result := Result + IntToStr(Ord(myHost.h_addr^[i - 1]));
if i < 4 then
Result := Result + '.';
end;
end;