Advertisement

COM gurus please help (Passing non standard types)

Started by December 22, 2000 09:18 AM
8 comments, last by itchynads 24 years ago
I was wondering if any COM/VC++ gurus could assist me in a problem. I''m trying to pass a Win32 HWND structure to a COM object via a method call of that COM object. ie: MyComObject->SetHWND(&MyHWND); And in the IDL: [id(2), helpstring("method SetHWND")] HRESULT SetHWND([in] sometype parent); I''m not exactly sure how to do this or what type to use in the IDL for the parameter . I can handle passing standard automation types just fine but not the non-standard stuff. Is there some sort of pointer type I could use? Thanks.
A HWND is simply a long value, you can cast the two interchangerably.
Advertisement
Ummm, no. You might be able to get away with bogus stuff like that in regular Win32 programming but doing it in a COM interface is not going to work.

There''s nothing magic about an HWND. Just define your method like you would any other time: SetHWND([in] HWND MyHWND);

-Mike
Hmmm thanks for the help so far but....

When I try to pass a HWND from VisualBasic (using VB to test VC COM object), VB spits out an error saying that it does not support the automation type Is there any way to bypass this?

In VB: MyObject.SetHWND Picture1.hWnd


quote: Original post by Anonymous Poster

Ummm, no. You might be able to get away with bogus stuff like that in regular Win32 programming but doing it in a COM interface is not going to work.

There''s nothing magic about an HWND. Just define your method like you would any other time: SetHWND([in] HWND MyHWND);

-Mike


Blah, that''s just ignorant. DX does this with HWNDs all the time. Casting to a long to pass between different COM implementations is perfectly fine.

MSN
No it isn''t. DirectX is a special case because it''s local machine only - maybe even inproc only I don''t know the details. For remote calls or in restricted security situations casting a HWND to a long will not work.

-Mike
Advertisement
OK lets get something straight Mr. Anonymous, to pass an HWND you HAVE to use a long This is how Visual Basic does it and is how it is defined in the VB6.OLB typelib. Since a long is an automation compatible type.

Check this snippet out:

[propget, helpstring("Returns a handle (from Microsoft Windows) to an object''s window."), helpcontext(0x000dfc59)]        HRESULT _stdcall hWnd([out, retval] long* ); 


So to answer your question itchy, use a long when passing handles to an interface. (Internally cast the long to a HWND in your CoClass implementation if you use C++).

Now Anonymous is right in regards to RPCacross MACHINE boundaries - the HWND value will most likely not be valid. Now with out-of-process servers you can easily reuse the HWND handle with a little help from the Win32 API.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Try this itchy:

[id(2), helpstring("method SetHWND")] HRESULT SetHWND([in] long parent); 


Hopefully that helps you.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thanks for the help everyone. I did as some have suggested and passed the handle as a long and recasting it back to a HWND. It works fine now.
quote: Original post by Anonymous Poster

No it isn''t. DirectX is a special case because it''s local machine only - maybe even inproc only I don''t know the details. For remote calls or in restricted security situations casting a HWND to a long will not work.

-Mike


Gahh! Then why the hell would you send an HWND over the wire in the first place? You''d use some indirect handle scheme for that, sine the other stuff would have no context across the wire. For local computer stuff, it''s fine to cast between an HWND and a LONG. Hence the API calls GetWindowLong(GWL_PARENT,...)

MSN

This topic is closed to new replies.

Advertisement