⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 repareipdlg.cpp

📁 wince 访问注册表,修改ip地址,网关,如果结合自己程序,放到自动启动程序,可以实现开机后自动配置正确的ip地址,就象winxp一样.
💻 CPP
字号:
// RepareIpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RepareIp.h"
#include "RepareIpDlg.h"
#include   <winioctl.h> 
#include   <ntddndis.h> 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRepareIpDlg dialog

CRepareIpDlg::CRepareIpDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRepareIpDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRepareIpDlg)
	m_strIp = _T("192.168.1.102");
	m_strServerIP = _T("192.168.1.101");	
	m_bCheckIpFile = TRUE;
	m_strGateWay = _T("192.168.1.1");
    m_strMask = _T("255.255.255.0");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRepareIpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRepareIpDlg)
	DDX_Text(pDX, IDC_EDIT_IP, m_strIp);
	DDX_Text(pDX, IDC_EDIT_SIP, m_strServerIP);
	DDX_Check(pDX, IDC_CHECK_FILE, m_bCheckIpFile);
	DDX_Text(pDX, IDC_EDIT_GATEWAY, m_strGateWay);
	DDX_Text(pDX, IDC_EDIT_MASK, m_strMask);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRepareIpDlg, CDialog)
	//{{AFX_MSG_MAP(CRepareIpDlg)
	ON_BN_CLICKED(IDC_BUTTON_IP, OnButtonIp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRepareIpDlg message handlers

BOOL CRepareIpDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CRepareIpDlg::OnButtonIp() 
{	
	//获得网卡的设备名
	if(m_bCheckIpFile)
	{
		FILE * fp;
        if ((fp = fopen("\\ResidentFlash\\IP.txt","r")) == NULL)//读取存卡中的配置文件
		{
             MessageBox(_T("打开Ip.txt文件失败"));
             return;
		}		
		int i;
        char lineValue[32];
		memset(lineValue,0,32);
		if(fgets(lineValue,32,fp))
		{
			i = 0;
			while((lineValue[i]<='9' && lineValue[i]>='0')||lineValue[i]=='.')
				i++;
			lineValue[i] = 0x00;
		    m_strIp = lineValue;
		}	

		memset(lineValue,0,32);
        if(fgets(lineValue,32,fp))
		{
			i = 0;
			while((lineValue[i]<='9' && lineValue[i]>='0')||lineValue[i]=='.')
				i++;
			lineValue[i] = 0x00;
		    m_strMask = lineValue;
		}	

		memset(lineValue,0,32);
		if(fgets(lineValue,32,fp))
		{
			i = 0;
			while((lineValue[i]<='9' && lineValue[i]>='0')||lineValue[i]=='.')
				i++;
			lineValue[i] = 0x00;
		    m_strGateWay = lineValue;
		}	
		fclose(fp);
		UpdateData(FALSE);		
	}
	UpdateData();

    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和端口存入注册表
    /*
    HKEY hOpenKey;
    DWORD dwOpenStyle;
    long lResult = 0;
    LPCTSTR keyName = L"MySoftware\\CEClient";
    lResult = RegCreateKeyEx(HKEY_CURRENT_USER,keyName,0,L"",0,0,NULL,&hOpenKey,&dwOpenStyle);
    ASSERT(lResult == ERROR_SUCCESS);
    //write host	
    lResult = RegSetValueEx(hOpenKey, L"host",0,REG_SZ,(BYTE *)m_strServerIP.GetBuffer(0),m_strServerIP.GetLength() * 2);
    ASSERT(lResult == ERROR_SUCCESS);

	//write port
	DWORD m_dwServerPort;
	m_dwServerPort = 8062;
    lResult = RegSetValueEx(hOpenKey, L"port",0,REG_SZ,(BYTE *)&m_dwServerPort,sizeof(m_dwServerPort));
    ASSERT(lResult == ERROR_SUCCESS);

    RegCloseKey(hOpenKey);
    */
	MessageBox(_T( "操作成功!")); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -