How to assign a static IP address to the network adapter at runtime

From: Selin Metin (nospam_at_nospam.com.tr)
Date: 07/30/04


Date: Fri, 30 Jul 2004 14:37:51 +0300

Hi all,
I'm trying to assign a static IP address to the network adapter of my cepc
at runtime. The code below runs correctly so far, but when I check the new
assigned IP address with ipconfig /all from command prompt, I see that the
changes didn't take effect. However when I open the Control Panel/Network
and Dial-up Connections/VMINI1, I see the address I assigned to the cepc.

//////////////////////////////////////////////////////////////
 CString str;
 DWORD Addr;
 TCHAR szTemp[256];
 HKEY hKey;
 LONG hRes;
 DWORD Len;
 BYTE lpBuffer;

 _tcscpy (szTemp, TEXT("Comm\\"));
 _tcscat (szTemp, (LPWSTR)(LPCTSTR)m_szAdapterName);
 _tcscat (szTemp, TEXT("\\Parms\\TcpIp"));
 hKey = NULL;
 hRes = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szTemp, 0, 0, &hKey);
 if (ERROR_SUCCESS == hRes && hKey != NULL)
 {
  lpBuffer = 0;
  RegSetValueEx (hKey, TEXT("EnableDHCP"), 0, REG_DWORD,
       &lpBuffer, sizeof(DWORD));

  GetDlgItem(IDC_IPADDRESS)->GetWindowText(str);
  Addr = StringToAddr((LPWSTR)(LPCTSTR)str);
  if (!IsValidIPAddress(Addr))
   return;
  IPAddrToStr (szTemp, Addr);
  Len = _tcslen(szTemp)+1;
  szTemp[Len++] = TEXT('\0');
  RegSetValueEx (hKey, TEXT("IpAddress"), 0, REG_MULTI_SZ,
       (BYTE *)szTemp, sizeof(TCHAR)*Len);

  GetDlgItem(IDC_SUBNETMASK)->GetWindowText(str);
  Addr = StringToAddr((LPWSTR)(LPCTSTR)str);
  if (!IsValidIPAddress(Addr))
   return;
  IPAddrToStr (szTemp, Addr);
  Len = _tcslen(szTemp)+1;
  szTemp[Len++] = TEXT('\0');
  RegSetValueEx (hKey, TEXT("Subnetmask"), 0, REG_MULTI_SZ,
       (BYTE *)szTemp, sizeof(TCHAR)*Len);

  GetDlgItem(IDC_DEFGATEWAY)->GetWindowText(str);
  if (!str.IsEmpty()) {
   Addr = StringToAddr((LPWSTR)(LPCTSTR)str);
   if (!IsValidIPAddress(Addr))
    return;
  }
  else
   Addr = 0;
  IPAddrToStr (szTemp, Addr);
  Len = _tcslen(szTemp)+1;
  szTemp[Len++] = TEXT('\0');
  RegSetValueEx (hKey, TEXT("DefaultGateway"), 0, REG_MULTI_SZ,
       (BYTE *)szTemp, sizeof(TCHAR)*Len);

  GetDlgItem(IDC_PRIMARYDNS)->GetWindowText(str);
  if (!str.IsEmpty()) {
   Addr = StringToAddr((LPWSTR)(LPCTSTR)str);
   if (!IsValidIPAddress(Addr))
    return;
  }
  else
   Addr = 0;
  IPAddrToStr (szTemp, Addr);
  Len = _tcslen(szTemp)+1;
  szTemp[Len++] = TEXT('\0');
  RegSetValueEx (hKey, TEXT("DNS"), 0, REG_MULTI_SZ,
       (BYTE *)szTemp, sizeof(TCHAR)*Len);

  GetDlgItem(IDC_PRIMARYWINS)->GetWindowText(str);
  if (!str.IsEmpty()) {
   Addr = StringToAddr((LPWSTR)(LPCTSTR)str);
   if (!IsValidIPAddress(Addr))
    return;
  }
  else
   Addr = 0;
  IPAddrToStr (szTemp, Addr);
  Len = _tcslen(szTemp)+1;
  szTemp[Len++] = TEXT('\0');
  RegSetValueEx (hKey, TEXT("WINS"), 0, REG_MULTI_SZ,
       (BYTE *)szTemp, sizeof(TCHAR)*Len);

  RegCloseKey (hKey);
 }
//////////////////////////////////////////////////////////////

I assume I have to rebind these new parameters to the network adapter, so I
call IOCTL_NDIS_REBIND_ADAPTER with the following code:

//////////////////////////////////////////////////////////////
 // Open the NDIS driver.
 HANDLE hNdis;
 if (INVALID_HANDLE_VALUE == (hNdis = CreateFile(_T("NDS0:"), GENERIC_READ |
GENERIC_WRITE,
   0, NULL, OPEN_EXISTING, 0, NULL))) {
  DWORD dwerr = GetLastError();
  return;
 }

 // Send the device command.
 TCHAR errs[ 512 ];
 DWORD xcount;
 TCHAR szDevName[256];
 LPCTSTR szAdaptName = (LPWSTR)(LPCTSTR)m_szAdapterName;
 _tcsncpy(szDevName, szAdaptName, m_szAdapterName.GetLength() + 1);
 LPVOID lpInBuffer = szDevName;
 int klk = 0;
 if (DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER, lpInBuffer,
  _tcslen(m_szAdapterName) + sizeof(TCHAR), // buf contains the name of the
adapter
   NULL, 0, &xcount, NULL ) )
 {
  OutputDebugString( _T( "Adapter rebound\r\n" ) );
 }
 else
 {
  _stprintf( errs, _T("DeviceIoControl failed. error = %d\r\n"),
    GetLastError());
  OutputDebugString(errs);
 }

 CloseHandle(hNdis);
//////////////////////////////////////////////////////////////

I always receive error when I call DeviceIoControl with these parameters. I
suspect that the adapter name I pass in parameter 3 causes a problem, but
I'm not sure. Can someone give me a hint on this please?

TIA,

--
Selin Metin
selinm_at_esit_dot_com_dot_tr


Relevant Pages