{"id":501,"date":"2015-06-03T10:11:44","date_gmt":"2015-06-03T00:11:44","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=501"},"modified":"2015-06-03T10:11:44","modified_gmt":"2015-06-03T00:11:44","slug":"programmably-dislable-windows-firewall-in-cc","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=501","title":{"rendered":"Programmably dislable Windows Firewall in C\/C++"},"content":{"rendered":"<pre lang=\"c\">\/\/ firewall.cpp : Defines the entry point for the console application.\n\/\/\n\n#include \"stdafx.h\"\n\/********************************************************************++\nCopyright (C) Microsoft. All Rights Reserved.\n\nAbstract:\n    This C++ file includes sample code for disabling Windows Firewall \n    per profile using the Microsoft Windows Firewall APIs.\n\n--********************************************************************\/\n#include <windows.h>\n#include <stdio.h>\n#include <netfw.h>\n\n#pragma comment( lib, \"ole32.lib\" )\n\n\/\/ Forward declarations\nHRESULT     WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2);\n\nint __cdecl main()\n{\n    HRESULT hrComInit = S_OK;\n    HRESULT hr = S_OK;\n\n    INetFwPolicy2 *pNetFwPolicy2 = NULL;\n\n    \/\/ Initialize COM.\n    hrComInit = CoInitializeEx(\n                    0,\n                    COINIT_APARTMENTTHREADED\n                    );\n\n    \/\/ Ignore RPC_E_CHANGED_MODE; this just means that COM has already been\n    \/\/ initialized with a different mode. Since we don't care what the mode is,\n    \/\/ we'll just use the existing mode.\n    if (hrComInit != RPC_E_CHANGED_MODE)\n    {\n        if (FAILED(hrComInit))\n        {\n            printf(\"CoInitializeEx failed: 0x%08lxn\", hrComInit);\n            goto Cleanup;\n        }\n    }\n\n    \/\/ Retrieve INetFwPolicy2\n    hr = WFCOMInitialize(&amp;pNetFwPolicy2);\n    if (FAILED(hr))\n    {\n        goto Cleanup;\n    }\n\n    \/\/ Disable Windows Firewall for the Domain profile\n    hr = pNetFwPolicy2-&gt;put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, FALSE);\n    if (FAILED(hr))\n    {\n        printf(\"put_FirewallEnabled failed for Domain: 0x%08lxn\", hr);\n        goto Cleanup;\n    }\n\n    \/\/ Disable Windows Firewall for the Private profile\n    hr = pNetFwPolicy2-&gt;put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, FALSE);\n    if (FAILED(hr))\n    {\n        printf(\"put_FirewallEnabled failed for Private: 0x%08lxn\", hr);\n        goto Cleanup;\n    }\n\n    \/\/ Disable Windows Firewall for the Public profile\n    hr = pNetFwPolicy2-&gt;put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, FALSE);\n    if (FAILED(hr))\n    {\n        printf(\"put_FirewallEnabled failed for Public: 0x%08lxn\", hr);\n        goto Cleanup;\n    }\n\nCleanup:\n\n    \/\/ Release INetFwPolicy2\n    if (pNetFwPolicy2 != NULL)\n    {\n        pNetFwPolicy2-&gt;Release();\n    }\n\n    \/\/ Uninitialize COM.\n    if (SUCCEEDED(hrComInit))\n    {\n        CoUninitialize();\n    }\n   \n    return 0;\n}\n\n\/\/ Instantiate INetFwPolicy2\nHRESULT WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)\n{\n    HRESULT hr = S_OK;\n\n    hr = CoCreateInstance(\n        __uuidof(NetFwPolicy2), \n        NULL, \n        CLSCTX_INPROC_SERVER, \n        __uuidof(INetFwPolicy2), \n        (void**)ppNetFwPolicy2);\n\n    if (FAILED(hr))\n    {\n        printf(\"CoCreateInstance for INetFwPolicy2 failed: 0x%08lxn\", hr);\n        goto Cleanup;        \n    }\n\nCleanup:\n    return hr;\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ firewall.cpp : Defines the entry point for the console application. \/\/ #include &#8220;stdafx.h&#8221; \/********************************************************************++ Copyright (C) Microsoft. All Rights Reserved. Abstract: This C++ file includes sample code for disabling Windows Firewall per profile using the Microsoft Windows Firewall APIs. &#8211;********************************************************************\/ #include #include #include #pragma comment( lib, &#8220;ole32.lib&#8221; ) \/\/ Forward declarations HRESULT WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2); &hellip; <a href=\"https:\/\/nickdu.com\/?p=501\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Programmably dislable Windows Firewall in C\/C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=501"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/501\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}