Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-05-17
11
Medium Priority
?
393 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

722 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