通過註冊表修改WINCE系統IP參數

緊跟《註冊表中RegSetValueEx用法》文章,自己寫了一個測試程序,用到的註冊表函數有:

RegCreateKeyEx、RegSetValueEx、RegFlushKey、RegCloseKey。

   測試程序的功能是,誰知當前WINCE設備的IP地址,包括:子網掩碼、默認網關等,並且使設置參數重啓有效

#include <ntddndis.h>
#include <Iphlpapi.h>

HINSTANCE hMainInstance=NULL;

TCHAR szMsg[256];

void Rebind_Adapter()
{
    HANDLE hDevice;
    hDevice = CreateFile(
        TEXT("NDS0:"),
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
    if (hDevice != INVALID_HANDLE_VALUE)
    {
        if(DeviceIoControl(
                       hDevice,
                       IOCTL_NDIS_REBIND_ADAPTER,
                       TEXT("CS89501/0"),
                       sizeof(TEXT("CS89501/0")),
                       NULL, 0, NULL, NULL))
  {
   RETAILMSG(1, (TEXT("Rebind NDS0 ok/r/n")));
  }
  else
  {
   RETAILMSG(1, (TEXT("Rebind NDS0 failed/r/n")));
  }

        CloseHandle(hDevice);
    }
    else
    {
  RETAILMSG(1, (TEXT("CreateFile NDS0 Failed/r/n")));
    }
}

void RegSetUStringValue(HKEY root, LPCWSTR Path, LPCWSTR Attr, LPCWSTR Value)
{
 HKEY hKey;
 DWORD nDisposition;
 if (RegCreateKeyEx (root, Path, 0, NULL, 0, 0, 0, &hKey, &nDisposition) == ERROR_SUCCESS )
 {
  RegSetValueEx(hKey, Attr, 0, REG_SZ, (BYTE*)Value, wcslen(Value)*2);
  RETAILMSG(1, (TEXT("RegSetValueEx %s %d/r/n"), Value, wcslen(Value)));
  RegFlushKey(hKey);
  RegCloseKey(hKey);
 }
}
void RegSetUintValue(HKEY  root, LPCWSTR Path, LPCWSTR Attr, DWORD Value)
{
 HKEY hKey;
 DWORD nDisposition;
 if (RegCreateKeyEx (root, Path, 0, NULL, 0, 0, 0, &hKey, &nDisposition) == ERROR_SUCCESS )
 {
        RegSetValueEx(hKey, Attr, 0, REG_DWORD, (BYTE*)(&Value), sizeof(DWORD));
        RegFlushKey(hKey);
  RegCloseKey(hKey);
 }
}

static void SetDHCP(HWND m_hWnd)
{
    RegSetUintValue(HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms//TcpIp", L"EnableDHCP", 1);
    Rebind_Adapter();
}

static void SetStaticIP(HWND m_hWnd)
{
 TCHAR szIpAddress[32];
 TCHAR szGateway[32];
 TCHAR szNetmask[32];

 memset(szIpAddress,0,sizeof(szIpAddress));
 GetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_IPADDRESS), szIpAddress, sizeof(szIpAddress));
 memset(szGateway,0,sizeof(szGateway));
 GetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_GATEWAY), szGateway, sizeof(szGateway));
 memset(szNetmask,0,sizeof(szNetmask));
 GetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_NETMASK), szNetmask, sizeof(szNetmask));

 RETAILMSG(1, (TEXT("SetStaticIP/r/nszIpAddress = %s/r/n"), szIpAddress));
 RETAILMSG(1, (TEXT("szGateway = %s/r/n"), szGateway));
 RETAILMSG(1, (TEXT("szNetmask = %s/r/n"), szNetmask));

    RegSetUStringValue(HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms//TcpIp", L"IpAddress", szIpAddress);
    RegSetUStringValue(HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms//TcpIp", L"Subnetmask", szNetmask);
    RegSetUStringValue(HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms//TcpIp", L"DefaultGateway", szGateway);
    RegSetUintValue(HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms//TcpIp", L"EnableDHCP", 0);
    Rebind_Adapter();

}

void GetConfig(HWND m_hWnd)
{
 int ret;
    IP_ADAPTER_INFO* pAdapterInfo = NULL;
    DWORD pOutBufLen = 0;
 TCHAR szIpAddress[32];
 TCHAR szGateway[32];
 TCHAR szNetmask[32];
 TCHAR szMacAddress[32];

    HKEY    hKey;
    DWORD  dwType;
    DWORD  dwCbData;
    DWORD   dwValue[3];
   
    ret = GetAdaptersInfo(pAdapterInfo, &pOutBufLen);
   
    pAdapterInfo = (IP_ADAPTER_INFO*)malloc(pOutBufLen);
    ret = GetAdaptersInfo(pAdapterInfo, &pOutBufLen);

 memset(szIpAddress,0,sizeof(szIpAddress));
 MultiByteToWideChar (CP_ACP, 0, pAdapterInfo->IpAddressList.IpAddress.String, strlen(pAdapterInfo->IpAddressList.IpAddress.String), szIpAddress, sizeof(szIpAddress));
 memset(szNetmask,0,sizeof(szNetmask));
 MultiByteToWideChar (CP_ACP, 0, pAdapterInfo->IpAddressList.IpMask.String, strlen(pAdapterInfo->IpAddressList.IpMask.String), szNetmask, sizeof(szNetmask));
 memset(szGateway,0,sizeof(szGateway));
 MultiByteToWideChar (CP_ACP, 0, pAdapterInfo->GatewayList.IpAddress.String, strlen(pAdapterInfo->GatewayList.IpAddress.String), szGateway, sizeof(szGateway));
 
 RETAILMSG(1, (TEXT("szIpAddress = %s/r/n"), szIpAddress));
 RETAILMSG(1, (TEXT("szGateway = %s/r/n"), szGateway));
 RETAILMSG(1, (TEXT("szNetmask = %s/r/n"), szNetmask));
 SetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_IPADDRESS), szIpAddress);
 SetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_GATEWAY), szGateway);
 SetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_NETMASK), szNetmask);

 

    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms", 0, 0, &hKey) == ERROR_SUCCESS)
    {
     dwType = REG_DWORD;
     dwCbData = sizeof(DWORD);
  RegQueryValueEx(hKey, L"MACAddress1", 0, &dwType, (LPBYTE)&dwValue[0], &dwCbData);
  RegQueryValueEx(hKey, L"MACAddress2", 0, &dwType, (LPBYTE)&dwValue[1], &dwCbData);
  RegQueryValueEx(hKey, L"MACAddress3", 0, &dwType, (LPBYTE)&dwValue[2], &dwCbData);
     _stprintf(szMacAddress, TEXT("%02x:%02x:%02x:%02x:%02x:%02x"),
         dwValue[0]&0xFF, (dwValue[0]>>8)&0xFF,
         dwValue[1]&0xFF, (dwValue[1]>>8)&0xFF,
         dwValue[2]&0xFF, (dwValue[2]>>8)&0xFF);
  SetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_MACADDRESS), szMacAddress);
  RegFlushKey(hKey);
     RegCloseKey( hKey);
    }

}

