1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
//取本机的 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 Exit; // can’t start winsock try if GetHostName(@ch[1],32)<>0 then Exit; // getHostName failed except Exit; end; myHost := GetHostByName(@ch[1]); // GetHostName error 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; //取本机的计算机名 { ComputerName } function ComputerName: string; var FStr: PChar; FSize: Cardinal; begin FSize := 255; GetMem(FStr, FSize); Windows.GetComputerName(FStr, FSize); Result := FStr; FreeMem(FStr); end; //取Windows登录用户名 { WinUserName } function WinUserName: string; var FStr: PChar; FSize: Cardinal; begin FSize := 255; GetMem(FStr, FSize); GetUserName(FStr, FSize); Result := FStr; FreeMem(FStr); end; |
转载请注明:exchen's blog » Delphi 获取本机 ip 机器名 用户名