我现在遇到一个问题,我想在我的程序中,创建一个子进程,然后向子进程发送消息,等子进程执行完毕后关闭子进程。
我是用CreateProcess来创建子进程的,我想在创建的时候,能够得到子进程的句柄,这样我在对子进程进行操作时,要容易。
我用CreateProcess创建子进程后,返回aProcessInfo中的dwProcessId字段,函数原型如下:DWORD CreateProcessByName(CString& strFileName)
{
STARTUPINFO aStartInfo;
PROCESS_INFORMATION aProcessInfo;
BOOL aIsSuccess; char cCommandLine[MAX_PATH];
memset(cCommandLine,0,MAX_PATH);
strcpy(cCommandLine,strFileName); // Initialize the STARTUPINFO structure.
memset(&aStartInfo, 0, sizeof(aStartInfo));
aStartInfo.cb = sizeof(aStartInfo);
aStartInfo.wShowWindow = SW_MAXIMIZE; aIsSuccess = CreateProcess( NULL,
cCommandLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&aStartInfo, // pointer to STARTUPINFO
&aProcessInfo // pointer to PROCESS_INFORMATION
); if(aIsSuccess == FALSE){
return -1;
}
return aProcessInfo.dwProcessId;
}我发现该函数返回的子进程的ID跟我遍历系统进程池得到的ID并不一样,而且要命的是我并不能通过该函数返回的句柄来对子进程进行操作,相反却可以通过遍历进程池得到的那个ID对子进程进行发送消息,关闭子进程等操作。当我同时创建很多个子进程时,我刚刚创建的跟我遍历得到的ID,可能就不能一一对应了,比如有些子进程收到了两个重复的消息,有些子进程一个消息没收到,所以程序会出差。我想问的是,上述函数中,return aProcessInfo.dwProcessId;返回的,是不是新建子进程的ID?为什么跟我遍历进程池得到的ID不一样?我怎样得到正确的进程ID?
我是用CreateProcess来创建子进程的,我想在创建的时候,能够得到子进程的句柄,这样我在对子进程进行操作时,要容易。
我用CreateProcess创建子进程后,返回aProcessInfo中的dwProcessId字段,函数原型如下:DWORD CreateProcessByName(CString& strFileName)
{
STARTUPINFO aStartInfo;
PROCESS_INFORMATION aProcessInfo;
BOOL aIsSuccess; char cCommandLine[MAX_PATH];
memset(cCommandLine,0,MAX_PATH);
strcpy(cCommandLine,strFileName); // Initialize the STARTUPINFO structure.
memset(&aStartInfo, 0, sizeof(aStartInfo));
aStartInfo.cb = sizeof(aStartInfo);
aStartInfo.wShowWindow = SW_MAXIMIZE; aIsSuccess = CreateProcess( NULL,
cCommandLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&aStartInfo, // pointer to STARTUPINFO
&aProcessInfo // pointer to PROCESS_INFORMATION
); if(aIsSuccess == FALSE){
return -1;
}
return aProcessInfo.dwProcessId;
}我发现该函数返回的子进程的ID跟我遍历系统进程池得到的ID并不一样,而且要命的是我并不能通过该函数返回的句柄来对子进程进行操作,相反却可以通过遍历进程池得到的那个ID对子进程进行发送消息,关闭子进程等操作。当我同时创建很多个子进程时,我刚刚创建的跟我遍历得到的ID,可能就不能一一对应了,比如有些子进程收到了两个重复的消息,有些子进程一个消息没收到,所以程序会出差。我想问的是,上述函数中,return aProcessInfo.dwProcessId;返回的,是不是新建子进程的ID?为什么跟我遍历进程池得到的ID不一样?我怎样得到正确的进程ID?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货