掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ファイアウォール有効無効を取得するには (ID:71110)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
追伸・・・ 有効無効の取得・設定も作っていたので参考に! static 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; } ほかにも・・・ static 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; } static 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; } static 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; } static 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; } 以上。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.