Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C++

Understanding Custom Marshaling Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (53 votes)
18 Aug 2006CPOL31 min read 208.8K   1.4K   147  
Learn the fundamental principles of COM custom marshaling by code examples.
#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)


Written By
Systems Engineer NEC
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.

Comments and Discussions