Enable disable Firewall for Win7


  • I have an application compiled on VS2005 using "put_FirewallEnabled() ". When I am running on WinXP it works fine. But when I run it on Win7 machine it fails. The reason I found is Win7 support Firewall with advanced settings. And "put_FirewallEnabled() " is deprecated.

    Remedy, I found below related link But it can be compiled only with VS2010 or VS2008 with SDK 7.0.

    #include <objbase.h>
    #include <windows.h>
    #include <stdio.h>
    #include <comutil.h>
    #include <strsafe.h>
    #include <netfw.h>
    #import "netfw.tlb"
    HRESULT CoCreateInstanceAsAdmin(
    		HWND hwnd, 
    		REFCLSID rclsid, 
    		REFIID riid, 
    		__out void ** ppv)
      BIND_OPTS3 bo;
      WCHAR  wszCLSID[50];
      WCHAR  wszMonikerName[300];
      StringFromGUID2(rclsid, wszCLSID, sizeof(wszCLSID)/sizeof(wszCLSID[0])); 
      HRESULT hr = StringCchPrintf(wszMonikerName, 
       		L"Elevation:Administrator!new:%s", wszCLSID);
      if (FAILED(hr))
    	return hr;
      memset(&bo, 0, sizeof(bo));
      bo.cbStruct = sizeof(bo);
      bo.hwnd = hwnd;
      bo.dwClassContext  = CLSCTX_LOCAL_SERVER;
      return CoGetObject(wszMonikerName, &bo, riid, ppv);
    int __cdecl main()
        HRESULT hr;
        BOOL fComInitialized = FALSE;
            // Initialize the COM library on the current thread
            hr = CoInitialize(NULL); 
            if (FAILED(hr))
            fComInitialized = TRUE;
            NetFwPublicTypeLib::INetFwPolicy2Ptr sipFwPolicy2;
            hr = CoCreateInstanceAsAdmin(GetDesktopWindow(), 
    	__uuidof(NetFwPolicy2), IID_PPV_ARGS(&sipFwPolicy2));
            if (FAILED(hr))
            sipFwPolicy2->FirewallEnabled[NetFwPublicTypeLib::NET_FW_PROFILE2_DOMAIN] = FALSE;
            sipFwPolicy2->FirewallEnabled[NetFwPublicTypeLib::NET_FW_PROFILE2_PRIVATE] = FALSE;
            sipFwPolicy2->FirewallEnabled[NetFwPublicTypeLib::NET_FW_PROFILE2_PUBLIC] = FALSE;
        catch (_com_error& e)
            printf ("Error. HRESULT message is: %s (0x%08lx)\n", e.ErrorMessage(), e.Error());
            if (e.ErrorInfo())
                printf ("Description: %s\n", (char *)e.Description());
        if (fComInitialized)
        return 0;

    Kindly provide me a solution that can compile on VS2005, which can run on Win7 machine.

    Please let me know if I can write any wrapper or link during run time.

    I want my application to compile on VS2005, but run Win7.

    Friday, April 15, 2011 10:42 AM


All replies