1. Delphi 4 has a TNMSMTP control on the Internet Pallette that returns
your I.P in a string with one call.
2. use Winsock:
a. Start a new application
b. Add the Winsock unit to the uses clause.
c. Replace the form create procedure with the following:
procedure TForm1.FormCreate(Sender: TObject);
var
WSAData: TWSAData;
FUnit, Scratch: String;
HostEnt: PHostEnt;
begin // Initialise the sockets interface
WSAStartup($0101, WSAData); // Set FUnit to the name of the PC you want the IP address of.
// An empty string means the local machine.
FUnit := ''; // Get IP address of FUnit
HostEnt := gethostbyname(PChar(FUnit)); // Was call OK?
if HostEnt <> nil then begin
Scratch := IntToStr(ord(HostEnt^.h_addr^[0]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[1]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[2]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[3]));
end; //Clean up sockets interface
WSACleanUp; // Assign IP address to forms caption
Caption := Scratch;end;
your I.P in a string with one call.
2. use Winsock:
a. Start a new application
b. Add the Winsock unit to the uses clause.
c. Replace the form create procedure with the following:
procedure TForm1.FormCreate(Sender: TObject);
var
WSAData: TWSAData;
FUnit, Scratch: String;
HostEnt: PHostEnt;
begin // Initialise the sockets interface
WSAStartup($0101, WSAData); // Set FUnit to the name of the PC you want the IP address of.
// An empty string means the local machine.
FUnit := ''; // Get IP address of FUnit
HostEnt := gethostbyname(PChar(FUnit)); // Was call OK?
if HostEnt <> nil then begin
Scratch := IntToStr(ord(HostEnt^.h_addr^[0]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[1]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[2]));
Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[3]));
end; //Clean up sockets interface
WSACleanUp; // Assign IP address to forms caption
Caption := Scratch;end;
解决方案 »
- 此情可待成追忆(五)
- 我用idHppt去post一个网页,这个网页每次访问后自动跳转到的新网页才是我需要的。
- 登录的问题!
- 关于ADO的事务处理(SQL Server 2000)
- 在新机器上装DELPHI开发的程序建BDE的时候MSSQL没有怎么办!!谢谢
- 怎样实现抽题时不重复up有分
- 关于TFileStream的问题!
- 我想问问如何有没有关于如何实现这种技术的文章,或资料
- 密码加密分析,清高手指点下, 谢谢。
- 富翁又可以上了,感覺速度還不錯。 DELPHI 用戶有福了 wwww.delphibbs.com(內空)
- 请问如何减少exe文件的大小?
- 编译的问题:我的程序编译后出现警告 谁知道? 先谢谢了
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;
注意,需要use WinSock Unit.
Windows, Sysutils, WinSock;function NameToIP(Name: string): string;
function IPToName(IP: string): string;
function DllStartUp: Boolean;
function DllCleanUp: Boolean;implementationfunction DllStartUp: Boolean;
var
wVersionRequested: WORD;
WSADATA: TWSAData;
err: Integer;
begin
Result := True;
wVersionRequested := MAKEWORD( 2, 0 );
err := WSAStartup( wVersionRequested, WSADATA );
if (err<>0)or(LOBYTE(WSADATA.wVersion)<>2)or(HIBYTE(WSADATA.wVersion)<>0) then
begin
WSACleanup;
Result := False;
end;
end;function DllCleanUp: Boolean;
begin
WSACleanup;
Result := True;
end;function IPToName(IP: string): string;
var
p: PHostEnt;
INetAddr: dword;
begin
Result := '';
INetAddr := INet_Addr(PChar(IP));
p := GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
if p <> nil then
Result := p^.h_name;
end;function NameToIP(Name: string): string;
var
HostEnt: PHostEnt;
begin
Result := '';
HostEnt := GetHostByName(PChar(Name));
if HostEnt <> nil then
begin
with HostEnt^ do
Result := Format('%.3d.%.3d.%.3d.%.3d', [Byte(h_addr^[0]),
Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
end;end.uses MyFastNet;TForm1.OnCreate:
DllStartUp;
...TForm1.OnDestroy:
DllCleanUp;
...TForm1.Button1Click:
NameToIP(Name);
IPToName(Ip);
...