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;
// ** Create a read/write Pipeline
Bool RC;
Rc = createpipe (& hstdoutrd, & hstdoutwr, & SA, 0 );
If (! RC) Return-1;
Rc = createpipe (& hstdinrd, hstdinwr, & SA, 0 );
If (! RC) Return-1;
// ** The read end of the hstdout pipeline must be retained in this process and cannot be inherited from the sub-process. A non-inherited handle must be copied.
Rc = duplicatehandle (hprocess, hstdoutrd, hprocess, & hstdoutrddup, 0, false, duplicate_same_access );
Closehandle (hstdoutrd );
// ** The write end of the hstdin pipeline must be retained in the current process and cannot be inherited from the child process. A non-inherited handle must be copied.
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 );
// ** Save all commands to one string, and separate the two commands with/R/n.
Tchar szcmds [4096] = text ("DIR/R/nattrib/R/N"); // commands are separated by/R/n.
DWORD dwwrite = lstrlen (szcmd );
// ** Write the DOS command from the hstdinwrdup pipe after Replication
Writefile (hstdinwrdup, szcmds, dwwrite, & dwwrite, null );
Tchar buffer [64*1024 + 1];
DWORD dwbuffer = sizeof (buffer );
// ** Read the output result from the copied hstdoutrddup Pipe
Readfile (hstdoutrddup, buffer, dwbuffer, & dwbuffer, null );
---------------------
After I switched to winexec, the speed improved a little bit, about 45 seconds, but it was still too slow.
Chehw (chehw) I use your method to execute CreateProcess ("cmd.exe", null, null, 0, true, 0, null, null, & Si, & PI ); if it fails, it is changed to CreateProcess ("cmd.exe", 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;
}
The output information is in stroutfile and can be read to editbox or directly output to editbox.
------------------