Fórum Descobrir o IP #237651

14/06/2004

0

Olá pessoal, estou com o seguinte problema: preciso retornar o número do IP da máquina quando ela se conecta no SPeedy.... HOME. Cada vez que o operador se conecta eu preciso ver qual é meu IP na rede da Internet. Só que ao inves disso o programa me retorna o IP local da máquina, tipo 192.168.0.1 e não 200.xxx.xxx.xxx. Já testei com todos os componentes da indy e não tive sucesso. Estou usando o Delphi 7.

Muito agradecido... :(


Os1000r

Os1000r

Responder

Posts

14/06/2004

Afarias

vc tá pegando o IP da primeira controladora de rede, tem q pegar o da segunda.!


T+


Responder

Gostei + 0

14/06/2004

Otto

Olá, tente o seguinte:


const
  ANY_SIZE      = 1;

type
  PTMibIPAddrRow = ^TMibIPAddrRow;
  TMibIPAddrRow = packed record
    dwAddr: DWORD;
    dwIndex: DWORD;
    dwMask: DWORD;
    dwBCastAddr: DWORD;
    dwReasmSize: DWORD;
    Unused1,
      Unused2: WORD;
  end;
  PTMibIPAddrTable = ^TMibIPAddrTable;
  TMibIPAddrTable = packed record
    dwNumEntries: DWORD;
    Table: array[0..ANY_SIZE - 1] of TMibIPAddrRow;
  end;

function GetIpAddrTable( pIpAddrTable: PTMibIPAddrTable;
  pdwSize: PULONG;
  bOrder: BOOL ): DWORD;
stdcall; external ´IPHLPAPI.DLL´;

{ converts IP-address in network byte order DWORD to dotted decimal string}
function IpAddr2Str( IPAddr: DWORD ): string;
var
  i : integer;
begin
  Result := ´´;
  for i := 1 to 4 do
  begin
    Result := Result + Format( ´¬3d.´, [IPAddr and $FF] );
    IPAddr := IPAddr shr 8;
  end;
  Delete( Result, Length( Result ), 1 );
end;

procedure Get_IPAddrTable(Tipo: Integer; List: TStrings );
var
  IPAddrRow     : TMibIPAddrRow;
  TableSize     : DWORD;
  ErrorCode     : DWORD;
  i             : integer;
  pBuf          : PChar;
  NumEntries    : DWORD;
begin
  if not Assigned( List ) then EXIT;
  List.Clear;
  TableSize := 0; ;
  // first call: get table length
  ErrorCode := GetIpAddrTable( PTMibIPAddrTable( pBuf ), @TableSize, true );
  if Errorcode <> ERROR_INSUFFICIENT_BUFFER then
    EXIT;

  GetMem( pBuf, TableSize );
  // get table
  ErrorCode := GetIpAddrTable( PTMibIPAddrTable( pBuf ), @TableSize, true );
  if ErrorCode = NO_ERROR then
  begin
    NumEntries := PTMibIPAddrTable( pBuf )^.dwNumEntries;
    if NumEntries > 0 then
    begin
      inc( pBuf, SizeOf( DWORD ) );
      for i := 1 to NumEntries do
      begin
        Case Tipo of
        1 : begin
             IPAddrRow := PTMIBIPAddrRow( pBuf )^;
             with IPAddrRow do
               List.Add( Format( ´¬8.8x|¬15s|¬15s|¬15s|¬8.8d´,
                 [dwIndex, IPAddr2Str( dwAddr ), IPAddr2Str( dwMask ),
                   IPAddr2Str( dwBCastAddr ), dwReasmSize ] ) );
             inc( pBuf, SizeOf( TMIBIPAddrRow ) );
            end;
        2 : begin
             IPAddrRow := PTMIBIPAddrRow( pBuf )^;
             List.Add( Format(´¬15s´,[IPAddr2Str(IPAddrRow.dwAddr)]) );
             inc( pBuf, SizeOf( TMIBIPAddrRow ) );
            end;
        //3 : //;
        end;
      end;
    end
    else
      List.Add( ´no entries.´ );
  end
  else
    List.Add( SysErrorMessage( ErrorCode ) );

  // we must restore pointer!
  dec( pBuf, SizeOf( DWORD ) + NumEntries * SizeOf( IPAddrRow ) );
  FreeMem( pBuf );
end;



coloque no form um combobox, memo e um botao. no onclick do botão faça isso:

Get_IPAddrTable(1, Memo1.Lines);
Get_IPAddrTable(2, ComboBox1.Items);




T+. :wink:


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar