unit UnitDNS;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;
const
WM_RESOLVED = WM_USER+100;
Type
TWMRESOLVED = record
Msg: WORD;
wParam:Word;
lParam
WORD;
end;
type
TFormMain = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
EditPresentation: TEdit;
edtHost: TEdit;
BtnWSAAsyncGetHostByName: TButton;
BtnWSACancelAsyncRequest: TButton;
EditResult: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnWSAAsyncGetHostByNameClick(Sender: TObject);
procedure BtnWSACancelAsyncRequestClick(Sender: TObject);
private
{ Private declarations }
procedure WMRESOLVED(var Msg:TWMRESOLVED);Message WM_RESOLVED;
public
{ Public declarations }
end;
var
FormMain: TFormMain;
buf:HOSTENT;
hAsyncTaskHandle:THandle;
implementation
{$R *.DFM}
procedure TFormMain.WMRESOLVED(var Msg:TWMRESOLVED);
begin
if buf.h_addr=NIL then
Begin
EditResult.Text:='Failed to resolve';
exit;
end;
EditResult.Text:=Format('%d.%d.%d.%d',
[ord( buf.h_addr^[0]),
ord(buf.h_addr^[1]),
ord(buf.h_addr^[2]),
ord(buf.h_addr^[3])]);
edtHost.Text:=buf.h_name;
end;
procedure TFormMain.FormCreate(Sender: TObject);
Var
WSAData:TWSAData;
begin
//initiates use of WS2_32.DLL
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
raise Exception.Create('Winsock Version Error');
end;
procedure TFormMain.FormDestroy(Sender: TObject);
begin
WSACleanUP;
end;
procedure TFormMain.BtnWSAAsyncGetHostByNameClick(Sender: TObject);
begin
hAsyncTaskHandle:=WSAAsyncGetHostByName(Handle,WM_RESOLVED,pchar(EditPresentation.Text),pchar(@buf),MAXGETHOSTSTRUCT);
end;
procedure TFormMain.BtnWSACancelAsyncRequestClick(Sender: TObject);
begin
WSACancelAsyncRequest(hAsyncTaskHandle);
end;
end.