?? repareipdlg.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 + -