问题描述
- C语言,怎样实现获取调用其他程序的输入输出?
-
各位,我的目的是获取调用其他程序hello.exe输出的信息。网上查找一下代码,但是实现不了!!为什么呢?
我选择用LCC-win32为编译器。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> int main(int argc,char *argv[]) { SECURITY_ATTRIBUTES sa; sa.nLength=sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor=NULL; sa.bInheritHandle=TRUE; HANDLE outHandle; outHandle = CreateFile("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("hello.exe",NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startUpInfo, &processInfo)){ CloseHandle(outHandle); printf("open hello.exe error"); }else{ DWORD filesize=1000; char * buffer = malloc(filesize + 1); memset(buffer, 0, filesize+1); DWORD readsize; ReadFile(outHandle,buffer,filesize,&readsize,NULL); printf("%sn",buffer); free(buffer); WaitForSingleObject(processInfo.hProcess,INFINITE); printf("open hello.exe ok"); CloseHandle(outHandle); CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); } return 0; }
时间: 2025-01-27 00:39:31