void OnModify()
{
UpdateData(true);
HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭
//打开防火墙路径 data_Set相关的hKEY
LPCTSTR data_Set= "SYSTEM\\CurrentControlSet\\services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\";
//访问注册表,hKEY则保存此函数所打开的键的句柄
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_ALL_ACCESS,&hKEY));
if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
{
AfxMessageBox("错误:无法打开有关的hKEY");
return;
}
DWORD value_Set = 0x00000000;
long ret1=::RegSetValueEx(hKEY,"EnableFirewall",0,type_1,(LPBYTE)&value_Set,sizeof(DWORD));
if(ret1!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法设置有关的注册表信息");
return;
}
//程序结束,关闭打开的hKEY
::RegCloseKey(hKEY);
//win7及以上版本
CString strCommand="NetSh Advfirewall set allprofiles state off";
DealExecCmd(strCommand);
UpdateData(false);
AfxMessageBox("提示:成功设置有关的注册表信息!");
}
BOOL DealExecCmd(CString strCommandLine)
{
STARTUPINFO StartInfo;
memset(&StartInfo, '\0', sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo); //name structure
StartInfo.dwFlags = STARTF_USESHOWWINDOW;
StartInfo.wShowWindow = SW_HIDE; //隐藏DOC窗口
PROCESS_INFORMATION ProcInfo; //name structure
memset(&ProcInfo, 0, sizeof(ProcInfo));
BOOL flag = ::CreateProcess(NULL, strCommandLine.GetBuffer(0), NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo);
strCommandLine.ReleaseBuffer();
if(flag)
{
WaitForSingleObject(ProcInfo.hProcess,INFINITE);//此方法主要是等待进程处理
CloseHandle(ProcInfo.hThread);
CloseHandle(ProcInfo.hProcess);
}
return flag;
}