C语言如何获取 调用的外部程序 标准输出的内容
这个是在网上搜索的,但还是获取不到。
那位大神给改一下,好久没写C了
int GetRun()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE outHandle;
outHandle = CreateFile(L"aa.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, CREATE_ALWAYS, 0, NULL);
PROCESS_INFORMATION processInfo;
STARTUPINFO startUpInfo;
memset(&startUpInfo, 0, sizeof(STARTUPINFO));
memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.dwFlags = STARTF_USESTDHANDLES;
startUpInfo.wShowWindow = SW_SHOWNORMAL;
startUpInfo.hStdOutput = outHandle;
if (!CreateProcess(L"Coreinfo.exe", NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL,
&startUpInfo, &processInfo)){
CloseHandle(outHandle);
printf("open hello.exe error\n");
}
else
{
DWORD filesize = 1000;
char * buffer = (char *)malloc(filesize + 1);
memset(buffer, 0, filesize + 1);
DWORD readsize;
ReadFile(outHandle, buffer, filesize, &readsize, NULL);
buffer[filesize] = 0;
printf("1111111 - %s\n", buffer);
free(buffer);
WaitForSingleObject(processInfo.hProcess, INFINITE);
printf("open hello.exe ok\n");
CloseHandle(outHandle);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用匿名管道就可以,CreateFile是产生命名管道的,而且文件名也不正确。请搜索匿名管道和命名管道即可。
popen
``
Creating a Child Process with Redirected Input and Output