Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Understanding Custom Marshaling Part 1

, 18 Aug 2006 CPOL
Learn the fundamental principles of COM custom marshaling by code examples.
custommarshaling01_src.zip
utilities
BasicSample01
Interfaces
Implementations
Clients
BasicSample01Interfaces
BasicSample01Interfaces.aps
BasicSample01Interfaces.def
BasicSample01Interfaces.ncb
BasicSample01Interfaces.rgs
BasicSample01Interfacesps.def
Immutable.rgs
ImmutableObjectFactory.rgs
BasicSample01Interfaces.vcproj.vspscc
BasicSample01InterfacesPS.vcproj.vspscc
BasicSample01InterfacesImpl
BasicSample01InterfacesImpl.aps
BasicSample01InterfacesImpl.def
BasicSample01InterfacesImpl.rgs
BasicSample01InterfacesImplps.def
ImmutableImpl.rgs
ImmutableObjectFactoryImpl.rgs
BasicSample01InterfacesImpl.ncb
BasicSample01InterfacesImpl.vcproj.vspscc
BasicSample01InterfacesImplPS.vcproj.vspscc
VCConsoleClient01
VCConsoleClient03
VCConsoleClient02
VCConsoleClient01.ncb
VCConsoleClient03.ncb
VCConsoleClient02.ncb
#include "utilities.h"

DWORD ThreadMsgWaitForSingleObject (HANDLE hHandle, DWORD dwMilliseconds)
{
  HANDLE	dwChangeHandles[1] = { hHandle };
  DWORD		dwWaitStatus = 0;
  DWORD		dwRet = 0;
  bool		bContinueLoop = true;

  // Msg Loop while waiting for hHandle to be signaled.
  while (bContinueLoop) 
  {
    // Wait for notification.
    dwWaitStatus = ::MsgWaitForMultipleObjectsEx
	(
      (DWORD)1,          // number of handles in array
      dwChangeHandles, // object-handle array
      (DWORD)dwMilliseconds,  // time-out interval
      (DWORD)(QS_ALLINPUT | QS_ALLPOSTMESSAGE),      // input-event type
      (DWORD)(MWMO_INPUTAVAILABLE)          // wait options
    );
 
    switch (dwWaitStatus) 
    { 
      // First wait (hHandle) object has been signalled.
	  case WAIT_OBJECT_0 : 
	  {
	    dwRet = dwWaitStatus;
        // Flag to indicate stop loop.
		bContinueLoop = false;
	    break;
	  }

      // Windows message has arrived.
      case WAIT_OBJECT_0 + 1: 
	  {
	    MSG msg;

        // Dispatch all windows messages in queue.
	    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	    {
	      TranslateMessage (&msg);
		  DispatchMessage(&msg);
	    }

	    break; 
	  }

      // Timeout has elapsed.
	  case WAIT_TIMEOUT :
	  {
	    dwRet = dwWaitStatus;
        // Flag to indicate stop loop.
		bContinueLoop = false;
	    break;
	  }
 
      default: 
	  {
	    break;
	  }
    } 
  }

  return dwRet;
}





DWORD ThreadMsgWaitForMultipleObjects 
(
  DWORD dwCount, 
  LPHANDLE pHandles, 
  BOOL bWaitAll, 
  DWORD dwMilliseconds
)
{
  HANDLE_VECTOR	vec_Handle;
  DWORD			dwWaitStatus = 0;
  DWORD			dwRet = 0;
  DWORD			dwCountCurrent = 0;
  DWORD			dw = 0;
  bool			bContinueLoop = true;

  if ((dwCount == 0) || (pHandles == NULL))
  {
    return 0;
  }

  dwCountCurrent = dwCount;

  // Initialize our vector of handles.
  for (dw = 0; dw < dwCountCurrent; dw++)
  {
    vec_Handle.push_back(pHandles[dw]);
  }

  // Msg Loop while waiting for hHandle to be signaled.
  while (bContinueLoop) 
  {
    // Wait for notification.
    dwWaitStatus = ::MsgWaitForMultipleObjectsEx
	(
      (DWORD)dwCountCurrent,          // number of handles in array
      &(vec_Handle[0]), // object-handle array
      (DWORD)dwMilliseconds,  // time-out interval
      (DWORD)(QS_ALLINPUT | QS_ALLPOSTMESSAGE),      // input-event type
      (DWORD)(MWMO_INPUTAVAILABLE)          // wait options
    );

	if ((dwWaitStatus >= WAIT_OBJECT_0) && (dwWaitStatus <= (WAIT_OBJECT_0 + dwCountCurrent - 1)))
	{
	  // A wait handle object in pHandles array has been signalled.
	  // Check to see if bWaitAll is true or false.
	  if (bWaitAll)
	  {
	    // We need to wait on ALL handles.
		// Hence we need to call MsgWaitForMultipleObjectsEx() again
		// with the same vec_Handle but minus the handle which has 
		// just been signaled.
		DWORD	dwIndex = dwWaitStatus - WAIT_OBJECT_0;
		HANDLE	hHandleJustSignaled = vec_Handle[dwIndex];

        // Reduce our current count of handles.
		dwCountCurrent--;

		if (dwCountCurrent <= 0)
		{
		  // No more handles to wait on.
		  // Return a zero value.
	      dwRet = 0;
          // Flag to indicate stop loop.
		  bContinueLoop = false;
		}
		else
		{
		  // First make a copy of the current vector
		  HANDLE_VECTOR	vec_HandleTemp = vec_Handle;

          // Clear our vector of handles.
          vec_Handle.clear();
          // Re-initialize our vector of handles.
		  // Note : we use vec_HandleTemp.size().
		  // MUST NOT use "dwCountCurrent" - it has already
		  // been reduced by 1.
          for (dw = 0; dw < vec_HandleTemp.size(); dw++)
          {
		    if (vec_HandleTemp[dw] != hHandleJustSignaled)
			{
              vec_Handle.push_back(vec_HandleTemp[dw]);
			}
          }
		}
	  }
	  else
	  {
	    dwRet = dwWaitStatus;
        // Flag to indicate stop loop.
		bContinueLoop = false;
	  }
	}
	else if (dwWaitStatus == WAIT_OBJECT_0 + dwCountCurrent)
	{
	  // Windows message has arrived.
	  MSG msg;

      // Dispatch all windows messages in queue.
	  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	  {
	    TranslateMessage (&msg);
	    DispatchMessage(&msg);
	  }
	}
	else if (dwWaitStatus == WAIT_TIMEOUT)
	{
	  // Timeout has elapsed.
	  dwRet = dwWaitStatus;
      // Flag to indicate stop loop.
	  bContinueLoop = false;
	}
  }

  return dwRet;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lim Bio Liong
Web Developer
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.
 
Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.
 
Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 18 Aug 2006
Article Copyright 2006 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid