Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DeviceIoControl()

0 views
Skip to first unread message

Aman

unread,
Sep 18, 2007, 9:14:01 AM9/18/07
to
Hi,

I am trying reassign the IPaddress without restarting the board.
I have been sucessfully able to resassign the IP(which i checked from remote
reg editor tool) but rebinding the adapter is not working for me.
The function DeviceIoControl() is returning 0. The error code i got from
GetLastError() is 0xc0010006 = DTS_E_INVALIDVARIABLEVALUE.

The code snippet is as follows:-

{
TCHAR szReg1[] = L"NE20001";
HANDLE hNdis = NULL;
HKEY hKey = HKEY_LOCAL_MACHINE;
LPCWSTR lpSubKey = (LPCWSTR)"Comm\\PCI\\E100CE1\\Parms\\TCPIP";
REGSAM samDesired = KEY_ALL_ACCESS;
HKEY hRetKey = 0;
PHKEY phkResult = &hRetKey;
LONG RetVal;
LPCWSTR lpValueName;
DWORD dwType = REG_SZ, i = 0, dwRetVal = 0;
LPDWORD lpType = &dwType;
TCHAR Data[30] ;
LPCWSTR DataIP = L"199.63.33.172";
LPCWSTR DataDG = L"199.63.33.1";
LPCWSTR DataSM = L"255.255.255.0";
DWORD dwSize = sizeof(Data);
LPDWORD lpcbData = &dwSize;
LPBYTE lpData = (BYTE*)Data;

DWORD cbData;
BOOL bRetVal = 1;
RetVal = RegOpenKeyEx(hKey, lpSubKey, 0 , samDesired, phkResult);
printf("%ld\n",RetVal);
if(ERROR_SUCCESS == RetVal && NULL != phkResult)
{
lpValueName = (LPCWSTR)"IpAddress";
RetVal = RegQueryValueEx(hRetKey, lpValueName, NULL, lpType, lpData,
lpcbData);
cbData = wcslen(DataIP) + 1;
RetVal = RegSetValueEx(hRetKey, lpValueName, 0, dwType, (BYTE*)DataIP,
sizeof(TCHAR)*cbData);
printf("%ld\n",RetVal);
if(ERROR_SUCCESS != RetVal)
return 1;
lpValueName = (LPCWSTR)"DefaultGateway";
RetVal = RegQueryValueEx(hRetKey, lpValueName, NULL, lpType, lpData,
lpcbData);
cbData = wcslen(DataDG) + 1;
RetVal = RegSetValueEx(hRetKey, lpValueName, 0, dwType, (BYTE*)DataDG,
sizeof(TCHAR)*cbData);
printf("%ld\n",RetVal);
if(ERROR_SUCCESS != RetVal)
return 1;
lpValueName = (LPCWSTR)"Subnetmask";
RetVal = RegQueryValueEx(hRetKey, lpValueName, NULL, lpType, lpData,
lpcbData);
cbData = wcslen(DataSM) + 1;
RetVal = RegSetValueEx(hRetKey, lpValueName, 0, dwType, (BYTE*)DataSM,
sizeof(TCHAR)*cbData);
printf("%ld\n",RetVal);
if(ERROR_SUCCESS != RetVal)
return 1;
}
else
return 1;

RegCloseKey (hRetKey);

hNdis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, 0, NULL);
if (hNdis != NULL)
{
cbData = wcslen(szReg1) + 1;
bRetVal = DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER,
szReg1,cbData,NULL, 0, NULL, NULL);
dwRetVal = GetLastError();
printf("%d\n",bRetVal);
if(0 == bRetVal)
return 1;
}
return 0;

Can someone please tell whats wrong?

Aman

unread,
Sep 18, 2007, 10:27:27 AM9/18/07
to
I used GetAdaptersInfo() to get the adapter name and it worked.
anyways thanks for peeping in.

<ctacke/>

unread,
Sep 18, 2007, 10:31:07 AM9/18/07
to
When you're setting the IP address, I see you sending in dwType, but I see
it set nowhere. That needs to be a MULTI_SZ, and your IP
address/gateway/DNS also needs to be double-null terminated because of that
fact.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com

"Aman" <Am...@discussions.microsoft.com> wrote in message
news:A9CF87BD-11C3-49F5...@microsoft.com...

Aman

unread,
Sep 18, 2007, 10:46:08 AM9/18/07
to
Thanks Chris,
though i got the answere already but still i clarify
dwType is returned by RegQueryValueEx()
REG_SZ is working for me instead of MULTI_SZ for IPaddress/gateway/DNS etc.
thats why i kept them without double-null terminated.
I was not using the proper adapter name which now i got using
GetAdaptersInfo().
All in all its working now but
thanks for replying.

Paul G. Tobey [eMVP]

unread,
Sep 18, 2007, 11:22:24 AM9/18/07
to
Either REG_SZ or REG_MULTI_SZ will work. The system will always make it a
multi-string, so it would be wise to copy that, in case the system becomes
less-amenable to various types being found there in the future.

Paul T.

"Aman" <Am...@discussions.microsoft.com> wrote in message

news:17EBA3BD-EA1B-46F8...@microsoft.com...

0 new messages