Var HostName:String; Name :pChar; i:integer; begin getmem(Name,254); i:=Sizeof(Name); GetHostName(Name,i); HostName:=Strpas(Name); Freemem(Name); end; 注意:uses idwinsock;
uses windows,winsock; const MAX_COMPUTERNAME_LENGTH = 255; ...{------------------------------------------------------} { 功能:取机器名 入口参数:无 返回结果:string类型的机器名 说明:是因为windows API返回的是PChar型才做的函数 } function get_computer_name : String; var pcComputer : PChar; dwCSize : DWORD; begin dwCSize := MAX_COMPUTERNAME_LENGTH + 1; GetMem( pcComputer, dwCSize ); // allocate memory for the string try if Windows.GetComputerName( pcComputer, dwCSize ) then Result := pcComputer; finally FreeMem( pcComputer ); // now free the memory allocated for the string end; end; {------------------------------------------------------} { 功能:取本地机器IP地址 入口参数:无 返回结果:string类型的IP地址 说明:uses winsock. } function get_computer_IP : string; type TaPInAddr = array [0..10] of PInAddr; PaPInAddr = ^TaPInAddr; var phe : PHostEnt; pptr : PaPInAddr; Buffer : array [0..63] of char; I : Integer; GInitData : TWSADATA;begin WSAStartup($101, GInitData); Result := ''; GetHostName(Buffer, SizeOf(Buffer)); phe :=GetHostByName(buffer); if phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list); I := 0; while pptr^[I] <> nil do begin result:=StrPas(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup; end;
var str: String;
pp: pChar;
iSize: Cardinal;
begin
iSize := 255;
GetMem(pp,255);
GetComputerName(pp,iSize);
str := StrPas(pp);
FreeMem(pp);
ShowMessage(str);
end;
在windows单元里.
自己用Widnows SDK很容易就查出来了。
HostName:String;
Name :pChar;
i:integer;
begin
getmem(Name,254);
i:=Sizeof(Name);
GetHostName(Name,i);
HostName:=Strpas(Name);
Freemem(Name);
end;
注意:uses idwinsock;
const
MAX_COMPUTERNAME_LENGTH = 255;
...{------------------------------------------------------}
{
功能:取机器名
入口参数:无
返回结果:string类型的机器名
说明:是因为windows API返回的是PChar型才做的函数
}
function get_computer_name : String;
var
pcComputer : PChar;
dwCSize : DWORD;
begin
dwCSize := MAX_COMPUTERNAME_LENGTH + 1;
GetMem( pcComputer, dwCSize ); // allocate memory for the string
try
if Windows.GetComputerName( pcComputer, dwCSize ) then
Result := pcComputer;
finally
FreeMem( pcComputer ); // now free the memory allocated for the string
end;
end;
{------------------------------------------------------}
{
功能:取本地机器IP地址
入口参数:无
返回结果:string类型的IP地址
说明:uses winsock.
}
function get_computer_IP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;