3

Is it possible to have a COM method which passes a HWND? With the following method in my object CoCreateInstance returns DISP_E_BADVARTYPE (0x80020008):

STDMETHODIMP ShowDialog(HWND hWndParent);

So far, I'm getting round this problem by passing an OLE_HANDLE then casting it but it feels like a cludge:

STDMETHODIMP ShowDialog(OLE_HANDLE hWndParent);
Gregor Brandt
  • 7,659
  • 38
  • 59
Mat
  • 82,161
  • 34
  • 89
  • 109
  • Note that this can happen if you have a property on the interface with HWND type, even if it's never referenced in the .NET code! I spent quite some time chasing my tail trying to run a similar problem down before I noticed that there was something else on the interface (which I wasn't using) with an `[out, retval] HWND *` argument. The interface was `oleautomation` but not `dual`. – Craig Feb 08 '19 at 16:21

2 Answers2

2

I think that HWND is a pointer to a struct thats why you can't use it in the IDL.
If you look at Microsoft Typelibs you will see all sort of variation on how to pass a handle (From int to long to HANDLE).

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
1

Your interface is probably registered as "dual", and HWND is not one of the types supported by OLE automation. Does your interface need to be IDispatch-compatible (do you need to call it from scripting or late-bound languages)? If not, deriving from IUnknown rather than IDispatch and not registering as dual will help you out.

NB: Casting is okay as long as you are only using the method in-process.

jlew
  • 10,491
  • 1
  • 35
  • 58
  • In my experience, this error can still happen with an interface that is not marked as `dual`. – Craig Feb 08 '19 at 16:22