如何确定生成的进程何时准备好? (使用CreateProcess()和FindWindow()) [英] How to determine when spawned process is ready? (Using CreateProcess() and FindWindow())
问题描述
这应该是一个简单的:我创建一个程序,使用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)
如何改善这个?
(编辑): User IInspectable 指出了 (...)回调函数用于 SetWindowsHookEx 此外,CBT是基于计算机的训练, > (old,beware,see comments。)您正在查找 WaitForInputIdle()。引用: 当父进程创建一个子 This should be an easy one: I am creating a program that spawns a process using the win32 Consider the following: The code above will (almost?) always fail; the window cannot be created and found that quickly. If I put a thread wait, say How can I improve this? (Edit): User IInspectable pointed out problems with (...) callback function used with the SetWindowsHookEx
function. The system calls this function before activating,
creating, (...) a window; (... many other things). Also, CBT is short for computer-based training, for whatever reason. (Old, beware, see comments.) You are looking for WaitForInputIdle(). Quote: When a parent process creates a child
process, the CreateProcess function
returns without waiting for the child
process to finish its initialization.
Before trying to communicate with the
child process, the parent process can
use the WaitForInputIdle function to
determine when the child's
initialization has been completed.
这篇关于如何确定生成的进程何时准备好? (使用CreateProcess()和FindWindow())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! WaitForInputIdle()
,并建议
功能。系统调用此函数之前激活,
创建,(...)一个窗口; (...许多其他事情)。
进程时,CreateProcess函数
返回而不等待子进程
进程完成初始化。
在尝试与
子进程通信之前,父进程可以
使用WaitForInputIdle函数到
确定子进程的
初始化完成。
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?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);
}
Sleep(1000)
, between the CreateProcess
and FindWindow
calls, it works fine. But this feels like a very bad hack.WaitForInputIdle()
, and suggested CBT Hooks instead.