Delphi自定义过程获取本机ip,名字,用户名
生活随笔
收集整理的這篇文章主要介紹了
Delphi自定义过程获取本机ip,名字,用户名
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//取本機(jī)的 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;
//取本機(jī)的計(jì)算機(jī)名
{ 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;
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;
//取本機(jī)的計(jì)算機(jī)名
{ 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;
總結(jié)
以上是生活随笔為你收集整理的Delphi自定义过程获取本机ip,名字,用户名的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Delphi自定义获取网卡MAC地址过程
- 下一篇: Delphi替换PE图标资源