SOL9 Sample: NetFwOpenPortsAddPort
|
1 Screenshot
2 Source code
/*
* NetFwOpenPortsAddPort.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2012/09/23
// Note: This program must be run as System Administrator.
#include <sol\Locale.h>
#include <sol\com\ApartmentThreadedModel.h>
#include <sol\netfw\NetFwMgr.h>
#include <sol\netfw\NetFwPolicy.h>
#include <sol\netfw\NetFwProfile.h>
#include <sol\netfw\NetFwOpenPorts.h>
#include <sol\netfw\NetFwOpenPort.h>
void _tmain(int argc, const TCHAR* argv[])
{
Locale locale;
try {
ApartmentThreadedModel model;
_tprintf(_T("Create a netFwMgr object\n"));
SOL::NetFwMgr netFwMgr;
_tprintf(_T("Get a localPolicy from the netFwMgr\n"));
SOL::NetFwPolicy localPolicy = netFwMgr.getLocalPolicy();
_tprintf(_T("Get a currentProfile from the localPolicy\n"));
SOL::NetFwProfile currentProfile = localPolicy.getCurrentProfile();
_tprintf(_T("Get an openPorts from the currentProfile\n"));
SOL::NetFwOpenPorts openPorts = currentProfile.getGloballyOpenPorts();
_tprintf(_T("Create an openPort\n"));
SOL::NetFwOpenPort openPort;
_bstr_t name = "C:\\sample\\SolWebServer.exe";
openPort.putName(name);
openPort.putIpVersion(NET_FW_IP_VERSION_ANY);
openPort.putProtocol(NET_FW_IP_PROTOCOL_TCP);
openPort.putPort(8081);
openPort.putScope(NET_FW_SCOPE_ALL);
openPort.putRemoteAddresses(NULL);
_tprintf(_T("Add the openPort to the openPorts\n"));
_tprintf(_T("Hit any key")); getchar();
openPorts.add(openPort);
_tprintf(_T("OK. Added\n"));
_tprintf(_T("Get a solPort from the openPorts\n"));
SOL::NetFwOpenPort solPort = openPorts.item(6400, NET_FW_IP_PROTOCOL_TCP);
solPort.display();
printf("Remove a rule from the rules by name\n");
printf("Hit any key"); getchar();
openPorts.remove(8081, NET_FW_IP_PROTOCOL_TCP);
printf("OK. Removed\n");
printf("Hit any key"); getchar();
} catch (Exception& ex) {
ex.printf();
} catch (HRESULT hr) {
_tprintf(_T("Exception: HRESULT=%x\n"), hr);
_tprintf(_T("This program must be run as System Administrator\n"));
}
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.