Solved

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

Posted on 2000-05-17
11
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2819587
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
ID: 2819608
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
ID: 2819620
hang on i test ;-)
psock ?
probably just delete it..
ill be back
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:mahara
ID: 2819628
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
ID: 2819636
hi inthe,


waitin' for your ans.

are you icq-online and available recently?


0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2819661
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
ID: 4093671
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
ID: 4093887
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
ID: 4108511
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
ID: 4109791
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
ID: 4133672
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

705 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