SOL9 Sample: IPAdapterAddressesWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL++2000
// 2008/07/21
//

#include <sol\WSAInitializer.h>

#include <sol\net\IPAdapterAddresses.h>
#include <sol\StringBufferT.h>
#include <sol/StringConverter.h>

//..\cc.bat IPAdapterAddressesWatcher.cpp

//#include <process.h>

// Thread procedure for this IP Address Watcher program

unsigned __stdcall watcherThread(void *);

/*
 * Simple application to see the effect of Windows NotifyAddrChange API. 
 * You can easily change this to Windows Application by defining WinMain 
 * instead of main.
 */

//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 state of network interface with up IP addresses.
//
int showMessageBox(IPAdapterAddresses& address)
{
  StringBufferT<char> sbuffer;

  const char* state = "IPAdapterAddressesWatcher: Address changed";
  const char* confirm = "Do you continue to monitor the change of IP address?\r\n\r\n";
  sbuffer.append(confirm);
  address.listup();
  int count = 0;
  PIP_ADAPTER_ADDRESSES adapter= address.getAdaptersAddresses(count);
  for (PIP_ADAPTER_ADDRESSES adpt= adapter; adpt !=NULL; adpt= adpt->Next) {
        
    if(adpt->IfType == IF_TYPE_SOFTWARE_LOOPBACK ) {
        //printf("Ignore %d\n", n);
      continue;     // Ignore 
    }
    if (adpt->OperStatus == IfOperStatusUp) {
      StringConverter converter;  
      
      char* friendlyName = converter.toMultiByte(adpt->FriendlyName);
      sbuffer.append("Up:");
      sbuffer.append(friendlyName);
      sbuffer.append("\r\n");
      delete [] friendlyName;

    }
  }
  return MessageBoxA(NULL, (const char*)sbuffer, state, MB_ICONQUESTION|MB_YESNO);
}


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

  //WSAInitializer initializer;

  IPAdapterAddresses address;
  address.listup();

  if (showMessageBox(address) == IDNO) {
    return rc;
  }
  bool looping = true;
  while(looping) {
    //printf("Waiting NotifyAddrChange.Blocked here\n");
    DWORD ret = NotifyAddrChange(NULL, NULL);
    
    if (ret == NO_ERROR) {
      
      address.listup();
      int c = address.getCount();

      printf("Detected IPAddress change...Count=%d\n", c);
            
      if (c > 0) {
        if (showMessageBox(address) == IDNO) {
          break;
        }
      }
    } else {
      printf("NotifyAddrChange:Error=%d\n", ret);
    }  
  }
  return rc;
}

Last modified: 2 May 2016

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