Get IUnkown using window handle

496 views Asked by At

I know this may be a long shot so forgive me as I don't really know that much about COM.

Basically what I am trying to do is get the pointer to the IUnknown interface for a running application, the problem is the only thing I know about the app is its main window handle.

Would this be possible?

2

There are 2 answers

0
nitzmahone On BEST ANSWER

The "standard" way to do this is with the AccessibleObjectFromWindow Win32 API. That said, not many things actually support this, so your mileage may vary. You'd PInvoke to AccessibleObjectFromWindow, pass the HWND, then use Marshal.GetObjectForIUnknown on the IntPtr you get back.

0
Sheng Jiang 蒋晟 On

Sometimes yes, if the window is in your process (otherwise a pointer in another process is meaningless to you) and the window's writer explicitly tell you that it is expecting a message and returning an interface. Mostly no, people usually do not write hacks when there is no need to and sending messages unknown to the writer would have unpredictable behavior.