?
Solved

How does Winipcfg get IP addresses

Posted on 1999-01-11
18
Medium Priority
?
281 Views
Last Modified: 2013-12-04
I would like some detailed workable Win32 'C' code of how to get my IP address when connected to the Internet, and I want to take into account that my computer may or may not have a network card !!!!!!!!

Just like Windows winipcfg proggy does????????
0
Comment
Question by:haast
[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
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 1418548
It's hard enough, and it's different for NT and 95.
Keep in mind that may be more than one adapter card, more than one RAS (dial-up) connections, DHCP...

It cannot be described with two or two hundred words.

I can write for you working code for 1000 pts at least.
0
 

Author Comment

by:haast
ID: 1418549
nickrepin

OK, how about setting limits on this 1 ras connection, 1 adapter card win95/98.

Can you help with that scenario ?????

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1418550
The problem is not in how many cards we have.
To obtain this info we must:

1) enum all adapters
2) enum all RAS connections
3) obtain TCP/IP and DHCP settings for them
These all must be done through registry, here is no API at all!

I have not got any code you want.
I have got some info about registry keys for NT, and have not for 95.

I can find these all, but it's all absolute free for me. Moreover, I'll waste my time. It's indeed very hard job. For me, at least. I'll do it for 1000 pts, not less.

The Qs like yours were asked here with no success (if I'm not wrong).
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:haast
ID: 1418551
No DHCP !!
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1418552
I'm sorry, but I really have no time.
The main problem is to enumerate adapters and RAS connections.
May be, other experts can help you...
0
 

Expert Comment

by:CodeMonkey
ID: 1418553
This code sample will give you a list of valid network interfaces that are available to your computer. If you have Dial up networking then your as your new connection becomes established, extra interafaces will appear in the list. You can use the RAS functions to decide if they are Dial up related connections

char szHostname[100];
if( gethostname( szHostname, sizeof( szHostname ))==0)
{
  HOSTENT* pHostEnt=gethostbyname(szHostname);
  if (pHostEnt)
  {
    int nAdapter = 0;
    while ( pHostEnt->h_addr_list[nAdapter] )
    {
      print("%s\n",inet_ntoa( *(IN_ADDR*)(pHostEnt->h_addr_list[nAdapter]));
    }
  }
}
0
 

Author Comment

by:haast
ID: 1418554
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections

0
 

Author Comment

by:haast
ID: 1418555
Codemonkey, I realise that the list of IP's in network byte order is here: pHostEnt->h_addr_list[nAdapter], but how do establish which IP is the actual current ISP dynamic IP.

(RAS connection) I have had a brief look at RASCON & RASPPPIP structs and HRASCON. As Nickrepin has suggested, I probably have to enumerate the connections with RasEnumConnections and then RasGetProjectionInfo and also he is right about NT being different to Win95/98.

So this is the partof the question that I need answered !!

thanks,


0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1418556
haast, as I know, it's need to enumerate registry entries that relates to RAS connections (their like 'fake' adapters). The addresses in these entries are changes dynamically when RAS connected/disconnected.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1418557
I can give you some info about that entries in NT, but now I have no info about 95.
0
 

Author Comment

by:haast
ID: 1418558
NickRepin could you please contact me at: westwebs@ihug.co.nz
0
 

Author Comment

by:haast
ID: 1418559
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections
0
 

Author Comment

by:haast
ID: 1418560
Codemonkey, i follow your code OK, but can you elaborate on this .....
You can use the RAS functions to decide if they are Dial up related connections
0
 
LVL 32

Accepted Solution

by:
jhance earned 800 total points
ID: 1418561
For a DIAL-UP connection, this code will get information about the currently established DUN connection.  YOUR IP address is returned in this line:

strcpy(hostip, prasip->szIpAddress);
       ^^^^^^



//
// Check the state of any RAS connection
//
char *CWindow::CheckRAS(DWORD *state, char *hostip)
{

RASCONN *ras_buf;
RASPPPIP *prasip;
DWORD sz_ras_buf, numconn, errcd, szrasip;
HRASCONN hrc;
static char rasname[256];

// Allocate space for RASCONN buffers
ras_buf = (RASCONN *)malloc(32 * sizeof(RASCONN));
sz_ras_buf = 32 * sizeof(RASCONN);

ras_buf->dwSize = sizeof(RASCONN);

if((errcd = RasEnumConnections(ras_buf, &sz_ras_buf, &numconn)) != 0){
    *state = RAS_ERROR;
    free(ras_buf);
return NULL;
}

if(numconn > 0){
hrc = ras_buf->hrasconn;

prasip = (RASPPPIP *)malloc(sizeof(RASPPPIP));
szrasip = sizeof(RASPPPIP);
prasip->dwSize = szrasip;

errcd = RasGetProjectionInfo(hrc, RASP_PppIp, prasip, &szrasip);
switch(errcd){

default:
    *state = RAS_ERROR;
    free(ras_buf);
    return NULL;
case 0:
*state = RAS_CONN;
strcpy(rasname, ras_buf->szEntryName);
strcpy(hostip, prasip->szIpAddress);
if(strlen(rasname) > 0){
    free(ras_buf);
     return(rasname);
     }
     else{
         *state = RAS_ERROR;
         free(ras_buf);
         return NULL;
     }

case 731:
    *state = RAS_DIAL;
    strcpy(rasname, ras_buf->szEntryName);
if(strlen(rasname) > 0){
    free(ras_buf);
     return(rasname);
     }
     else{
         *state = RAS_ERROR;
         free(ras_buf);
         return NULL;
     }
}
}
else{
    *state = RAS_NOCONN;
    free(ras_buf);
    return NULL;
}
}
0
 

Author Comment

by:haast
ID: 1418562
jhance, give me a bit of time and i will check it out, thanks
0
 
LVL 32

Expert Comment

by:jhance
ID: 1418563
BTW, Winipcfg does this via undocumented WINSOCK calls.  I've traced some out enough to verify that they are indeed undocumented calls.  

The only other reference I have on this subject which you might also find useful is:

http://www.caip.rutgers.edu/~arni/ws2/stas.htm

but it's techniques don't apply to DUN.

Enjoy...
0
 

Expert Comment

by:CodeMonkey
ID: 1418564
Sorry, I didn't answer but I was away
0
 

Expert Comment

by:CodeMonkey
ID: 1418565
Sorry, I didn't answer but I was away. jhance's answer seems okay
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 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