PROCESS_INFORMATION piProcInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
bsuccess = ::CreateProcess(NULL,
TEXT("Client.exe"), // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
当断点走到最后一行时,报有未处理异常,是怎么回事呢?
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
bsuccess = ::CreateProcess(NULL,
TEXT("Client.exe"), // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
当断点走到最后一行时,报有未处理异常,是怎么回事呢?
解决方案 »
- operator HBITMAP();该如何解释?是什么作用?
- 急!一个问题!
- 程序调试用哪个工具?
- 关于数据插入的问题。。
- 初学者请教,散分! 怎样自动建立一个.mdb文件??
- 哪有msdn library 免费下载啊?
- 怎么获取鼠标单击的点的坐标并保存到文件当中?
- 用300分求助一个问题,该问题我在论坛上求助三次都没有得到很彻底的解决,希望这次可以~!·1
- 怎么用汇编语言读取指定端口数据(Windows 2000 profession),急死了!
- C++的不足之处讨论系列(一):虚拟函数(转贴)
- SR engine支持中文么?
- 在使用webbrowser时,能否通过DOM得到所有可见的页面文本?
#include <stdio.h>void main( VOID )
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
TEXT("MyChildProcess"), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
ZeroMemory(&siStartInfo, sizeof(siStartInfo));
siStartInfo.cb = sizeof(siStartInfo);
这个是必须设置的
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
STARTUPINFO siStartInfo;
PROCESS_INFORMATION piProcInfo;
SECURITY_ATTRIBUTES saAttr; //安全属性 saAttr.bInheritHandle = true; //可继承
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.lpSecurityDescriptor = NULL; //匿名PIPE,此属性没用 char readBuf[100];
memset(readBuf,'0',sizeof(readBuf)); DWORD readNum;
HANDLE hRead; //读句柄
HANDLE hWrite; //写句柄 BOOL bsuccess = ::CreatePipe(&hRead, &hWrite, &saAttr, 0); //创建匿名管道
if (bsuccess)
{
cout<<"管道创建成功"<<endl;
}
else
{
cout<<"管道创建失败"<<endl;
}
bsuccess = false; HANDLE hTemp = ::GetStdHandle(STD_OUTPUT_HANDLE); //获得本进程的标准输出 ::SetStdHandle(STD_OUTPUT_HANDLE, hWrite); //设置标准输出到匿名管道
::GetStartupInfo(&siStartInfo); //获取本进程的STARTUPINFO结构信息
//-------------------------------------------------------------------------
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = hWrite;
siStartInfo.hStdOutput = hWrite;
siStartInfo.hStdInput = hRead;
/*siStartInfo.dwFlags |= STARTF_USESTDHANDLES;*/
//-------------------------------------------------------------------------
//创建一个子线程
bsuccess = ::CreateProcess(NULL,
TEXT("Client.exe"), // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION ::SetStdHandle(STD_OUTPUT_HANDLE, hTemp); //恢复父线程的标准输出
::CloseHandle(hWrite); //关闭写端口,以便读取数据 while (ReadFile(hRead, readBuf, 100, &readNum, NULL)) //读取内容
{
readBuf[readNum] = '\0';
cout<<"从"<<readBuf<<"管道中读取数据字节数为:"<<readNum<<endl;
}
if (GetLastError() == ERROR_BROKEN_PIPE)
{
cout<<"管道进程被关闭"<<endl;
}
else
{
cout<<"读数据错误,错误代码为:"<<GetLastError()<<endl;
}
return 0;
}
管道能创建,就是启线程时有异常
这行怎么注释掉了?
是当前进程异常还是新启动的进程异常?
另外,CreateProcess的第2参数建议改成一个字符数组:
TCHAR cmdLine[100] = TEXT("Client.exe");
CreateFile(NULL, cmdLine, ……
CreateProcess的第2参数改成一个字符数组:
TCHAR cmdLine[100] = TEXT("Client.exe");
好用了,谢谢啊!