-1

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?

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
  • 2
    What 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
  • 1
    It'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 Answers1

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