Hej Emil
Her en funktion fra min egen "netværksscanner" komponent.
Den burde hjælpe dig lidt hen af vejen (selvom jeg har kommenteret den på engelsk).
function TSFHWorkgroupEnum.GetHostIP(Host : string; var ipstr : string) : DWORD;
{ Function result is a DWORD which indicates which WSA error
occurred. Zero means no errors. The resolved ip is returned
in "var ipstr". }
var
hEntity : pHostEnt;
WSAData : TWSAData;
IpAddr : string;
i : integer;
begin
{ WinSock init }
if WSAStartup($0101, WSAData) <> 0 then begin
Result := WSAGetLastError;
Exit;
end;
{ Strip any leading \\ char from host }
while (Host[1] = '\\') do delete(host, 1, 1);
{ Get host info }
hEntity := GetHostByName(PCHAR(Host));
{ Convert Ip to a string }
if (hEntity <> nil) then begin
{ Build the ip string }
for i := 0 to hEntity^.h_length - 1 do
IPaddr := Concat(IPaddr, IntToStr(Ord(hEntity^.h_addr_list^)) + '.');
{ Shorten string length }
SetLength(IPaddr, Length(IPaddr) - 1);
{ Return result }
IpStr := IPaddr;
Result := 0;
end else
Result := WSAGetLastError;
{ Stop WinSock }
WSACleanup;
end;
Den kommer med fejl når jeg kompilerer den:
[Error] Unit1.pas(36): Undeclared identifier: 'pHostEnt'
[Error] Unit1.pas(37): Undeclared identifier: 'TWSAData'
[Error] Unit1.pas(43): Undeclared identifier: 'WSAStartup'
[Warning] Unit1.pas(43): Comparing signed and unsigned types - widened both operands
[Error] Unit1.pas(44): Undeclared identifier: 'WSAGetLastError'
[Error] Unit1.pas(52): Undeclared identifier: 'GetHostByName'
[Error] Unit1.pas(55): Operator not applicable to this operand type
[Error] Unit1.pas(57): Pointer type required
[Error] Unit1.pas(68): Undeclared identifier: 'WSACleanup'
[Error] Unit1.pas(73): Types of actual and formal var parameters must be identical
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
Her kan du se hvor fejlene kommer:
<pre>{01} unit Unit1;
{02}
{03} interface
{04}
{05} uses
{06} Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
{07} Dialogs, StdCtrls;
{08}
{09} type
{10} TForm1 = class(TForm)
{11} Edit1: TEdit;
{12} Button1: TButton;
{13} Edit2: TEdit;
{14} procedure Button1Click(Sender: TObject);
{15} private
{16} { Private declarations }
{17} function GetHostIP(Host : string; var ipstr : string) : DWORD;
{18} public
{19} { Public declarations }
{20} end;
{21}
{22} var
{23} Form1: TForm1;
{24}
{25} implementation
{26}
{27} {$R *.dfm}
{28}
{29} function TForm1.GetHostIP(Host : string; var ipstr : string) : DWORD;
{30}
{31} { Function result is a DWORD which indicates which WSA error
{32} occurred. Zero means no errors. The resolved ip is returned
{33} in "var ipstr". }
{34}
{35} var
{36} hEntity : pHostEnt;
{37} WSAData : TWSAData;
{38} IpAddr : string;
{39} i : integer;
{40}
{41} begin
{42} { WinSock init }
{43} if WSAStartup($0101, WSAData) <> 0 then begin
{44} Result := WSAGetLastError;
{45} Exit;
{46} end;
{47}
{48} { Strip any leading \\ char from host }
{49} while (Host[1] = '\\') do delete(host, 1, 1);
{50}
{51} { Get host info }
{52} hEntity := GetHostByName(PCHAR(Host));
{53}
{54} { Convert Ip to a string }
{55} if (hEntity <> nil) then begin
{56} { Build the ip string }
{57} for i := 0 to hEntity^.h_length - 1 do
{58} IPaddr := Concat(IPaddr, IntToStr(Ord(hEntity^.h_addr_list^)) + '.');
{59} { Shorten string length }
{60} SetLength(IPaddr, Length(IPaddr) - 1);
{61} { Return result }
{62} IpStr := IPaddr;
{63} Result := 0;
{64} end else
{65} Result := WSAGetLastError;
{66}
{67} { Stop WinSock }
{68} WSACleanup;
{69} end;
{70}
{71} procedure TForm1.Button1Click(Sender: TObject);
{72} begin
{73} GetHostIP(Edit1.Text, Edit2.Text);
{74} end;
{75}
{76} end.</pre>Emil Melgaard
emil.melgaard@private.dk
- Admin på gruppen Delphi spilprogrammering.
http://www.delphispilprogram.udvikleren.dk