2 kiwiw3 kiwiw3 于 2013.10.01 10:32 提问

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("%s\n",buffer);
free(buffer); 


WaitForSingleObject(processInfo.hProcess,INFINITE);
printf("open hello.exe ok");


CloseHandle(outHandle); 


CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);

}


return 0;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!