This question on StackOverflow presents a minimal Win32 program written in C which launches some processes (Notepad and Calc) using CreateProcess
.
The OP's issue is that the windows of these programs appear in the background, behind the Explorer window from where the sample program is launched.
Though the question is closed as a duplicate, the OP came up with a solution which is not found in the referenced answer. The solution consists of aadding a single TranslateMsg
call to the program, before the CreateProcess
calls:
MSG msg;
TranslateMessage(&msg);
With this in place, the windows of the launched processes pop into the foreground, as expected.
My question is: can someone who has a bit of knowledge about Windows internals explain how this TranslateMessage
call is helping?
The call is not even well-defined in the above usage; TranslateMessage
is supposed to operate on a message pulled from the message queue. The msg
object is not even initialized. The trick still works if we change this to MSG msg = { 0 }
. There seems to be a bug/hack in Windows based on the idea that if a process never calls TranslateMessage
then it's a background process, and the window of any process spawned by it goes to the background. Even a useless call to TranslateMessage
with garbage data is sufficient to fool this kludge.