Solved

passing binary data through an interface

Posted on 2000-05-04
3
246 Views
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.  :/
0
Comment
Question by:SD_Adept
[X]
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
3 Comments
 
LVL 4

Accepted Solution

by:
captainkirk earned 100 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...
0
 
LVL 3

Expert Comment

by:Norbert
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.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2818190
thanks...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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