function GetCurrComputeName: String; //得到当前的计算机名 var CNameBuffer: PChar; fl_loaded: Boolean; CLen: ^DWord; begin GetMem(CNameBuffer,255); New(CLen); CLen^:= 255; fl_loaded := GetComputerName(CNameBuffer,CLen^); if fl_loaded then Result := StrPas(CNameBuffer) else Result := 'Unkown'; FreeMem(CNameBuffer,255); Dispose(CLen); end;//uses WinSockfunction GetIPFromComputeName(ACompName: String): String; //根据计算机名取得IP type //copy the add_list TAddrList = array [0..20] of PInAddr; PAddrList = ^TAddrList; var phent: PHostEnt; P: PAddrList; WSAData: TWSAData; begin //initiates use of WS2_32.DLL if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then raise Exception.Create('Winsock Version Error');{ CopyRight (C) Bowman 2000 } { [email protected] } //detect yahoo.com phent := GetHostByName(PChar(ACompName)); if phent<>nil then begin P := PAddrList(phent^.h_Addr_list); if P[0] <> nil then Result := StrPas(inet_ntoa(P[0]^)); end; WSACleanUP; end;
VB的代码,其中我的可以运行,你可以参考一下
TechnoFantasy(www.applevb.com
var
CNameBuffer: PChar;
fl_loaded: Boolean;
CLen: ^DWord;
begin
GetMem(CNameBuffer,255);
New(CLen);
CLen^:= 255;
fl_loaded := GetComputerName(CNameBuffer,CLen^);
if fl_loaded then
Result := StrPas(CNameBuffer)
else
Result := 'Unkown';
FreeMem(CNameBuffer,255);
Dispose(CLen);
end;//uses WinSockfunction GetIPFromComputeName(ACompName: String): String; //根据计算机名取得IP
type
//copy the add_list
TAddrList = array [0..20] of PInAddr;
PAddrList = ^TAddrList;
var
phent: PHostEnt;
P: PAddrList;
WSAData: TWSAData;
begin
//initiates use of WS2_32.DLL
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
raise Exception.Create('Winsock Version Error');{ CopyRight (C) Bowman 2000 }
{ [email protected] }
//detect yahoo.com
phent := GetHostByName(PChar(ACompName));
if phent<>nil then
begin
P := PAddrList(phent^.h_Addr_list);
if P[0] <> nil then
Result := StrPas(inet_ntoa(P[0]^));
end;
WSACleanUP;
end;