I want to Post messages directly to the HWND that's owned by COM in my process. How do I get the HWND that COM is using in single-threaded-apartment mode?
Asked
Active
Viewed 153 times
-1
-
2What would be the use case for this? I can't help but think, that this is a terrible hack, likely the result of an [XY problem](http://xyproblem.info/). – IInspectable Jul 29 '17 at 11:22
-
1It's a terrible hack. You're right. That isn't illegal. Is it? – zumalifeguard Jul 30 '17 at 02:22
-
It is legal. It is also legal to juggle chainsaws. – IInspectable Jul 30 '17 at 02:24
-
I'm pretty sure I agree with you 100%. But what do I do about this? I want to capture this information on stackoverflow so that I, or others, can get back to it at some other time in the future. – zumalifeguard Jul 30 '17 at 02:25
-
Again, what would be the use case for this? Why don't you ask about the real problem you are trying to solve instead of your proposed solution? – IInspectable Jul 30 '17 at 02:30
-
1@zumalifeguard I tried to do this sort of thing (and was successful) many years ago, but I found better solutions to every problem for which I have ever considered this. If you would like to discuss, here or privately, your use case, I would be happy to offer some alternatives for you to consider. – Michael Gunter Jul 31 '17 at 15:26
1 Answers
1
Try this:
HWND prevWindow = NULL;
HWND hwnd;
for ( ;; )
{
hwnd = FindWindowEx( HWND_MESSAGE, prevWindow, L"OleMainThreadWndClass", NULL );
if ( !hwnd )
break;
if ( GetWindowThreadProcessId( hwnd, NULL ) == GetCurrentThreadId() )
break;
prevWindow = hwnd;
WCHAR className[255];
*className = 0;
::GetClassName( hwnd, className, 255 );
}
Let me know if it works.

zumalifeguard
- 8,648
- 5
- 43
- 56
-
6You're asking yourself to tell if that works? Wow :D – Antti Haapala -- Слава Україні Jul 29 '17 at 05:47
-
1Answers on Stack Overflow should answer the question being asked. If you need to ask, whether a proposed solution is correct or not, you need to put that in your question. Please use the [edit](https://stackoverflow.com/posts/45385193/edit) link and update your question. – IInspectable Jul 29 '17 at 11:23
-
-
Wait. I'm not asking if a proposed solution is correct or not in the question. The question is simply asking how to do it. – zumalifeguard Jul 30 '17 at 02:23