Shryeash Patel said:
I wanted to know how I can switch between the "Obtain an
IP address from a DHCP Server" and "Specify an IP
Address". In addition, how I can assign a static IP to the
my network adapter.
You can do this through the Registry using the
Microsoft.Win32.Registry and Microsoft.Win32.RegistryKey namespaces in
..NET.
First, you need to determine your network card name. This can be
found in the Registry under the HKEY_LOCAL_MACHINE (HKLM) key:
HKLM\Software\Microsoft\Windows NT\CurrentVersion\NetworkCards
Each network card on the machine is listed in a separate subkey.
Within the subkey you will see data values named Description and
ServiceName.
The ServiceName data value is then used to reference the network
card IP information in other keys. The information is stored in two
locations:
HKLM\SYSTEM\CurrentControlSet\Services\servicename, and
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\servicename
(where servicename is the network card ServiceName value).
These keys contain the IP data values used for the network card.
The EnableDHCP data value will be set to 1 if DHCP is enabled for that
interface. You can change it to 0 to disable DHCP. The IPAddress,
SubnetMask, and DefaultGateway values can then be set with the
approptiate static information. Be careful though, as these three
values are string arrays (a null-terminated list of null-terminated
strings). Once the values are changed, they will take effect the next
time the network card is enabled, or if the machine is rebooted.
Hope this gets you started. I show an example of querying the IP
address info from the Registry in chapter 2 of my "C# Network
Programming" book. The sample code can be freely downloaded from the
Sybex web site (obviously it is in C#, but it should not be too
difficult to convert to C++.NET). Good luck!
Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html