First off, I know this is a controversal discussion but I hope we can keep this technical.
I have an application which is started in background and I somehow want it to be able to activate/bring to focus a window in a different process. But calling SetForegroundWindow always fails even if the process whose window I want to activate has called AllowSetForegroundWindow(ASFW_ANY).
The reason is (IMO) that the initiating application is a background process and since it has not received the input it is not allowed to set foreground window. So everything appears in the tasklist but it is not shown.
So I tried creating a dummy window to receive input which is closed immideately and afterwards be able to call SetForegroundWindow successfully. But even the dummy window I display is displayed in background.
However, if I call
AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetCurrentThreadId(), TRUE);
before creating the dummy window, the window is indeed created in foreground and I can afterwards call SetForegroundWindow for a different HWND in a different process which works.
BUT: If I do not create the dummy window, SetForegroundWindow still returns zero although I use AttachThreadInput.
I don't understand why the AttachThreadInput hack is successful if I create an own Window (and successds for other windows afterwards) but is unsuccessful if I do not create an own window first.
How can my background process call SetForegroundWindow on a different window in a different process without creating a dummy window?
[*] The background application is actually gpg-agent.exe which calls pinentry.exe (my application) whenever a password is requested. pinentry.exe (running as background process) must request the password from another running application, therefore it must bring its window to foreground ...