tags:

views:

162

answers:

2

I am converting an old VB COM object (which I didn't write) to C++ using ATL. One of the methods, according to the IDL, takes an IDispatch* as a parameter and the documentation and samples for this method claim that you can pass either a string (which is the progid of an object that will be created and used by the control) or an IDispatch* to an object that has already been created. How on earth do I implement this in ATL?

For example, the IDL:

[id(1)] HRESULT Test(IDispatch* obj);

The samples (which are all JScript):

obj.Test("foo.bar");

or

var someObject = new ActiveXObject("foo.bar");
obj.Test(someObject);

To make matters even more bizarre the actual VB code that implements this method actually declares the 'obj' parameter as a string! However, it all seems to work.

Can you even pass a string to a COM method that takes an IDispatch*? If so, can I determine that the IDispatch* is actually a string in my C++ ATL code? Even better, if it's an IDispatch that implements a specific interface I will want to call methods on it, or instantiate an object if it's a string.

Any ideas welcome!

+1  A: 

Declare the method so that it takes a VARIANT argument and check the actual type at runtime.

Alexandre Jasmin
A: 

If you have instance obj of VB6 class Class1 and try to assign it to a primitive type like this

    Dim str As String
    str = obj

... then Class1's default property gets evaluated. Same rule applies for method calls

Function MyFunc(str As String)
   ...
End Function

   MyFunc obj

Last method call will succeed only if Class1 has a default property defined and obj's default property value can be cast to String.

wqw