// 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 (! Sethandleinformation (hreadpipe, handle_flag_inherit, 0 ))
{
Log (error <"sethandleinfomation fail .");
}
String stroutfile;
If (! Res)
{
Return res;
}
Else
{
Startupinfo Si;
Memset (& Si, 0, sizeof (SI ));
Si. cb = sizeof (SI );
Getstartupinfo (& Si );
// Hide DOS window
Si. dwflags = startf_useshowwindow;
Si. wshowwindow = sw_hide;
Si. dwflags = startf_useshowwindow | startf_usestdhandles;
Si. hstdoutput = hwritepipe;
Si. hstdinput = hreadpipe;
Si. hstderror = hwritepipe;
Process_information PI;
Zeromemory (& Pi, sizeof (process_information ));
Wstring sfullcommand = l "WMIC diskdrive list ";
Res = CreateProcess (null, (lptstr) (lpctstr) sfullcommand. c_str (), null, null, true,
0, null, null, & Si, & PI );
If (! Res)
{
// Process error of RSH
Log (error <"CreateProcess fail .");
Return err_error;
}
Else
{
// Close write Pipe
Closehandle (hwritepipe );
DWORD numberofbytesread;
Char buffer [257];
While (: readfile (hreadpipe, buffer, sizeof (buffer)/sizeof (buffer [0])-1,
& Numberofbytesread, null ))
{
Log (info <buffer <"numberofbytesread:" <numberofbytesread );
If (numberofbytesread)
{
Buffer [numberofbytesread] = tchar (0 );
Stroutfile + = buffer;
}
Else
Break;
};
Log (info <"lasterror:" <getlasterror ());
// Close read pipe and wait for the finish
Closehandle (hreadpipe );
Waitforsingleobject (PI. hprocess, infinite );
Closehandle (PI. hprocess );
Res = true;
}
}
Refer:
Http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499 (V = vs.85). aspx