Variants and Objects

The VariantExample function shows how you declare and call a DLL function with a Variant passed by value. A Variant passed by reference would be declared in C as a pointer to a VARIANT structure.

void VariantByRef(LPVARIANT *pvar)

While it is possible to pass an object as a Variant, it is also possible to specifically declare a function that accepts only an object. An object is passed as a dispatch pointer, either by value:

void ObjectByVal(LPDISPATCH pdisp)

or by reference:

void ObjectByRef(LPDISPATCH *ppdisp)

When you pass a variant or object by reference, you receive a pointer. If you change what the pointer is pointing to, remember that your DLL code must free any allocated object, string, or array first. Failure to free allocations results in memory leaks. More information about freeing allocated strings and arrays appears in the following sections, and the ReleaseVariant function example that appears in Chapter 6 shows how this can be done for a variant variable.

Return Values

When your function returns an object, it is declared as returning a dispatch pointer:

LPDISPATCH ReturnsObject(void)

Returning a Variant is simple:

VARIANT ReturnsVariant(void)