代码如下,直接执行shell
TCHAR szModulePath[MAX_PATH] = {0};
GetModuleFileName(GetModuleHandle(NULL), szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
string strParameters = "firewall add allowedprogram ";
strParameters += szModulePath;
strParameters += " udpSrv2 enable";
SHELLEXECUTEINFO shellExecute = {0};
shellExecute.cbSize = sizeof SHELLEXECUTEINFO;
shellExecute.fMask = SEE_MASK_NOCLOSEPROCESS;
shellExecute.lpFile = "netsh";
shellExecute.lpParameters = strParameters.c_str();
shellExecute.nShow = SW_HIDE;
if (ShellExecuteEx(&shellExecute))
{
WaitForSingleObject(shellExecute.hProcess, 2000);
CloseHandle(shellExecute.hProcess);
}
这样在程序启动时候防火墙就不会拦截。