Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


passing binary data through an interface

Posted on 2000-05-04
Medium Priority
Last Modified: 2013-11-20
Whats it he most efficient and correct way to pass binary data through a COM interface?  I need to pass variable sized binary data through an interface, then I need to be able to have that COM object release that memory.  I assume I will ahve to use CoTaskAlloc etc... but how do I get it all through the interface?  Do I stuff it all into a BSTR? (yuk), or is there something else I have to do?  Note: this must be compatible with VB etc... so I have to run a fully compliant IDispatch interface.  :/
Question by:SD_Adept
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

captainkirk earned 300 total points
ID: 2778378
Use a VARIANT... here's a tidbit from the MSDN docs:

VARIANT Structure
[This is preliminary documentation and subject to change.]

The VARIANT structure carries a wide variety of information used when calling functions through the IDispatch interface. For more information about how Active Accessibility uses the IDispatch interface, see IDispatch Interface. Since so many Active Accessibility API elements use IDispatch, you'll frequently work with VARIANT structures as well.

Essentially, the VARIANT structure is a container for a large union that can carry many types of data. Before using the structure, you must initialize it by calling the VariantInit OLE function.

The value in the first structure member, vt, describes which of the union members is valid. When you receive information in a VARIANT structure, check the vt member to find out which member to look at next. Similarly, when you send information using a VARIANT structure, always set vt to reflect the union member you're using to send the information.

VARIANT Manipulation API Functions
These functions are provided to allow applications to manipulate VARIANTARG variables. Applications that implement IDispatch should test each VARIANTARG for all permitted types by attempting to coerce the variant to each type using VariantChangeType or VariantChangeTypeEx. If objects are allowed, the application should always test for object types before other types. If an object type is expected, the application must use IUnknown::QueryInterface to test whether the object is the desired type.

Although applications can access and interpret the VARIANTARGs without these functions, using them ensures uniform conversion and coercion rules for all implementers of IDispatch. For example, these functions automatically coerce numeric arguments to strings, and vice versa, when necessary.

Because variants can contain strings, references to scalars, objects, and arrays, all data ownership rules must be followed. All variant manipulation functions should conform to the following rules:

Before use, all VARIANTARGs must be initialized by VariantInit.
For the types VT_UI1, VT_I2, VT_I4, VT_R4, VT_R8, VT_BOOL, VT_ERROR, VT_CY, VT_DECIMAL, and VT_DATE, data is stored within the VARIANT structure. Any pointers to the data become invalid when the type of the variant is changed.
For VT_BYREF | any type, the memory pointed to by the variant is owned and freed by the caller of the function.
For VT_BSTR, there is only one owner for the string. All strings in variants must be allocated with the SysAllocString function. When releasing or changing the type of a variant with the VT_BSTR type, SysFreeString is called on the contained string.
For VT_ARRAY | any type, the rule is analogous to the rule for VT_BSTR. All arrays in variants must be allocated with SafeArrayCreate. When releasing or changing the type of a variant with the VT_ARRAY flag set, SafeArrayDestroy is called.
For VT_DISPATCH and VT_UNKNOWN, the objects that are pointed to have reference counts that are incremented when they are placed in a variant. When releasing or changing the type of the variant, Release is called on the object that is pointed to.

hope that helps...

Expert Comment

ID: 2780838
Variant is full supported by VB.
You can pass anything using Variant.
Using MFC you are able to use HelperClasses Like COleVariant COleSafeArray and so on
Access of variant from VB Site is very easy.Access of variant from C++ may be a little bit more difficult but at last if you understand the variant it is also easy.

Expert Comment

ID: 2818190

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question