问题描述
这应该是一个简单的:我创建一个程序,使用win32 CreateProcess()
函数产生一个进程。一旦这个过程被加载,我找到它的窗口使用 FindWindow
并使用 SendMessage()
发送它的消息。
请考虑以下问题:
$ b $ HWND wnd;
BOOL开始()
{
//生成进程
if(!CreateProcess(...))
return FALSE;
//查找进程的窗口(类和名称已知)
wnd = FindWindow(MY_WINDOW_CLASS,MY_WINDOW_NAME);
//始终返回FALSE,因为窗口尚未创建。
return(wnd!= NULL);
}
上面的代码会(几乎?该窗口不能被快速创建和发现。如果我把线程等待,说 CreateProcess
和 FindWindow之间
调用,它工作正常。 Sleep(1000)
如何改善这个?
(编辑):指出了 WaitForInputIdle()
,并建议。引用:
This should be an easy one: I am creating a program that spawns a process using the win32 CreateProcess()
function. Once this process is loaded, I find its window using FindWindow
and send it messages using SendMessage()
. The question is, how do I know when that window is ready to accept messages?
Consider the following:
HWND wnd;
BOOL Start()
{
// Spawn the process
if (! CreateProcess(...))
return FALSE;
// Find the process's window (class and name already known)
wnd = FindWindow(MY_WINDOW_CLASS, MY_WINDOW_NAME);
// Always returns FALSE because window has not yet been created.
return (wnd != NULL);
}
The code above will (almost?) always fail; the window cannot be created and found that quickly. If I put a thread wait, say Sleep(1000)
, between the CreateProcess
and FindWindow
calls, it works fine. But this feels like a very bad hack.
How can I improve this?
(Edit): User IInspectable pointed out problems with WaitForInputIdle()
, and suggested CBT Hooks instead.
Also, CBT is short for computer-based training, for whatever reason.
(Old, beware, see comments.) You are looking for WaitForInputIdle(). Quote:
这篇关于如何确定生成的进程何时准备好? (使用CreateProcess()和FindWindow())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!