Can I pass a HWND in a COM method?

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);
windows
com
hwnd
asked on Stack Overflow Apr 30, 2009 by Mat • edited Apr 30, 2009 by Gregor Brandt

2 Answers

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).

answered on Stack Overflow Apr 30, 2009 by Shay Erlichmen
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.

answered on Stack Overflow Apr 30, 2009 by jlew

User contributions licensed under CC BY-SA 3.0