Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Control How COM Marshals Your Data


March 1998/Control How COM Marshals Your Data/Listing 2

Listing 2: marshal.c — Custom data marshaling/unmarshaling code

// Custom Marshalling Code to compress/decompress BOOL Array
// before and after transmission.
// Author : Fran Heeran

#include "objidl.h"
#include "rpcproxy.h"
#include "sobj.h"
#include "marsamp.h"

// ====================================================
// Called to Marshal Array of BOOLS into the RPC buffer
// ====================================================
unsigned char __RPC_FAR * __RPC_USER LPMYARRAY_UserMarshal(
   ULONG __RPC_FAR *pFlags, BYTE __RPC_FAR *pBuffer,
   LPMYARRAY __RPC_FAR *pArr)
{
   BOOL  *pBoolArr = *pArr;
   int   i;
   BYTE  mask = 0x80;
   BYTE  value = 0;

   // We need to be true to our wire_type definition so
   // insert the data size here
   *((short *)pBuffer) = (FLAG_ARRAY_SIZE / 8);
   pBuffer += sizeof(short);

   // Loop through the array packing 8 BOOL values per BYTE
   for (i = 0;i < FLAG_ARRAY_SIZE;i++) {
      if (*pBoolArr++)
         value |= mask;
      // If we are doing the 8th element - Store
      // the value and reset mask and value elements
      if (mask == 1) {
         mask = 0x80;
         *pBuffer++ = value;
         value = 0;
      }
      else
         mask = mask >> 1; // On to the next bit
   }

   // pBuffer must point to the first byte after our marshalled data
   return pBuffer;
}

// =========================================================
// Called to Unmarshal bitfields back into an Array of BOOLS
// =========================================================
unsigned char __RPC_FAR * __RPC_USER LPMYARRAY_UserUnmarshal(
      ULONG __RPC_FAR *pFlags, BYTE __RPC_FAR *pBuffer,
      LPMYARRAY __RPC_FAR *pArr)
{
   BOOL  *pArray;
   BYTE  mask = 0x80;
   int   i;

   pBuffer += sizeof(short);  // Skip past tthe size field

   // Allocate user memory for the array
   pArray = CoTaskMemAlloc(FLAG_ARRAY_SIZE * sizeof(BOOL));
   *pArr = pArray;

   // Rebuild the original data from bitfields by cycling
   // through each bit in the bytes
   for (i = 0;i < FLAG_ARRAY_SIZE;i++) {
      if (*pBuffer & mask)
         *pArray++ = TRUE;
      else
         *pArray++ = FALSE;

      if (mask == 1) {
         mask = 0x80;
         pBuffer++;
      }
      else
         mask = mask >> 1;
   }

   // pBuffer must point to first byte after our marshaled data
   return pBuffer;
}

// =============================================================
// Returns the amount of space our compressed array will require
// =============================================================
unsigned long __RPC_USER LPMYARRAY_UserSize(
      ULONG __RPC_FAR *pFlags, ULONG startingSize,
      LPMYARRAY __RPC_FAR *pStr)
{
   return startingSize + (FLAG_ARRAY_SIZE / 8) + sizeof(short);
}

// ===========================================================
// Free's the memory allocated when the array was unmarshalled
// ===========================================================
void __RPC_USER LPMYARRAY_UserFree(ULONG __RPC_FAR *pFlags,
      LPMYARRAY __RPC_FAR *pStr)
{
   CoTaskMemFree((LPVOID)*pStr);
}
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.