在Windows VISTA上,以下代码不起作用,它只是一个Microsoft示例...... :-( [英] On Windows VISTA , the following code doesn't work , it's a Microsoft Sample only... :-(
问题描述
在此发布此问题,因为我没有得到以下帖子的任何回复。请让我知道什么 以下代码有误?
/ Firewall.cpp:定义控制台应用程序的入口点。
//
#include" stdafx.h"
/ *
版权所有(c)Microsoft Corporation
概要
Windows防火墙COM接口的示例代码。
* /
#include< windows.h>
#include< crtdbg.h>
#include< netfw.h>
#include< objbase.h>
#include< oleauto.h>
#include< stdio.h>
#pragma comment(lib," ole32.lib")
#pragma comment(lib," oleaut32.lib")
HRESULT WindowsFirewallInitialize(OUT INetFwProfile ** fwProfile)
{
HRESULT hr = S_OK;
INetFwMgr * fwMgr = NULL;
INetFwPolicy * fwPolicy = NULL;
_ASSERT(fwProfile!= NULL);
* fwProfile = NULL;
//创建防火墙设置管理器的实例。
hr = CoCreateInstance(
__uuidof(NetFwMgr),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwMgr),
(void **)& fwMgr
);
if(FAILED(hr))
{
printf(" CoCreateInstance failed:0x%08lx \ n",hr);
转到错误;
}
//检索本地防火墙策略。
hr = fwMgr-> get_LocalPolicy(& fwPolicy);
if(FAILED(hr))
{
printf(" get_LocalPolicy failed:0x%08lx \ n",hr);
转到错误;
}
//检索当前有效的防火墙配置文件。
hr = fwPolicy-> get_CurrentProfile(fwProfile);
if(FAILED(hr))
{
printf(" get_CurrentProfile failed:0x%08lx \ n",hr);
转到错误;
}
错误:
//释放本地防火墙策略。
if(fwPolicy!= NULL)
{
fwPolicy-> Release();
}
//释放防火墙设置管理器。
if(fwMgr!= NULL)
{
fwMgr-> Release();
}
返回小时;
}
无效WindowsFirewallCleanup(IN INetFwProfile * fwProfile)
{
//释放防火墙配置文件。
if(fwProfile!= NULL)
{
fwProfile-> Release();
}
}
HRESULT WindowsFirewallIsOn(IN INetFwProfile * fwProfile,OUT BOOL * fwOn)
{
HRESULT hr = S_OK;
VARIANT_BOOL fwEnabled;
_ASSERT(fwProfile!= NULL);
_ASSERT(fwOn!= NULL);
* fwOn = FALSE;
//获取防火墙的当前状态。
hr = fwProfile-> get_FirewallEnabled(& fwEnabled);
if(FAILED(hr))
{
printf(" get_FirewallEnabled failed:0x%08lx \ n",hr);
转到错误;
}
//检查防火墙是否打开。
if(fwEnabled!= VARIANT_FALSE)
{
* fwOn = TRUE;
printf("防火墙已开启。\ n");
}
其他
{
printf("防火墙已关闭。\ n");
}
错误:
返回hr;
}
HRESULT WindowsFirewallTurnOn(IN INetFwProfile * fwProfile)
{
HRESULT hr = S_OK;
BOOL fwOn;
_ASSERT(fwProfile!= NULL);
//检查防火墙是否关闭。
hr = WindowsFirewallIsOn(fwProfile,& fwOn);
if(FAILED(hr))
{
printf(" WindowsFirewallIsOn failed:0x%08lx \ n",hr);
转到错误;
}
//如果是,请将其打开。
if(!fwOn)
{
//打开防火墙。
hr = fwProfile-> put_FirewallEnabled(VARIANT_TRUE);
if(FAILED(hr))
{
printf(" put_FirewallEnabled failed:0x%08lx \ n",hr);
转到错误;
}
printf("防火墙现在开启。\ n";);
}
错误:
返回hr;
}
HRESULT WindowsFirewallTurnOff(IN INetFwProfile * fwProfile)
{
HRESULT hr = S_OK;
BOOL fwOn;
_ASSERT(fwProfile!= NULL);
//检查防火墙是否打开。
hr = WindowsFirewallIsOn(fwProfile,& fwOn);
if(FAILED(hr))
{
printf(" WindowsFirewallIsOn failed:0x%08lx \ n",hr);
转到错误;
}
//如果是,请将其关闭。
if(fwOn)
{
//关闭防火墙。
hr = fwProfile-> put_FirewallEnabled(VARIANT_FALSE);
if(FAILED(hr))
{
printf(" put_FirewallEnabled failed:0x%08lx \ n",hr);
转到错误;
}
printf("防火墙现已关闭。\ n";);
}
错误:
返回hr;
}
HRESULT WindowsFirewallAppIsEnabled(
IN INetFwProfile * fwProfile,
IN const wchar_t * fwProcessImageFileName,
OUT BOOL * fwAppEnabled
)
{
HRESULT hr = S_OK;
BSTR fwBstrProcessImageFileName = NULL;
VARIANT_BOOL fwEnabled;
INetFwAuthorizedApplication * fwApp = NULL;
INetFwAuthorizedApplications * fwApps = NULL;
_ASSERT(fwProfile!= NULL);
_ASSERT(fwProcessImageFileName!= NULL);
_ASSERT(fwAppEnabled!= NULL);
* fwAppEnabled = FALSE;
//检索授权的应用程序集合。
hr = fwProfile-> get_AuthorizedApplications(& fwApps);
if(FAILED(hr))
{
printf(" get_AuthorizedApplications failed:0x%08lx \ n",hr);
转到错误;
}
//为过程映像文件名分配BSTR。
fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
if(fwBstrProcessImageFileName == NULL)
{
hr = E_OUTOFMEMORY;
printf(" SysAllocString failed:0x%08lx \ n",hr);
转到错误;
}
//尝试检索授权的应用程序。
hr = fwApps-> Item(fwBstrProcessImageFileName,& fwApp);
if(SUCCEEDED(hr))
{
//查看是否启用了授权的应用程序。
hr = fwApp-> get_Enabled(& fwEnabled);
if(FAILED(hr))
{
printf(" get_Enabled failed:0x%08lx \ n",hr);
转到错误;
}
if(fwEnabled!= VARIANT_FALSE)
{
//已启用授权的应用程序。
* fwAppEnabled = TRUE;
printf(
"授权应用程序%lS在防火墙中启用。\ n",
fwProcessImageFileName
);
}
其他
{
printf(
"授权应用程序%lS在防火墙中被禁用。\ n",
fwProcessImageFileName
);
}
}
else
{
//授权的应用程序不在集合中。
hr = S_OK;
printf(
"授权应用程序%lS在firewall.\ n中被禁用,
fwProcessImageFileName
);
}
错误:
//释放BSTR。
SysFreeString(fwBstrProcessImageFileName);
//释放授权的应用程序实例。
if(fwApp!= NULL)
{
fwApp-> Release();
}
//发布授权的应用程序集合。
if(fwApps!= NULL)
{
fwApps-> Release();
}
返回小时;
}
HRESULT WindowsFirewallAddApp(
IN INetFwProfile * fwProfile,
IN const wchar_t * fwProcessImageFileName,
IN const wchar_t * fwName
)
{
HRESULT hr = S_OK;
BOOL fwAppEnabled;
BSTR fwBstrName = NULL;
BSTR fwBstrProcessImageFileName = NULL;
INetFwAuthorizedApplication * fwApp = NULL;
INetFwAuthorizedApplications * fwApps = NULL;
_ASSERT(fwProfile!= NULL);
_ASSERT(fwProcessImageFileName!= NULL);
_ASSERT(fwName!= NULL);
//首先检查申请是否已获得授权。
hr = WindowsFirewallAppIsEnabled(
fwProfile,
fwProcessImageFileName,
& fwAppEnabled
);
if(FAILED(hr))
{
printf(" WindowsFirewallAppIsEnabled failed:0x%08lx \ n",hr);
转到错误;
}
//仅在尚未授权的情况下添加应用程序。
if(!fwAppEnabled)
{
//检索授权的应用程序集合。
hr = fwProfile-> get_AuthorizedApplications(& fwApps);
if(FAILED(hr))
{
printf(" get_AuthorizedApplications failed:0x%08lx \ n",hr);
转到错误;
}
//创建授权应用程序的实例。
hr = CoCreateInstance(
__uuidof(NetFwAuthorizedApplication),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwAuthorizedApplication),
(void **)& fwApp
);
if(FAILED(hr))
{
printf(" CoCreateInstance failed:0x%08lx \ n",hr);
转到错误;
}
//为过程映像文件名分配BSTR。
fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
if(fwBstrProcessImageFileName == NULL)
{
hr = E_OUTOFMEMORY;
printf(" SysAllocString failed:0x%08lx \ n",hr);
转到错误;
}
//设置过程映像文件名。
hr = fwApp-> put_ProcessImageFileName(fwBstrProcessImageFileName);
if(FAILED(hr))
{
printf(" put_ProcessImageFileName failed:0x%08lx \ n",hr);
转到错误;
}
//为应用程序友好名称分配BSTR。
fwBstrName = SysAllocString(fwName);
if(SysStringLen(fwBstrName)== 0)
{
hr = E_OUTOFMEMORY;
printf(" SysAllocString failed:0x%08lx \ n",hr);
转到错误;
}
//设置应用程序友好名称。
hr = fwApp-> put_Name(fwBstrName);
if(FAILED(hr))
{
printf(" put_Name failed:0x%08lx \ n",hr);
转到错误;
}
//将应用程序添加到集合中。
hr = fwApps->添加(fwApp);
if(FAILED(hr))
{
printf(" Add failed:0x%08lx \ n",hr);
转到错误;
}
printf(
"授权应用程序%lS现在在防火墙中启用。\ n",
fwProcessImageFileName
);
}
错误:
//释放BSTR。
SysFreeString(fwBstrName);
SysFreeString(fwBstrProcessImageFileName);
//释放授权的应用程序实例。
if(fwApp!= NULL)
{
fwApp-> Release();
}
//发布授权的应用程序集合。
if(fwApps!= NULL)
{
fwApps-> Release();
}
返回小时;
}
HRESULT WindowsFirewallPortIsEnabled(
IN INetFwProfile * fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
OUT BOOL * fwPortEnabled
)
{
HRESULT hr = S_OK;
VARIANT_BOOL fwEnabled;
INetFwOpenPort * fwOpenPort = NULL;
INetFwOpenPorts * fwOpenPorts = NULL;
_ASSERT(fwProfile!= NULL);
_ASSERT(fwPortEnabled!= NULL);
* fwPortEnabled = FALSE;
//检索全局开放端口集合。
hr = fwProfile-> get_GloballyOpenPorts(& fwOpenPorts);
if(FAILED(hr))
{
printf(" get_GloballyOpenPorts failed:0x%08lx \ n",hr);
转到错误;
}
//尝试检索全局开放端口。
hr = fwOpenPorts-> Item(portNumber,ipProtocol,& fwOpenPort);
if(SUCCEEDED(hr))
{
//查看全局开放端口是否已启用。
hr = fwOpenPort-> get_Enabled(& fwEnabled);
if(FAILED(hr))
{
printf(" get_Enabled failed:0x%08lx \ n",hr);
转到错误;
}
if(fwEnabled!= VARIANT_FALSE)
{
//启用全局开放端口。
* fwPortEnabled = TRUE;
printf("防火墙端口%ld已在防火墙中打开。\ n",portNumber);
}
else
{
printf(" Port%ld未在防火墙中打开。\ n",portNumber);
}
}
else
{
//全局开放端口不在集合中。
hr = S_OK;
printf("防火墙端口%ld未在防火墙中打开。\ n",portNumber);
}
错误:
//释放全局开放端口。
if(fwOpenPort!= NULL)
{
fwOpenPort-> Release();
}
//释放全局开放端口集合。
if(fwOpenPorts!= NULL)
{
fwOpenPorts-> Release();
}
返回小时;
}
HRESULT WindowsFirewallPortAdd(
IN INetFwProfile * fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
IN const wchar_t * name
)
{
HRESULT hr = S_OK;
BOOL fwPortEnabled;
BSTR fwBstrName = NULL;
INetFwOpenPort * fwOpenPort = NULL;
INetFwOpenPorts * fwOpenPorts = NULL;
_ASSERT(fwProfile!= NULL);
_ASSERT(name!= NULL);
//首先检查端口是否已添加。
hr = WindowsFirewallPortIsEnabled(
fwProfile,
portNumber,
ipProtocol,
& fwPortEnabled
);
if(FAILED(hr))
{
printf(" WindowsFirewallPortIsEnabled failed:0x%08lx \ n",hr);
转到错误;
}
//仅添加端口(如果尚未添加)。
if(!fwPortEnabled)
{
//检索全局开放端口的集合。
hr = fwProfile-> get_GloballyOpenPorts(& fwOpenPorts);
if(FAILED(hr))
{
printf(" get_GloballyOpenPorts failed:0x%08lx \ n",hr);
转到错误;
}
//创建一个开放端口的实例。
hr = CoCreateInstance(
__uuidof(NetFwOpenPort),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwOpenPort),
(void **)& fwOpenPort
);
if(FAILED(hr))
{
printf(" CoCreateInstance failed:0x%08lx \ n",hr);
转到错误;
}
//设置端口号。
hr = fwOpenPort-> put_Port(portNumber);
if(FAILED(hr))
{
printf(" put_Port failed:0x%08lx \ n",hr);
转到错误;
}
//设置IP协议。
hr = fwOpenPort-> put_Protocol(ipProtocol);
if(FAILED(hr))
{
printf(" put_Protocol failed:0x%08lx \ n",hr);
转到错误;
}
//为端口的友好名称分配BSTR。
fwBstrName = SysAllocString(name);
if(SysStringLen(fwBstrName)== 0)
{
hr = E_OUTOFMEMORY;
printf(" SysAllocString failed:0x%08lx \ n",hr);
转到错误;
}
//设置端口的友好名称。
hr = fwOpenPort-> put_Name(fwBstrName);
if(FAILED(hr))
{
printf(" put_Name failed:0x%08lx \ n",hr);
转到错误;
}
//打开端口并将其添加到集合中。
hr = fwOpenPorts->添加(fwOpenPort);
if(FAILED(hr))
{
printf(" Add failed:0x%08lx \ n",hr);
转到错误;
}
printf("端口%ld现在在防火墙中打开。\ n",portNumber);
}
错误:
//释放BSTR。
SysFreeString(fwBstrName);
//释放开放端口实例。
if(fwOpenPort!= NULL)
{
fwOpenPort-> Release();
}
//释放全局开放端口集合。
if(fwOpenPorts!= NULL)
{
fwOpenPorts-> Release();
}
返回小时;
}
int _tmain(int argc,_TCHAR * argv [])
{
HRESULT hr = S_OK ;
HRESULT comInit = E_FAIL;
INetFwProfile * fwProfile = NULL;
//初始化COM。
comInit = CoInitializeEx(
0,
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE
);
//忽略RPC_E_CHANGED_MODE;这只是意味着COM已经使用不同的模式初始化了
//。由于我们不关心模式是什么,
//我们只使用现有模式。
if(comInit!= RPC_E_CHANGED_MODE)
{
hr = comInit;
if(FAILED(hr))
{
printf(" CoInitializeEx failed:0x%08lx \ n",hr);
转到错误;
}
}
//检索当前有效的防火墙配置文件。
hr = WindowsFirewallInitialize(& fwProfile);
if(FAILED(hr))
{
printf(" WindowsFirewallInitialize failed:0x%08lx \ n",hr);
转到错误;
}
wchar_t * filename = L" C:\\Program Files \\ SERVER.EXE" ;;
wchar_t * friendlyfilename = L" SERVER" ;;
WindowsFirewallAddApp(fwProfile,filename,friendlyfilename);
错误:
返回0;
}
我试图运行上面的示例,尝试将我的Server.exe添加到Windows防火墙例外列表。
程序说,它成功添加了例外列表。
但是当我运行Server.exe应用程序时,我仍然获取"Windows防火墙已阻止此程序的某些功能。"
<<< http://social.msdn .microsoft.com /论坛/ zh-CN / windowsgeneraldevelopmentissues / thread / ff42225b-3f4a-4e06-90fd-4d923917924f />>
非常感谢。
您好,
感谢您的提问。
我
目前正在调查此问题,并会给你一个尽快更新。
感谢
您的理解和支持。
YI
Posting this question over here , as i didn't get any response to the below post . Please let me know what is wrong in the below code ?
/ Firewall.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*
Copyright (c) Microsoft Corporation
SYNOPSIS
Sample code for the Windows Firewall COM interface.
*/
#include <windows.h>
#include <crtdbg.h>
#include <netfw.h>
#include <objbase.h>
#include <oleauto.h>
#include <stdio.h>
#pragma comment( lib, "ole32.lib" )
#pragma comment( lib, "oleaut32.lib" )
HRESULT WindowsFirewallInitialize(OUT INetFwProfile** fwProfile)
{
HRESULT hr = S_OK;
INetFwMgr* fwMgr = NULL;
INetFwPolicy* fwPolicy = NULL;
_ASSERT(fwProfile != NULL);
*fwProfile = NULL;
// Create an instance of the firewall settings manager.
hr = CoCreateInstance(
__uuidof(NetFwMgr),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwMgr),
(void**)&fwMgr
);
if (FAILED(hr))
{
printf("CoCreateInstance failed: 0x%08lx\n", hr);
goto error;
}
// Retrieve the local firewall policy.
hr = fwMgr->get_LocalPolicy(&fwPolicy);
if (FAILED(hr))
{
printf("get_LocalPolicy failed: 0x%08lx\n", hr);
goto error;
}
// Retrieve the firewall profile currently in effect.
hr = fwPolicy->get_CurrentProfile(fwProfile);
if (FAILED(hr))
{
printf("get_CurrentProfile failed: 0x%08lx\n", hr);
goto error;
}
error:
// Release the local firewall policy.
if (fwPolicy != NULL)
{
fwPolicy->Release();
}
// Release the firewall settings manager.
if (fwMgr != NULL)
{
fwMgr->Release();
}
return hr;
}
void WindowsFirewallCleanup(IN INetFwProfile* fwProfile)
{
// Release the firewall profile.
if (fwProfile != NULL)
{
fwProfile->Release();
}
}
HRESULT WindowsFirewallIsOn(IN INetFwProfile* fwProfile, OUT BOOL* fwOn)
{
HRESULT hr = S_OK;
VARIANT_BOOL fwEnabled;
_ASSERT(fwProfile != NULL);
_ASSERT(fwOn != NULL);
*fwOn = FALSE;
// Get the current state of the firewall.
hr = fwProfile->get_FirewallEnabled(&fwEnabled);
if (FAILED(hr))
{
printf("get_FirewallEnabled failed: 0x%08lx\n", hr);
goto error;
}
// Check to see if the firewall is on.
if (fwEnabled != VARIANT_FALSE)
{
*fwOn = TRUE;
printf("The firewall is on.\n");
}
else
{
printf("The firewall is off.\n");
}
error:
return hr;
}
HRESULT WindowsFirewallTurnOn(IN INetFwProfile* fwProfile)
{
HRESULT hr = S_OK;
BOOL fwOn;
_ASSERT(fwProfile != NULL);
// Check to see if the firewall is off.
hr = WindowsFirewallIsOn(fwProfile, &fwOn);
if (FAILED(hr))
{
printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr);
goto error;
}
// If it is, turn it on.
if (!fwOn)
{
// Turn the firewall on.
hr = fwProfile->put_FirewallEnabled(VARIANT_TRUE);
if (FAILED(hr))
{
printf("put_FirewallEnabled failed: 0x%08lx\n", hr);
goto error;
}
printf("The firewall is now on.\n");
}
error:
return hr;
}
HRESULT WindowsFirewallTurnOff(IN INetFwProfile* fwProfile)
{
HRESULT hr = S_OK;
BOOL fwOn;
_ASSERT(fwProfile != NULL);
// Check to see if the firewall is on.
hr = WindowsFirewallIsOn(fwProfile, &fwOn);
if (FAILED(hr))
{
printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr);
goto error;
}
// If it is, turn it off.
if (fwOn)
{
// Turn the firewall off.
hr = fwProfile->put_FirewallEnabled(VARIANT_FALSE);
if (FAILED(hr))
{
printf("put_FirewallEnabled failed: 0x%08lx\n", hr);
goto error;
}
printf("The firewall is now off.\n");
}
error:
return hr;
}
HRESULT WindowsFirewallAppIsEnabled(
IN INetFwProfile* fwProfile,
IN const wchar_t* fwProcessImageFileName,
OUT BOOL* fwAppEnabled
)
{
HRESULT hr = S_OK;
BSTR fwBstrProcessImageFileName = NULL;
VARIANT_BOOL fwEnabled;
INetFwAuthorizedApplication* fwApp = NULL;
INetFwAuthorizedApplications* fwApps = NULL;
_ASSERT(fwProfile != NULL);
_ASSERT(fwProcessImageFileName != NULL);
_ASSERT(fwAppEnabled != NULL);
*fwAppEnabled = FALSE;
// Retrieve the authorized application collection.
hr = fwProfile->get_AuthorizedApplications(&fwApps);
if (FAILED(hr))
{
printf("get_AuthorizedApplications failed: 0x%08lx\n", hr);
goto error;
}
// Allocate a BSTR for the process image file name.
fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
if (fwBstrProcessImageFileName == NULL)
{
hr = E_OUTOFMEMORY;
printf("SysAllocString failed: 0x%08lx\n", hr);
goto error;
}
// Attempt to retrieve the authorized application.
hr = fwApps->Item(fwBstrProcessImageFileName, &fwApp);
if (SUCCEEDED(hr))
{
// Find out if the authorized application is enabled.
hr = fwApp->get_Enabled(&fwEnabled);
if (FAILED(hr))
{
printf("get_Enabled failed: 0x%08lx\n", hr);
goto error;
}
if (fwEnabled != VARIANT_FALSE)
{
// The authorized application is enabled.
*fwAppEnabled = TRUE;
printf(
"Authorized application %lS is enabled in the firewall.\n",
fwProcessImageFileName
);
}
else
{
printf(
"Authorized application %lS is disabled in the firewall.\n",
fwProcessImageFileName
);
}
}
else
{
// The authorized application was not in the collection.
hr = S_OK;
printf(
"Authorized application %lS is disabled in the firewall.\n",
fwProcessImageFileName
);
}
error:
// Free the BSTR.
SysFreeString(fwBstrProcessImageFileName);
// Release the authorized application instance.
if (fwApp != NULL)
{
fwApp->Release();
}
// Release the authorized application collection.
if (fwApps != NULL)
{
fwApps->Release();
}
return hr;
}
HRESULT WindowsFirewallAddApp(
IN INetFwProfile* fwProfile,
IN const wchar_t* fwProcessImageFileName,
IN const wchar_t* fwName
)
{
HRESULT hr = S_OK;
BOOL fwAppEnabled;
BSTR fwBstrName = NULL;
BSTR fwBstrProcessImageFileName = NULL;
INetFwAuthorizedApplication* fwApp = NULL;
INetFwAuthorizedApplications* fwApps = NULL;
_ASSERT(fwProfile != NULL);
_ASSERT(fwProcessImageFileName != NULL);
_ASSERT(fwName != NULL);
// First check to see if the application is already authorized.
hr = WindowsFirewallAppIsEnabled(
fwProfile,
fwProcessImageFileName,
&fwAppEnabled
);
if (FAILED(hr))
{
printf("WindowsFirewallAppIsEnabled failed: 0x%08lx\n", hr);
goto error;
}
// Only add the application if it isn't already authorized.
if (!fwAppEnabled)
{
// Retrieve the authorized application collection.
hr = fwProfile->get_AuthorizedApplications(&fwApps);
if (FAILED(hr))
{
printf("get_AuthorizedApplications failed: 0x%08lx\n", hr);
goto error;
}
// Create an instance of an authorized application.
hr = CoCreateInstance(
__uuidof(NetFwAuthorizedApplication),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwAuthorizedApplication),
(void**)&fwApp
);
if (FAILED(hr))
{
printf("CoCreateInstance failed: 0x%08lx\n", hr);
goto error;
}
// Allocate a BSTR for the process image file name.
fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
if (fwBstrProcessImageFileName == NULL)
{
hr = E_OUTOFMEMORY;
printf("SysAllocString failed: 0x%08lx\n", hr);
goto error;
}
// Set the process image file name.
hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName);
if (FAILED(hr))
{
printf("put_ProcessImageFileName failed: 0x%08lx\n", hr);
goto error;
}
// Allocate a BSTR for the application friendly name.
fwBstrName = SysAllocString(fwName);
if (SysStringLen(fwBstrName) == 0)
{
hr = E_OUTOFMEMORY;
printf("SysAllocString failed: 0x%08lx\n", hr);
goto error;
}
// Set the application friendly name.
hr = fwApp->put_Name(fwBstrName);
if (FAILED(hr))
{
printf("put_Name failed: 0x%08lx\n", hr);
goto error;
}
// Add the application to the collection.
hr = fwApps->Add(fwApp);
if (FAILED(hr))
{
printf("Add failed: 0x%08lx\n", hr);
goto error;
}
printf(
"Authorized application %lS is now enabled in the firewall.\n",
fwProcessImageFileName
);
}
error:
// Free the BSTRs.
SysFreeString(fwBstrName);
SysFreeString(fwBstrProcessImageFileName);
// Release the authorized application instance.
if (fwApp != NULL)
{
fwApp->Release();
}
// Release the authorized application collection.
if (fwApps != NULL)
{
fwApps->Release();
}
return hr;
}
HRESULT WindowsFirewallPortIsEnabled(
IN INetFwProfile* fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
OUT BOOL* fwPortEnabled
)
{
HRESULT hr = S_OK;
VARIANT_BOOL fwEnabled;
INetFwOpenPort* fwOpenPort = NULL;
INetFwOpenPorts* fwOpenPorts = NULL;
_ASSERT(fwProfile != NULL);
_ASSERT(fwPortEnabled != NULL);
*fwPortEnabled = FALSE;
// Retrieve the globally open ports collection.
hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
if (FAILED(hr))
{
printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr);
goto error;
}
// Attempt to retrieve the globally open port.
hr = fwOpenPorts->Item(portNumber, ipProtocol, &fwOpenPort);
if (SUCCEEDED(hr))
{
// Find out if the globally open port is enabled.
hr = fwOpenPort->get_Enabled(&fwEnabled);
if (FAILED(hr))
{
printf("get_Enabled failed: 0x%08lx\n", hr);
goto error;
}
if (fwEnabled != VARIANT_FALSE)
{
// The globally open port is enabled.
*fwPortEnabled = TRUE;
printf("Port %ld is open in the firewall.\n", portNumber);
}
else
{
printf("Port %ld is not open in the firewall.\n", portNumber);
}
}
else
{
// The globally open port was not in the collection.
hr = S_OK;
printf("Port %ld is not open in the firewall.\n", portNumber);
}
error:
// Release the globally open port.
if (fwOpenPort != NULL)
{
fwOpenPort->Release();
}
// Release the globally open ports collection.
if (fwOpenPorts != NULL)
{
fwOpenPorts->Release();
}
return hr;
}
HRESULT WindowsFirewallPortAdd(
IN INetFwProfile* fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
IN const wchar_t* name
)
{
HRESULT hr = S_OK;
BOOL fwPortEnabled;
BSTR fwBstrName = NULL;
INetFwOpenPort* fwOpenPort = NULL;
INetFwOpenPorts* fwOpenPorts = NULL;
_ASSERT(fwProfile != NULL);
_ASSERT(name != NULL);
// First check to see if the port is already added.
hr = WindowsFirewallPortIsEnabled(
fwProfile,
portNumber,
ipProtocol,
&fwPortEnabled
);
if (FAILED(hr))
{
printf("WindowsFirewallPortIsEnabled failed: 0x%08lx\n", hr);
goto error;
}
// Only add the port if it isn't already added.
if (!fwPortEnabled)
{
// Retrieve the collection of globally open ports.
hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
if (FAILED(hr))
{
printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr);
goto error;
}
// Create an instance of an open port.
hr = CoCreateInstance(
__uuidof(NetFwOpenPort),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(INetFwOpenPort),
(void**)&fwOpenPort
);
if (FAILED(hr))
{
printf("CoCreateInstance failed: 0x%08lx\n", hr);
goto error;
}
// Set the port number.
hr = fwOpenPort->put_Port(portNumber);
if (FAILED(hr))
{
printf("put_Port failed: 0x%08lx\n", hr);
goto error;
}
// Set the IP protocol.
hr = fwOpenPort->put_Protocol(ipProtocol);
if (FAILED(hr))
{
printf("put_Protocol failed: 0x%08lx\n", hr);
goto error;
}
// Allocate a BSTR for the friendly name of the port.
fwBstrName = SysAllocString(name);
if (SysStringLen(fwBstrName) == 0)
{
hr = E_OUTOFMEMORY;
printf("SysAllocString failed: 0x%08lx\n", hr);
goto error;
}
// Set the friendly name of the port.
hr = fwOpenPort->put_Name(fwBstrName);
if (FAILED(hr))
{
printf("put_Name failed: 0x%08lx\n", hr);
goto error;
}
// Opens the port and adds it to the collection.
hr = fwOpenPorts->Add(fwOpenPort);
if (FAILED(hr))
{
printf("Add failed: 0x%08lx\n", hr);
goto error;
}
printf("Port %ld is now open in the firewall.\n", portNumber);
}
error:
// Free the BSTR.
SysFreeString(fwBstrName);
// Release the open port instance.
if (fwOpenPort != NULL)
{
fwOpenPort->Release();
}
// Release the globally open ports collection.
if (fwOpenPorts != NULL)
{
fwOpenPorts->Release();
}
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
HRESULT comInit = E_FAIL;
INetFwProfile* fwProfile = NULL;
// Initialize COM.
comInit = CoInitializeEx(
0,
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE
);
// Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
// initialized with a different mode. Since we don't care what the mode is,
// we'll just use the existing mode.
if (comInit != RPC_E_CHANGED_MODE)
{
hr = comInit;
if (FAILED(hr))
{
printf("CoInitializeEx failed: 0x%08lx\n", hr);
goto error;
}
}
// Retrieve the firewall profile currently in effect.
hr = WindowsFirewallInitialize(&fwProfile);
if (FAILED(hr))
{
printf("WindowsFirewallInitialize failed: 0x%08lx\n", hr);
goto error;
}
wchar_t* filename= L"C:\\Program Files\\SERVER.EXE";
wchar_t* friendlyfilename= L"SERVER";
WindowsFirewallAddApp( fwProfile , filename , friendlyfilename );
error:
return 0;
}
I tried to run the above sample , trying to add my Server.exe to the Windows Firewall Exception list.
The program says , it successfully added the exception list.
but still when i run the Server.exe application , I'm still getting "Windows Firewall has blocked some features of this program."
<<http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/ff42225b-3f4a-4e06-90fd-4d923917924f/>>
Thanks much.
Hello,
Thank you for your question.
I am currently looking into this issue and will give you an update as soon as possible.
Thank you for your understanding and support.
YI
这篇关于在Windows VISTA上,以下代码不起作用,它只是一个Microsoft示例...... :-(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!