By Code get IP address - new problem [SOLVED AGAIN] lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- timcs · 2013-04-10 16:32 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): Quotefunction 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 ---------------------------------------------------------------------- ChrisF · 2013-04-10 17:32 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). ---------------------------------------------------------------------- BigChimp · 2013-04-11 05:25 If you want to get your external IP address, see e.g. this link: http://wiki.lazarus.freepascal.org/fphttpclient#Get_external_IP_addres s ... 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. ---------------------------------------------------------------------- timcs · 2013-04-11 07:13 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 · 2013-04-11 07:27 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... ---------------------------------------------------------------------- timcs · 2013-04-11 07:57 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 · 2013-04-16 10:31 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 · 2013-04-16 10:44 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 · 2013-04-16 10:55 Quote from: marcov on April 16, 2013, 12:44:50 pmIn 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 · 2013-04-16 11:16 Quote from: timcs on April 16, 2013, 12:55:51 pmQuote from: marcov on April 16, 2013, 12:44:50 pmIn 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 · 2013-04-16 11:30 Quote from: marcov on April 16, 2013, 01:16:38 pmQuote from: timcs on April 16, 2013, 12:55:51 pmQuote from: marcov on April 16, 2013, 12:44:50 pmIn 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 · 2013-04-16 14:01 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 · 2013-04-16 15:39 It will probably fail for IPs that contain e.g. 10.1.68 at the end, like 10.10.1.68 ---------------------------------------------------------------------- timcs · 2013-04-16 15:58 Quote from: marcov on April 16, 2013, 05:39:07 pmIt 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