您的位置:首页 > 其它

在vc中多次调用dos命令,循环使用CreateProcess的

2010-07-16 07:09 369 查看
HANDLE hProcess=GetCurrentProcess();
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=TRUE;
sa.lpSecurityDescriptor=NULL;

HANDLE hStdinRd, hStdinWr, hStdinWrDup;
HANDLE hStdoutRd, hStdoutWr,hStdoutRdDup;

//** 创建读写管道
BOOL rc;
rc=CreatePipe(&hStdoutRd, &hStdoutWr, &sa, 0);
if(!rc) return -1;
rc=CreatePipe(&hStdinRd, hStdinWr, &sa, 0);
if(!rc) return -1;

//** hStdout管道的读取端需保留在本进程,不能继承给子进程,需要复制出一个不可继承的句柄
rc=DuplicateHandle(hProcess, hStdoutRd, hProcess, &hStdoutRdDup, 0, FALSE, DUPLICATE_SAME_ACCESS);
CloseHandle(hStdoutRd);

//** hStdin管道的写入端需保留在本进程,不能继承给子进程,需要复制出一个不可继承的句柄
rc=DuplicateHandle(hProcess, hStdinWr, hProcess, &hStdinWrDup, 0, FALSE, DUPLICATE_SAME_ACCESS);
CloseHandle(hStdinWr);

PROCESS_INFORMATION pi={0};
STARTUPINFO si={sizeof(STARTUPINFO};
si.dwFlags=STARTF_USESTDHANDLES;
si.hStdInput=hStdinRd;
si.hStdOutput=hStdoutWr;
si.hStdError=hStdoutWr;

rc=CreateProcess( "cmd.exe ", NULL, NULL, 0, TRUE, 0, NULL, NULL, &si, &pi);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

//**将所有命令存到一个字符串中,前后二个命令中间以/r/n分隔。
TCHAR szCmds[4096]=TEXT( "dir/r/nattrib/r/n "); //命令间以/r/n分隔
DWORD dwWrite=lstrlen(szCmd);
//** 从复制后的hstdinWrDup管道端写入dos命令
WriteFile(hStdinWrDup, szCmds, dwWrite, &dwWrite, NULL);

TCHAR buffer[64*1024+1];
DWORD dwBuffer=sizeof(buffer);

//** 从复制后的hstdoutRdDup管道端读取输出结果
ReadFile(hStdoutRdDup, buffer, dwBuffer, &dwBuffer, NULL);

---------------------

我改用WinExec之后速度提高了一点点,45秒左右,但是还是太慢了,呵呵
chehw(chehw)我用你的方法,执行CreateProcess( "cmd.exe ", NULL, NULL, 0, TRUE, 0, NULL, NULL, &si, &pi);时不成功,改成CreateProcess( "cmd.exe ", NULL, NULL, NULL, NULL, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &si, &pi);也不行,什么问题?

------

CreateProcess( "c://windows//system32//cmd.exe ",

-------

BOOL LaunchExe::PipeResult( const CString& CommandLine, const CString& Parameters,
CString& strOutFile )
{
STARTUPINFO si;
memset( &si, 0, sizeof( si ) );
si.cb = sizeof( si );
// Hide DOS window
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
// Prepare pipe handles for standard output redirection
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
HANDLE hReadPipe, hWritePipe;
BOOL res = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 );
if ( !res )
{
return res;
}
else
{
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdOutput = hWritePipe;
si.hStdInput = hWritePipe;
si.hStdError = hWritePipe;
PROCESS_INFORMATION pi;
CString sFullCommand = "/ " " + CommandLine + "/ " " + Parameters;
res = CreateProcess( NULL, (LPTSTR)(LPCTSTR)sFullCommand, NULL, NULL, TRUE,
0, NULL, NULL, &si, &pi );
if (!res)
{
// process error of rsh
return res;
}
else
{
// Close write pipe
CloseHandle( hWritePipe );
DWORD NumberOfBytesRead;
TCHAR Buffer[ 257 ];
while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,
&NumberOfBytesRead, NULL ) )
{
if (NumberOfBytesRead)
{
Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
strOutFile += Buffer;
}
else
break;
};
// Close read pipe and wait for the finish
CloseHandle( hReadPipe );
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle (pi.hProcess);
res = TRUE;
}
}
return res;
}

输出信息在strOutFile,把他读到editbox或者直接输出到editbox

------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: