- C/C++ code
WCHAR Names[50]; DWORD bytes; HANDLE m_hFileHandle = CreateFile(_T("NDS0:"),0,0,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)INVALID_HANDLE_VALUE); if( m_hFileHandle == INVALID_HANDLE_VALUE ) { MessageBox(_T("获得网卡名时打开设备错误")); return; } // Get list of adapter names if (!DeviceIoControl(m_hFileHandle,IOCTL_NDIS_GET_ADAPTER_NAMES, NULL,0,Names,MAX_PATH,&bytes,NULL)) { MessageBox(_T("获得网卡名错误")); return; } DWORD len = wcslen(Names); Names[len] = 0; Names[len+1] = 0; CString strKeyName; strKeyName.Format(_T("Comm\\%s\\Parms\\TCPIP"),Names); //打开注册表对网卡IP信息对应子健进行修改 HKEY hkey; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName,0,KEY_WRITE,&hkey) != ERROR_SUCCESS) { MessageBox(_T("打开注册表错误")); return; } DWORD value; value = 0; //set EnableDHCP if(RegSetValueEx(hkey,TEXT("EnableDHCP"),0,REG_DWORD,(const unsigned char *)&value,sizeof(DWORD)) != ERROR_SUCCESS) { MessageBox(_T("关闭自动获得IP错误")); } WCHAR buffer[32]; memset(buffer,0,64); memcpy(buffer,m_strIp.GetBuffer(0),m_strIp.GetLength()*2); if(RegSetValueEx(hkey,TEXT("IpAddress"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strIp.GetLength()*2+2) != ERROR_SUCCESS) { MessageBox(TEXT("设置IP错误")); } memset(buffer,0,64); memcpy(buffer,m_strMask.GetBuffer(0),m_strMask.GetLength()*2); if(RegSetValueEx(hkey,TEXT("SubnetMask"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strMask.GetLength()*2+2) != ERROR_SUCCESS) { MessageBox(TEXT("设置子网掩码错误")); } memset(buffer,0,64); memcpy(buffer,m_strGateWay.GetBuffer(0),m_strGateWay.GetLength()*2); if(RegSetValueEx(hkey,TEXT("DefaultGateway"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strGateWay.GetLength()*2+2) != ERROR_SUCCESS) { MessageBox(TEXT("设置网关错误")); } RegFlushKey(hkey); RegCloseKey(hkey); // 重启网卡,不用机器热启动 HANDLE hNdis = CreateFile(_T("NDS0:"),0,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE) INVALID_HANDLE_VALUE); if( hNdis == INVALID_HANDLE_VALUE ) { MessageBox(_T( "重启网络驱动时打开设备错误")); return; } // Send the device command. // buf contains the name of the if (!DeviceIoControl( hNdis, IOCTL_NDIS_REBIND_ADAPTER, Names, _tcslen( Names) + sizeof( TCHAR ),NULL, 0, NULL, NULL ) ) { MessageBox(_T( "重启网络驱动错误")); } CloseHandle( hNdis );
修改IP地址成功了(方法如上所示),只是修改IP后,存在一个问题。就是改IP之前,执行recv函数时,可以进行比较大流量的通信(800k~1M/s)。但是修改IP后,就不能用recv进行大流量通信了,否则调用该函数的线程就会被迫退出,而且退出的code是0xC0000005(访问空地址时也报这个错)。这种情况下只能进行小流量通信(5K/s左右)。重新恢复系统后,又正常了。所以我觉得问题还是出在修改注册表方面,但是我又找不出问题所在,麻烦高手看看,帮我出个主意。谢谢
------解决方案--------------------
不知道使用代码修改和手动修改有什么区别,如果确实不知道问题出在哪。可以手动进行一下相关的操作,然后再进行通讯测试