Solved

I need the IP of the PPP adapter, not the IP of my ethernet card

Posted on 2000-05-17
11
381 Views
Last Modified: 2010-04-04
hey, i can check out my IP address with that function. When the only network adapter is the ppp adapter, it works out fine to let me know my internet IP address. But it does not, when i have an ethernet card and another network driver.



/////////
// uses winsock by the way
////////
function LocalIP : string;
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;
0
Comment
Question by:mahara
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Expert Comment

by:inthe
Comment Utility
hi mahara,
try this one posted the other day:
<start paste>

unit ipaddy1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
  StdCtrls, ExtCtrls, Psock;


type
  TName = array[0..100] of Char;
  PName = ^TName;


type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Timer1: TTimer;
    Edit3: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    NMFTP1: TNMFTP;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NMFTP1Connect(Sender: TObject);
    procedure NMFTP1Success(Trans_Type: TCmdType);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;



var
  Form1: TForm1;
  sIP, sHost, sErr: string;
   buffer: array[0..50] of char;
  p: trect;
  ftpconnect,changedir,connected: boolean;
  p3,p4: trect;
  n: integer;
  result: byte;
  counter: longint;
  wnd,wnd2: hwnd;
  p2: tpoint;
  i1 : integer;
  r1: trect;
  tmpname,tmpclass,s1 : string;

 function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;


implementation

{$R *.DFM}


function GetHostIP(var sHostName, sIPAddr, sWSAError: string): Boolean;
var
  HEnt: pHostEnt;
  HName: PName;
  WSAData: TWSAData;
  iCnt: Integer;
begin
  Result := False;
  if WSAStartup($0101, WSAData) <> 0 then begin
    sWSAError := 'WSAStartup error';
    Exit;
  end;
  sHostName := '';
  sIPAddr := '';
  sWSAError := '';
  New(HName);
  if GetHostName(HName^, SizeOf(TName)) = 0 then begin
    Result := True;
    sHostName := StrPas(HName^);
    HEnt := GetHostByName(HName^);
    for iCnt := 0 to HEnt^.h_length - 1 do
      sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
    SetLength(sIPAddr, Length(sIPAddr) - 1);
  end
  else begin
    case WSAGetLastError of
      WSAEFAULT        : sWSAError := 'WSAEFault';
      WSANOTINITIALISED: sWSAError := 'WSANotInitialised';
      WSAENETDOWN      : sWSAError := 'WSAENetDown';
      WSAEINPROGRESS   : sWSAError := 'WSAEInProgress';
    end;
  end;
  Dispose(HName);
  WSACleanup;
end;


procedure TForm1.FormActivate(Sender: TObject);
begin
  if GetHostIP(sHost, sIP, sErr) then begin
    Edit1.Text := sHost;
    Edit2.Text := sIP;
  end

end;


end.


sorry, i quickly stripped out of the code u did not.... The vars, you will have to sort out yourself.....

This code will return the ip address of the machine on the internet, even if its on a network and without the use of a username....


<end paste>
0
 
LVL 2

Author Comment

by:mahara
Comment Utility
hi inthe,

what's that unit psock?
the code does not work, it doesn't understand a PName.

i'm using d-4

0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
hang on i test ;-)
psock ?
probably just delete it..
ill be back
0
 
LVL 2

Author Comment

by:mahara
Comment Utility
hi inthe,

sorry i rejected, please do post an answer again, and let me accept it.

i thought it was the missing psock, found out that it was not!

cheers! good run you have, how fast are you :))))))

mahara
0
 
LVL 2

Author Comment

by:mahara
Comment Utility
hi inthe,


waitin' for your ans.

are you icq-online and available recently?


0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
Comment Utility
yes i retested and deleted all irrelevent code.it works nicely ;-)

yes have icq but not here .
dont know number off hand .

username is "inthe" (surprise eh ;-)
what your user name/number?
i speak some time when your online and i have icq at work .
cheers Barry


0
 
LVL 1

Expert Comment

by:she3i3i
Comment Utility
This still does not give me the the IP address of the PPP adapter.

My PC should have 3 IPs as follows.

1. 127.0.0.1 <- localhost
2. 192.168.0.1 <- ethernet adapter
3. xxx.xxx.xxx.xxx <- PPP adapter (dynamic internet IP)

Your code, returns the IP of the ethernet adapter.

How can I get it to safely return the IP of the PPP adapter?


Chris.
0
 
LVL 2

Author Comment

by:mahara
Comment Utility
it would return the ip of ppp adapter when the ppp adapter is connected. otherwise it'd return the eth. adapter.

at least, it works for me...
0
 
LVL 1

Expert Comment

by:she3i3i
Comment Utility
inthe,

   Although this seems to have worked for mahara, it doesn't for me, it always returns the ip of the ethernet adapter, whether my ppp adapter is connected or not.

   Any ideas? I'll give you points if you can help me.


Cheers, Chris.
0
 
LVL 2

Author Comment

by:mahara
Comment Utility
it doesn't only work for me, but seems to work pretty well for 4000 customers of mine. i'll just check out to see if the code i've sent is accurately the same with the one in my project.

cheers.
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
hello she3i3i ,mahara

try this one it works good for me:


unit Unit1;

interface

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

type
  PMIB_IPADDRROW = ^TMIB_IPADDRROW;
  TMIB_IPADDRROW = record
                     dwAddr: dword;
                     dwIndex: dword;
                     dwMask: dword;
                     dwBCastAddr: dword;
                     dwReasmSize: dword;
                     unused1: word;
                     unused2: word;
                   end;  { TMIB_IPADDRROW }

  PMIB_IPADDRTABLE = ^TMIB_IPADDRTABLE;
  TMIB_IPADDRTABLE = record
                       dwNumEntries: dword;
                       Table: array[0 .. 0] of TMIB_IPADDRROW;
                     end; { TMIB_IPADDRTABLE }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function GetIpAddrTable(pIpAddrTable: PMIB_IPADDRTABLE;
                        var pdwSize:  dword;
                        bOrder: BOOL): dword; stdcall; external 'IPHLPAPI.DLL';

procedure TForm1.Button1Click(Sender: TObject);
var
  IpAddrTable: PMIB_IPADDRTABLE;
  Size, dwResult: dword;
  ErrorMessage: array[0 .. 256] of char;
  LibH: THandle;
  C: word;
begin
  { Confirm that the IP Helper API DLL exists }
  LibH := LoadLibrary('IPHLPAPI.DLL');
  if LibH = 0
    then Exit
    else FreeLibrary(LibH);
  Memo1.Lines.Clear;

  Size := 1;
  GetMem(IpAddrTable, Size);
  FillChar(IpAddrTable^, Size, #0);
  dwResult := GetIpAddrTable(IpAddrTable, Size, true);
  if dwResult = ERROR_INSUFFICIENT_BUFFER
    then
      begin
        FreeMem(IpAddrTable);
        GetMem(IpAddrTable, Size);
        FillChar(IpAddrTable^, Size, #0);
        dwResult := GetIpAddrTable(IpAddrTable, Size, true);
      end
    else
      begin
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, dwResult, 0,
                      @ErrorMessage, Sizeof(ErrorMessage) - 1, nil);
        Memo1.Lines.Add('Function GetIpAddrTable failed - ' + ErrorMessage);
      end;

  { Display all IP addresses }
  for C := 0 to IpAddrTable.dwNumEntries - 1 do
    Memo1.Lines.Add(inet_ntoa(in_addr(IpAddrTable.Table[C].dwAddr)));

  FreeMem(IpAddrTable);
end;

end.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

13 Experts available now in Live!

Get 1:1 Help Now