Recent

Author Topic: By Code get IP address - new problem [SOLVED AGAIN]  (Read 15059 times)

timcs

  • Full Member
  • ***
  • Posts: 213
By Code get IP address - new problem [SOLVED AGAIN]
« on: April 10, 2013, 06:32:32 pm »
I have searched this forum for this and found this post:
http://lazarus.firmos.at/index.php?topic=5653.0

When I add this code (including the winsock in the uses clause):

Quote
function getIPs: Tstrings;
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 := TstringList.Create;
  Result.Clear;
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I    := 0;
  while pPtr^ <> nil do
  begin
    Result.Add(inet_ntoa(pptr^^));
    Inc(I);
  end;
  WSACleanup;
end;                 


 I get this error :

Quote
Error: Operator is not overloaded: "TaPInAddr" = "Pointer"
 Error: Illegal qualifier

Any ideas

Thanks

TimCS
« Last Edit: April 16, 2013, 04:02:10 pm by timcs »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: By Code get IP address
« Reply #1 on: April 10, 2013, 07:32:14 pm »
It sounds a bit weird, but here is a working modification:
Code: [Select]
  while Assigned(pPtr^[i]) do
    begin
      Result.Add(inet_ntoa(pptr^[i]^));
      Inc(I);
      if i>10 then break;
  end;

Anyway, if you are behind a router or a modem-router (with most probably a NAT) using IPV4 for instance, you won't get really your "general" IP address (i.e. public IP address, on Internet), but only the IP address from your LAN (i.e; private IP address).

« Last Edit: April 10, 2013, 08:42:58 pm by ChrisF »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: By Code get IP address
« Reply #2 on: April 11, 2013, 07:25:10 am »
If you want to get your external IP address, see e.g. this link:
http://wiki.lazarus.freepascal.org/fphttpclient#Get_external_IP_address

... but I suspect the OP doesn't. He didn't really elaborate much in this post, though I suspect it's his subnet problems in other posts that are involved here.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address
« Reply #3 on: April 11, 2013, 09:13:43 am »
Sorry if I have not made the matter clear , I need to get the PC IP address not the public internet IP address, have I got the wrong code for this?


Thanks

TimCS

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: By Code get IP address
« Reply #4 on: April 11, 2013, 09:27:08 am »
Well, if you tried ChrisF's mods you should be able to check this comparing the output with
Code: [Select]
ipconfig /all
Failing that, googling on inet_ntoa might give some clues...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address
« Reply #5 on: April 11, 2013, 09:57:35 am »
Thanks BigChimp and thanks ChrisF - Chris that sorted the problem thank you.

The code does give back the local IP address as required - this is solved now

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address - new problem
« Reply #6 on: April 16, 2013, 12:31:06 pm »
Further to this code , I have to admit that I can only understand some of it and unbeknown to me, some of the staff in the company have more that one adapter in their PCs. I need to some how only work on one IP address from this code which could be done by looking for the first two parts of the IP address. The third is the sub net where it changes on the different networks.

so for example it would start 10.1 to begin with how would I best do this?

Thanks

TimCS

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12982
  • FPC developer.
Re: By Code get IP address - new problem
« Reply #7 on: April 16, 2013, 12:44:50 pm »
In general, if you are looking for the way (network card) to the outside world, you are looking for the default gateway.

In Windows it looks like this:

(do a "route print" in a cmd.exe prompt:)
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0    192.168.187.1  192.168.187.110     10
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    306
     (other entries)


You probably want the interface (here 192.168.187.110) and the gateway (192.168.186.1) for the entry that has 0.0.0.0 as "network destination".

Under windows, you can also enumerate network interfaces using the API, but I don't have code for that here.

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address - new problem
« Reply #8 on: April 16, 2013, 12:55:51 pm »
In general, if you are looking for the way (network card) to the outside world, you are looking for the default gateway.

I am not looking to go to the outside world , all I am trying to do is establish which PC is on which sub net it appears that our different departments start with the range 10.1. and then have different ranges from that point onwards.

Thanks anyway for this information however is there a way of getting the first two parts of the IP e.g 10.1 and then stop at that point?

Thanks

TimCS

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12982
  • FPC developer.
Re: By Code get IP address - new problem
« Reply #9 on: April 16, 2013, 01:16:38 pm »
In general, if you are looking for the way (network card) to the outside world, you are looking for the default gateway.

I am not looking to go to the outside world , all I am trying to do is establish which PC is on which sub net it appears that our different departments start with the range 10.1. and then have different ranges from that point onwards.

A PC can be in multiple subnets at the same time. So in such case there is no "the" IP.

But it sounds you simply need to enumerate all interfaces, and then implement your match. The earlier link for finding the external IP afaik contains windows code for enumerating interfaces

 

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address - new problem
« Reply #10 on: April 16, 2013, 01:30:25 pm »
In general, if you are looking for the way (network card) to the outside world, you are looking for the default gateway.

I am not looking to go to the outside world , all I am trying to do is establish which PC is on which sub net it appears that our different departments start with the range 10.1. and then have different ranges from that point onwards.

A PC can be in multiple subnets at the same time. So in such case there is no "the" IP.

But it sounds you simply need to enumerate all interfaces, and then implement your match. The earlier link for finding the external IP afaik contains windows code for enumerating interfaces

I am not sure how to put the Windows code you have given in my program using Pascal though. However I might try and see if I can get the results from the procedure I am using to do this.

Thanks

TimCS

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address - new problem [SOLVED]
« Reply #11 on: April 16, 2013, 04:01:53 pm »
Got around it doing this:

Code: [Select]
if  pos('10.1.68',getIPS.text)>0 then
         pcipaddress := copy(getIPS.text,pos('10.1.68',getIPS.text),13)
       else if pos('10.1.69',getIPS.text)>0 then
         pcipaddress := copy(getIPS.text,pos('10.1.69',getIPS.text),13)
      else if pos('10.1.70',getIPS.text)>0 then
         pcipaddress := copy(getIPS.text,pos('10.1.70',getIPS.text),13);


         pcsubpos := lastdelimiter('.',pcipaddress);
         pcsubnet := copy(pcipaddress,1,pcsubpos-1) ;
         pcdbipadd := pcsubnet+'.6' ;
         PQConnection1.HostName := pcdbipadd;         


It appears to work and the three sub nets are all we have in the office


Thanks

TimCS

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12982
  • FPC developer.
Re: By Code get IP address - new problem [SOLVED AGAIN]
« Reply #12 on: April 16, 2013, 05:39:07 pm »
It will probably fail for IPs that contain e.g. 10.1.68 at the end, like 10.10.1.68

timcs

  • Full Member
  • ***
  • Posts: 213
Re: By Code get IP address - new problem [SOLVED AGAIN]
« Reply #13 on: April 16, 2013, 05:58:13 pm »
It will probably fail for IPs that contain e.g. 10.1.68 at the end, like 10.10.1.68

These do no exist so there should be no problems (fingers crossed) and anyway the sub net only starts with 10.1.68 , 10.1.69 and 10.1.70

Thanks

TimCS

 

TinyPortal © 2005-2018