在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
------------------
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
------------------
相关文章推荐
- 使用C#调用外部序或是执行DOS命令
- 将QT开发的界面程序封装成DLL,在VC中成功调用(必须有消息循环,所以使用了QTWinmigrate,附CSDN可下载的Demo)
- 使用C#调用Ping命令获取网络连接情况 + 获取DOS返回值
- c++调用shell 或者dos命令—使用system函数
- 用VC调用DOS命令来实现PING功能
- 使用ShellExW.dll中的ShellExecWait函数同步调用DOS命令
- c++调用shell 或者dos命令—使用system函数
- 对C++语言中dos命令的调用及system()函数使用方法的探究
- 在VC++程序中调用DOS命令例子
- 使用C#调用外部序或是执行DOS命令
- C++ 中的DOS命令调用(3)——我不提倡大量使用DOS命令
- DOS循环:bat/批处理for命令详解之一 (史上虽详尽的总结和说明~~)
- Shell脚本基础16-while循环中使用sleep命令
- Linux下使用python调用top命令获得CPU利用率
- 实验一、DOS使用命令实验
- 使用 Linux 系统调用的内核命令
- vc中调用其他应用程序的方法(函数) winexec,shellexecute ,createprocess
- java 调用 bat 如果里面用了第三方命令 dos 窗口没有关闭 解决方法
- DOS的MD和RD命令使用说明
- Windows下使用DOS命令进入MySQL数据库