下面是我写远程控制的时候用到的控制台程序定向输出到指定文件的代码。为了方便大家使用,我把代码写成了一个通用的函数,至于参数大家一看就明白,我就不解释了。
int Dxoutput(const wchar_t *filename,const wchar_t *runname,wchar_t *runparam)
{
STARTUPINFO si;
si.cb=sizeof(si);
GetStartupInfo(&si);
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle=TRUE;
sa.lpSecurityDescriptor=NULL;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
HANDLE hread=0,hwrite=0;
if(!CreatePipe(&hread,&hwrite,&sa,0))
return 0;
HANDLE hfile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,&sa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hfile==INVALID_HANDLE_VALUE)
{
return 0;
}
si.hStdOutput=hfile;
si.hStdError=hfile;
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
if(0==CreateProcess(runname,runparam,0,0,TRUE,0,0,0,&si,&pi))
{
CloseHandle(hfile);
CloseHandle(hread);
CloseHandle(hwrite);
return 0;
}
CloseHandle(hfile);
CloseHandle(hread);
CloseHandle(hwrite);
return 1;
}
阅读(883) | 评论(0) | 转发(0) |