Why C# Object passed to COM method's VARIANT* parameter converted to VT_DISPATCH type

StackOverflow https://stackoverflow.com/questions/17959645

  •  04-06-2022
  •  | 
  •  

سؤال

I have an ATL COM service which exposes a method that takes in an [in, out] VARIANT pointer as a parameter.

For example, I have the following method for a COM interface, say IMyApiCom:

HRESULT Action([in, out] VARIANT* EmptyObj);

I wrote a C# client application that calls this, like so:

MyApiCom myapi = new MyApiCom();
Object emptyObj = new Object();
myapi.Action(ref emptyObj);

Now, when I debug the COM service and check how EmptyObj is initialized, it is of type VT_DISPATCH, whereas I expected it to be VT_EMPTY. This causes an issue for me, although it can easily be worked around (by setting it as VT_EMPTY).

Reasons for why I'd prefer it to be VT_EMPTY aside, I'd just like to know why it's initialized as VT_DISPATCH and not VT_EMPTY.

Also, is it possible to pass an object of type VT_EMPTY?

هل كانت مفيدة؟

المحلول

To get VT_EMPTY in your ATL side, simply pass null (Nothing in VB, nullptr in C++/CLI) in managed code.

Reference:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top