Solved

what is wring with code get IP & TCP Installed installation

Posted on 2010-08-13
6
362 Views
Last Modified: 2012-05-10
I#m using these little function to get IP Number and information if TCP is avail. since a long time.

It has been OK since 5 years and inside a test application it also works fine, but in the target application i'm calling that 2 function a get a access violation inside ws2_32.dll  while doing a gethostname call

why ?

do not net any other solution code  eg. with INDY ......
function GetIPAddress: string;

var    phoste  : PHostEnt;

       Buffer  : PAnsiChar;

       WSAData : TWSADATA;

begin

  // result := '';

  if WSAStartup($0101, WSAData) <> 0 then exit;

  GetHostName(Buffer, SizeOf(Buffer));

  phoste :=GetHostByName(buffer);

  if phoste = nil then result := '127.0.0.1' else

  result := StrPas(inet_ntoa(PInAddr(phoste^.h_addr_list^)^));

  WSACleanup;

end;







function IsTCPIP: boolean;

var wsaData: TWSAData;

begin

   result := true;

   case Winsock.WSAStartup($0101, wsaData) of

    WSAEINVAL, WSASYSNOTREADY, WSAVERNOTSUPPORTED: result := false;

   else

    Winsock.WSACleanup

   end;

end;

Open in new window

0
Comment
Question by:BdLm
6 Comments
 
LVL 14

Expert Comment

by:systan
ID: 33435634
.
function GetIPAddress: string;

var    phoste  : PHostEnt;

       Buffer  : PAnsiChar;

       WSAData : TWSADATA;

begin

  result := '127.0.0.1';

  if WSAStartup($0101, WSAData) <> 0 then exit;

  GetHostName(Buffer, SizeOf(Buffer));

  phoste :=GetHostByName(buffer);

  if phoste = nil then result := '127.0.0.1' else

  result := StrPas(inet_ntoa(PInAddr(phoste^.h_addr_list^)^));

  WSACleanup;

end;







function IsTCPIP: boolean;

var wsaData: TWSAData;

begin

   case Winsock.WSAStartup($0101, wsaData) of

   WSAEINVAL, WSASYSNOTREADY, WSAVERNOTSUPPORTED:

   result := false;

   else

   result := true;

   end;

   Winsock.WSACleanup

end;

Open in new window

0
 
LVL 8

Author Comment

by:BdLm
ID: 33435637
can you understand the errpor mechanism ? 

must a PAnsichar always have a value assigend before calling  function hostname ? 

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33436313
solution with indy is very easy:


var
  IdIPWatch1: TIdIPWatch;

  IdIPWatch1.Active := True;
  ShowMessage(IdIPWatch1.CurrentIP);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Author Comment

by:BdLm
ID: 33439716
the AV is gone with the new line of code , but still the buffer seem to be nil, a working reference code I found at the net, pretty similiar but working compared to mine.

Can't get a idea why this code works but my code doee not ....  



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;

Open in new window

0
 
LVL 14

Accepted Solution

by:
systan earned 500 total points
ID: 33440152
Are you trying to get your own ip address? or from any host?
Here is the code if you want it;
Just create a new form with
  Label1: TLabel;
  Edit1: TEdit;
  Button1: TButton;

  ListBox1: TListBox;
  ListBox2: TListBox;
  Label2: TLabel;

Double click the form, double click the button, erase all the default codes inside, then copy and paste the code below.

unit Unit1;



interface



uses winsock,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)



    Label1: TLabel;

    Edit1: TEdit;

    Button1: TButton;



    ListBox1: TListBox;

    ListBox2: TListBox;

    Label2: TLabel;



    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure ListBox1DblClick(Sender: TObject);



  private

    { Private declarations }

    function NetCheck(const networkfoldertosave:string): boolean;

    procedure netSave(const netcpufoldername:string);



  public

    { Public declarations }



  end;



var

  Form1: TForm1;



implementation

 uses ShellApi;

{$R *.dfm}



function GetRemoteIPAddress(ComputerName : string) : string;

var

  wsdata : TWSAData;

  he : PHostEnt;

  ss : pchar;

  ip : TInAddr;

begin

  WSAStartup(MakeWord(1, 1), wsdata);

  he := gethostbyname(pchar(ComputerName));

  if he<>nil then begin

    ip.S_addr := integer(pointer(he^. h_addr_list^)^);

    ss := inet_ntoa(ip);

    Result := string(ss);

  end;

  WSACleanup();

