SOL9 Sample: FirewallWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * FirewallWatcher.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2008/08/07
//

#include <sol\WSAInitializer.h>

#include <sol\net\WindowsFirewall.h>
#include <sol\StringBuffer.h>

//..\cc.bat IPAddressWatcher.cpp

#include <process.h>

// Thread procedure for this Firewall Watcher program

unsigned __stdcall watcherThread(void *);

/*
 * Simple application to watch the setting (enabled or disabled) of WindowsFirewall
 * by using a thread. 
 * You can easily change this to Windows Application by defining WinMain 
 * instead of _tmain.
 */

//int APIENTRY WinMain(HINSTANCE progIns, HINSTANCE prevIns, 
//       LPSTR cmdLine, int cmdShow)
int _tmain(int argc, TCHAR** argv)

{
  WSAInitializer initializer;

  try {
    DWORD threadId=0;
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, watcherThread, NULL, 0, (unsigned int*)&threadId);

    if (hThread) {
      WaitForSingleObject(hThread, INFINITE);
      CloseHandle(hThread);
    }

  } catch (int err) {
    printf("Exception: %d\n", err);
  } catch (...) {

  }
  return 0;

}

//
// Show current status of WindowsFirewall
//
int showStatus(BOOL status)
{
  const TCHAR* report = _T("WindowFirewall is disabled.");
  const TCHAR* confirm = _T("Do you continue to monitor the change of status of Firewall?\r\n");
  
  TCHAR message[1024];

  if (status == TRUE) {
    report = _T("WindowsFirewall is enabled.");
    
  } 

  _stprintf_s(message, SizeOf(message), _T("%s\r\n\r\n%s"), report, confirm);

  //Confirm to continue monitoring.
  return MessageBox(NULL, message, _T("FirewallWatcher"), MB_ICONINFORMATION|MB_YESNO);
}


//
// Thread procedure to get notification of IPAddress changes of NeworkInterface. 
//
unsigned __stdcall watcherThread(void *lpx)
{
  int rc = 0;
  BOOL status = FALSE;

  WindowsFirewall firewall;
    
  firewall.isFirewallEnabled(status);

  rc = showStatus(status);
  if (rc ==IDNO) {
    return rc;
  }
  bool looping = true;
  while(looping) {
    Sleep(2000);
    BOOL nstatus = FALSE;
    if (SUCCEEDED(firewall.isFirewallEnabled(nstatus))) {
      if (status != nstatus) {
        status = nstatus;
        rc = showStatus(status);
        if (rc == IDNO) {
          break;
        }
      }
    }
  }
  return rc;
}

Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.