//作者:陆岛工作室 XOtec Studio
//组件库:XOtecExpress。
几个在Delphi 中取本机的计算机名、IP地址、Windows登录的用户名的函数.
uses Windows, WinSock;
{
ComputerLocalIP
}
// 取本机的 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 ;
// 取本机的 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 ;