end;



procedure EnumRemoteDrives(Items: TStringlist);

function EnumerateFunc(lpnr: PNetResource; Items: TStringList; DrillDepth: Integer): Boolean;

var

lpnrLocal: PNetResource;

hEnum:THandle;

dwResult, dwResultEnum, cbBuffer, cEntries: DWORD;

i: Integer;

p1:PChar;

p2:PnetResource;

s, t: string;

begin

dwResult := WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_DISK, 0, lpnr, hEnum);

if dwResult <> NO_ERROR then begin EnumerateFunc := False;

exit;

end;

cbBuffer := 32768;

cEntries := $FFFFFFFF;

lpnrLocal := PNetResource(GlobalAlloc(GPTR,cbBuffer));

repeat

dwResultEnum := WNetEnumResource(hEnum,cEntries, lpnrLocal, cbBuffer);

if dwResultEnum = NO_ERROR then begin

for i := 0 to cEntries - 1 do begin

p1 := PChar(lpnrLocal) + i *

SizeOf(TNetResource);

p2 := PNetResource(p1);

s := StrPas(p2^.lpRemoteName);

if Length(s) > 2 then

if (s[1] = '\') and

(s[2] = '\') then begin

t := Copy(s,

3, Length(s) - 2);

if Pos('\', t)

<> 0 then

items.Add(s);

end;

if (p2.dwUsage and RESOURCEUSAGE_CONTAINER) = RESOURCEUSAGE_CONTAINER then

if DrillDepth < 3 then

EnumerateFunc(p2, Items, DrillDepth + 1);

end;

end;

until dwResultEnum = ERROR_NO_MORE_ITEMS;

GlobalFree(HGLOBAL(lpnrLocal));

dwResult := WNetCloseEnum(hEnum);

if dwResult <> NO_ERROR then begin EnumerateFunc := False;

 exit;

  end;

EnumerateFunc := True;

end;

begin

EnumerateFunc(nil, Items, 0);

end;



function Tform1.NetCheck(const networkfoldertosave:string): boolean;

var

netx: string;

i: byte;

sl,nx : tstringlist;

p,q:integer;

begin

result:=false;

sl := tstringlist.create;

nx := tstringlist.create;

nx.Duplicates := dupIgnore;



EnumRemoteDrives(sl);

for i:= 0 to sl.Count-1 do

begin

netx:=sl[i];

listbox1.items.add(netx);

if uppercase(netx) = uppercase(networkfoldertosave) then result:=true;



q:=0;

p:=0;

repeat

inc(q);

p:=AnsiPos('\',netx);

netx:=copy(netx,p+1,length(netx));

until q=2;

p:=AnsiPos('\',netx);

delete(netx,p,length(netx));

if nx.IndexOf(netx)= -1 then nx.Add(netx);

end;



for q:= 0 to nx.Count-1 do listbox2.items.add(nx[q] + ' is ' +  GetRemoteIPAddress(nx[q]) );

sl.free;

nx.Free;

end;





procedure TForm1.netSave(const netcpufoldername:string);

begin

if netCheck(netcpufoldername) = true then

begin

//add your code the way you want it here

showmessage(netcpufoldername + ' is Active and ready to save files,  copying files now........');

end

else

begin

showmessage(netcpufoldername + ' is NOT Active and NOT ready to save files');

end;

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

Form1.Caption := 'WorkGroup Computers';

Label1.Caption := 'Find Sample:  \\COMPUTERNAME\FOLDERNAME';

Label2.Caption := 'Double click to open shared network';

Button1.Caption := 'Viewit';

Button1.Hint := 'Just hit me';

Edit1.Text := 'anyshared';

end;





procedure TForm1.Button1Click(Sender: TObject);

begin

Listbox1.Clear;

Listbox2.Clear;

netSave(Edit1.text);

//close;

end;





procedure TForm1.ListBox1DblClick(Sender: TObject);

begin

ShellExecute(Handle,'open', Pchar(listbox1.items[listbox1.itemINdex]), nil, nil, SW_SHOWNORMAL) ;

end;





end.

Open in new window

0
 
LVL 8

Expert Comment

by:GeneralTackett
ID: 33468559
other solution could be that you are referencing an old dll.. you could include this old dll in your program directory which would be searched first and all would be well as you have used it for years..  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 63
How to call a form that is in a DLL  from an application? 13 65
Delphi 2 51
Find and Replace Stream with 0s 8 57
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now