Passing Binary Data in COM | CodeGuru

Passing Binary Data in COM

There are two ways we can pass binary data in COM. SAFEARRAY and a pointer. by using a buffer pointer , we can get the best performance. Here is the method declared in server IDL: HRESULT Transfer([in] long cbSize, [in, size_is(cbSize)] unsigned char cBuffer[]) Here is the client code: ITargetObj * pITargeObj ; HRESULT hRC […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 28, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

There are two ways we can pass binary data in COM. SAFEARRAY and a pointer. by using a
buffer pointer , we can get the best performance.

Here is the method declared in server IDL:

HRESULT Transfer([in] long cbSize,
                 [in, size_is(cbSize)] unsigned char cBuffer[])

Here is the client code:

ITargetObj * pITargeObj ;
HRESULT hRC = ::CoCreateInstance(CLSID_TargetObj,
                                 NULL,
                                 CLSCTX_SERVER,
                                 IID_ITargetObj,
                                 (void **)&pITargetObj ) ;
if FAILED( hRC )
{
 AfxMessageBox( "Failed to make target object!" ) ;
 return FALSE ;
}

BYTE * pBuffer = (BYTE *)::CoTaskMemAlloc( 15 ) ;
CopyMemory( pBuffer, "HELLO WORLD!!!!", 15 ) ;
pITargetObj->Transfer( 15, pBuffer ) ;
::CoTaskMemFree( pBuffer ) ;
pITargetObj ->Release() ;

But it only working for a INPROC Server with
an object supporting a custom interface. If the object supporting a dual and
dispatch interface, only the first character is transferred from client to
server. Because the size_is
attribute is stripped out (it’s not supported by Automation), and the type
library is used to marshal the interface. Obviously, this will not do. By the
way, one good way to pass an array that does work for dual and dispatch
interfaces is to use a SAFEARRAY.

Using CBufferVariant

Well,It’s
seem we have to using the SAFEARRAY, It’s easy for a VB program, but I wanna
same to VC program. I’v created a class named CBufferVariant, What’s the cool
things:

Here is the method declared in server IDL:

HRESULT Transfer([in] VARIANT vData)

Here is the server code:

STDMETHODIMP CTargeObj::Transfer(VARIANT Data)
{
 CBufferVariant bvData=Data;
 if(bvData.GetLength()==0)
  return S_OK;
 send(m_nSocketFD,bvData,bvData.GetLength(),0);
 return S_OK;
}

Here is the client code:

#include "BufferVariant.h"

ITargetObj * pITargeObj ;
HRESULT hRC = ::CoCreateInstance(CLSID_TargetObj,
                                 NULL,
                                 CLSCTX_SERVER,
                                 IID_ITargetObj,
                                 (void **)&pITargetObj ) ;
if FAILED( hRC )
{
 AfxMessageBox( "Failed to make target object!" ) ;
 return FALSE ;
}

CBufferVariant bvData;

SomeStruct stData;
bvData.AssignData(&stData,sizeof(SomeStruct));
pITargetObj->Transfer( bvData );

bvData="Hello World!";
bvData+="n";
pITargetObj->Transfer( bvData );

char buffer[2048];
File1.Read(buffer,2048);
bvData.AssignData(buffer,2048);
File2.Read(buffer,1024);
bvData.AppendData(buffer,1024);
pITargetObj->Transfer( bvData );

pITargetObj->Transfer( CBufferVariant("This is a ANSI string.") ) ;

pITargetObj->Release();

Downloads

Download source – 2 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.