Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Connect and Obtain IP address from DHCP

I am writing an application, which get a list of all network interfaces, with their IP address, MAC address and other info.
The interfaces which are connected with internet have obviously IP addresses. For example, My WLAN interface has IP address. For other interfaces, for example if I plug in a 3G card, it gets IP address when I click the CONNECT button on the software came with this card.
I dont want to use that Software to connect my 3G card and get IP address. Rather I want to enable that interface and get IP address from DHCP server programmatically in C++.
In simplest words I want my network card / adapter should be connected and be assigned IP address using C++.
Note: I am using Windows Vista.
Waiting for reply.
1 Solution
How about calling the command line tools "netsh" and "ipconfig" shipped with Windows? You can do this from your C++ program.
Here is an example how to change the IP address assignment to DHCP (replace "MyConnection" by the name of your network - see Windows Control Panel):

netsh interface ip set address name="MyConnection" source=dhcp

To reassign the IP address via DHCP after this call:

ipconfig /renew "MyConnection"

Also the command line tool DevCon (http://support.microsoft.com/?kbid=311272) might help you to enable/disable network adapters in the Windows device manager.

Attention: You need real admin rights to do this. On Vista with UAC enabled you need to launch the command line tools with elevation (e.g via ShellExecuteEx or your application already has an according manifest and already runs with real admin rights, see http://en.wikipedia.org/wiki/User_Account_Control).

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now