hi have a COM visible API in C# which looks like the following:
public void DoSomething(string par1, string par2, object additionalParameter)
The idea is that based on the value of the string parameters I expect a different class as the third parameter and cast it appropriately in the implementation (I know this design is not optimal but I don't have much flexibility here).
Suppose that for some combination of the string parameters the type of the additional parameter is the following:
[ComVisible(true)]
[Serializable]
public class TestObject
{
public string String{ get; set; }
public long Long { get; set; }
}
I need to my API method from some unmanaged code; however I am having difficulties in creating the a proper variant
needed for the third parameter.
I am using the CComVariant(...)
passing an IDispatch
pointing to a TestObject that I have just built.
Suppose that pTestObject
is an IDispatch pointer to my TestObject, I have something like the following:
CComVariant pObjectVariant(pTestObject);
DoSomething(BSTR(L"first"), BSTR(L"second"), pObjectVariant);
However, when the C# function is finally invoked, I see that the object has type bool
instead of TestObject
that I was expecting.
Any idea?
Stefano
I have a couple of suggestions. First of all, create an interface for anything that you touch in COM, even if its just a bog-standard DTO that has no methods and only properties. COM loves interfaces. It loves them so much, that everything you touch in COM is an interface.
The other suggestion is that you place a GuidAttribute on anything that you touch in COM. This will ensure that your registry doesn't get crapped up when you register the managed assembly with COM. COM loves GUIDs more than it loves interfaces, and it gets confused easily if an interface is registered to more than one GUID, which can happen if you don't hard-fix the interface GUIDs in code. Concrete classes also need a GUIDAttribute.
I know it sucks, but that's why MS is trying so hard to get people away from using COM.
That being said, you probably want C# like this:
That can be called by the following C++: