Need help with delphi programing

Questions about programming languages and debugging
Post Reply
User avatar
luggyx
Life time student
Life time student
Posts: 135
Joined: 03 May 2007, 16:00
16

Need help with delphi programing

Post by luggyx »

I just started learning programing with Delphi I made some simple programs. And now I am trying make program witch will show my dinamic ip. But I get error and I don't know how fix it. :cry: Please help.

Code: Select all

unit ip3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin

uses Winsock; <<<<<Error is here

function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
  Name = array[0..100] of Char; 
  PName = ^Name; 
var 
  HEnt: pHostEnt; 
  HName: PName; 
  WSAData: TWSAData; 
  i: Integer; 
begin 
  Result := False;     
  if WSAStartup($0101, WSAData) <> 0 then begin 
    WSAErr := 'Winsock is not responding."'; 
    Exit; 
  end; 
  IPaddr := ''; 
  New(HName); 
  if GetHostName(HName^, SizeOf(Name)) = 0 then
  begin 
    HostName := StrPas(HName^); 
    HEnt := GetHostByName(HName^); 
    for i := 0 to HEnt^.h_length - 1 do 
     IPaddr :=
      Concat(IPaddr,
      IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.'); 
    SetLength(IPaddr, Length(IPaddr) - 1); 
    Result := True; 
  end
  else begin 
   case WSAGetLastError of
    WSANOTINITIALISED:WSAErr:='WSANotInitialised'; 
    WSAENETDOWN      :WSAErr:='WSAENetDown'; 
    WSAEINPROGRESS   :WSAErr:='WSAEInProgress'; 
   end; 
  end; 
  Dispose(HName); 
  WSACleanup;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
var 
  Host, IP, Err: string; 
begin 
  if GetIPFromHost(Host, IP, Err) then begin 
    Edit1.Text := Host; 
    Edit2.Text := IP; 
  end 
  else 
    MessageDlg(Err, mtError, [mbOk], 0); 
end;

end.

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

umm....maybe it would help if you post WHAT error you get, because I doubt pehmm...I am not familiar with Delphi, but I guess it's similar to other languages like Perl....so don't you have to open a socket first that listens?

another thought when reading the source was:
if "uses Winsock;" initializes a function don't there have to be a () like

Code: Select all

 uses Winsock();
?
:-k

User avatar
sternbildchen
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 421
Joined: 26 Apr 2006, 16:00
17
Location: Germany

Post by sternbildchen »

"uses Winsock;" is at the wrong position imho.

You have to add winsock to the uses at the start of the file.

try to replace

Code: Select all

uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; 
with:

Code: Select all

uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Winsock; 
You added it to a procedure, which is even unfinished because an end; is missing.

Just change

http://codeviewer.org/view/code:20 (stupid html filter)

which will delete the procedure.

User avatar
luggyx
Life time student
Life time student
Posts: 135
Joined: 03 May 2007, 16:00
16

Post by luggyx »

Thx my program work now

User avatar
sternbildchen
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 421
Joined: 26 Apr 2006, 16:00
17
Location: Germany

Post by sternbildchen »

Glad to help. :D

Post Reply