void SaveMAC(HWND m_hWnd)
{
    HKEY    hKey;
 TCHAR szMacAddress[32];
    DWORD   dwValue[6] = {0};
    DWORD  dwMacValue;

 memset(szMacAddress,0,sizeof(szMacAddress));
 GetWindowText(GetDlgItem(m_hWnd, IDC_EDIT_MACADDRESS), szMacAddress, sizeof(szMacAddress));
 
 _stscanf(szMacAddress, TEXT("%02x:%02x:%02x:%02x:%02x:%02x"),
  &dwValue[0],&dwValue[1],&dwValue[2],
  &dwValue[3],&dwValue[4],&dwValue[5]);
  

    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"Comm//CS89501//Parms", 0, 0, &hKey) == ERROR_SUCCESS)
    {
  dwMacValue = (dwValue[1]<<8) + dwValue[0];
  RegSetValueEx(hKey, L"MACAddress1", 0, REG_DWORD, (LPBYTE)&dwMacValue, sizeof(DWORD));
  dwMacValue = (dwValue[3]<<8) + dwValue[2];
  RegSetValueEx(hKey, L"MACAddress2", 0, REG_DWORD, (LPBYTE)&dwMacValue, sizeof(DWORD));
  dwMacValue = (dwValue[5]<<8) + dwValue[4];
  RegSetValueEx(hKey, L"MACAddress3", 0, REG_DWORD, (LPBYTE)&dwMacValue, sizeof(DWORD));
  RegFlushKey(hKey);
     RegCloseKey(hKey);
     _stprintf(szMsg, TEXT("Save MAC Address/r/n%02x:%02x:%02x:%02x:%02x:%02x/r/n/r/nNeed to restart device./r/n"),
          dwValue[0]&0xFF, (dwValue[1])&0xFF,
          dwValue[2]&0xFF, (dwValue[3])&0xFF,
          dwValue[4]&0xFF, (dwValue[5])&0xFF);
 }
}

 

以下是相應設備原始註冊表相關信息:

[HKEY_LOCAL_MACHINE/Comm/CS8950]
   "DisplayName"="EP93xx Ethernet Driver"
   "Group"="NDIS"
   "ImagePath"="cs8950.dll"

[HKEY_LOCAL_MACHINE/Comm/CS8950/Linkage]
   "Route"=multi_sz:"CS89501"

[HKEY_LOCAL_MACHINE/Comm/CS89501]
   "DisplayName"="EP9312 Ethernet Driver"
   "Group"="NDIS"
   "ImagePath"="cs8950.dll"

[HKEY_LOCAL_MACHINE/Comm/CS89501/Parms]
   "BusNumber"=dword:0
   "BusType"=dword:0
;   MACAddress: OverRide MAC Address stored in EEPROM.
;   The 1st (the least) DWord (32bits) of the MAC Address
   "MACAddress1"=dword:0012
;   The 2nd  DWord of the MAC Address
   "MACAddress2"=dword:3456
;   The 3rd (the most) DWord of the MAC Address
   "MACAddress3"=dword:7890

;   DuplexMode: 0:AutoNegotiate, 1:Half, 2:Full (Default:0)
   "DuplexMode"=dword:0
;   MediaSpeed: 0:AutoSense, 10:10_MBPS, 100:100_MBPS (Default:0)
   "MediaSpeed"=dword:0

[HKEY_LOCAL_MACHINE/Comm/CS89501/Parms/TcpIp]
   "DefaultGateway"="192.168.0.1"
   "DontAddDefaultGateway"=dword:0
   "UseZeroBroadcast"=dword:0
   "IpAddress"="192.168.0.18"
   "Subnetmask"="255.255.255.0"
   "EnableDHCP"=dword:0

[HKEY_LOCAL_MACHINE/Comm/Tcpip/Linkage]
   ; This should be MULTI_SZ
   ; This is the list of llip drivers to load
   "Bind"=multi_sz:"ppp","CS89501"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章