A Little Too Much Structure

Dear Dr. GUI,

I have written an ActiveX control and I want to define a method that has a parameter of type "pointer to structure." There is no explicit choice in the VC++ Class Wizard for "pointer to structure" for parameter types. I am using the control in Visual Basic and there is no way that I can find to get a pointer to a user-defined type (structure) to send as a generic pointer to the control's method. Is there any way to pass data in a structure to an ActiveX control method?

Thanks for your help!
Terry Coupe

Dr. GUI replies:

The differences in structures between Visual Basic and Visual C++ can cause lots of problems, especially since the layout of C++ structures can vary depending on the compiler options you choose. And not having your structures match up would be quite unsafe. That's why the recommended way to pass a structure to and from Visual Basic is with a SafeArray. A SafeArray is simply a type of structure that can be packaged in a variant format (Visual Basic understands these) and can contain any data type and number of elements. For example, if you want to pass a structure containing two longs and a character array of 20 bytes, you'd need 28 bytes total. Therefore, you'd create a SafeArray of 28 elements where each element is a single byte or a one-byte unsigned integer or, in the language of variant types, VT_UI1. The variant parameter you will pass will be a combination of VTUI1 | VT_SAFEARRAY. There are manipulator functions to help you create, initialize, destroy, and access the elements or the entire buffer of a SafeArray.

Microsoft Knowledge Base article Q154172, "How To Pass Binary Data Between an ActiveX Control and VB," provides some fundamentals of working with SafeArrays and points to several other articles that will go into more detail about passing structures between Visual Basic and C++-based automation objects. Be sure to read it, and be sure to pass data safely!