c++ - Execute command using Win32 -
i execute shell command update firmware procesor atmega 2560 this:
avrdude.exe -c breakout -p ft0 -p m2560 -u flash:w:\"file.cpp.hex\":a
i can shellexecute() function:
shellexecute(0, l"open", l"cmd.exe", l"/c avrdude.exe -c breakout -p ft0 -p m2560 -u flash:w:\"file.cpp.hex\":a > log.txt", 0, sw_hide);
but want redirect output buffer, think should use createprocess() function. tried hasn't worked.
createprocess(null, l"cmd /c avrdude.exe -c breakout -p ft0 -p m2560 -u flash:w:\"file.cpp.hex\":a", null, null, 0, 0, null, null, null, null);
use createprocess()
instead of shellexecute()
, , provide own pipes can read process's output. msdn has article on topic:
creating child process redirected input , output
for example:
lpwstr cmdline[] = l"avrdude.exe -c breakout -p ft0 -p m2560 -u flash:w:\"file.cpp.hex\":a"; security_attributes sa = {0}; sa.nlength = sizeof(sa); sa.lpsecuritydescriptor = null; sa.binherithandle = true; handle hstdoutrd, hstdoutwr; handle hstderrrd, hstderrwr; if (!createpipe(&hstdoutrd, &hstdoutwr, &sa, 0)) { // error handling... } if (!createpipe(&hstderrrd, &hstderrwr, &sa, 0)) { // error handling... } sethandleinformation(hstdoutrd, handle_flag_inherit, 0); sethandleinformation(hstderrrd, handle_flag_inherit, 0); startupinfo si = {0}; si.cbsize = sizeof(si); si.dwflags = startf_usestdhandles; si.hstdinput = getstdhandle(std_input_handle); si.hstdoutput = hstdoutwr; si.hstderror = hstderrwr; process_information pi = {0}; if (!createprocessw(null, cmdline, null, null, true, 0, null, null, &si, &pi)) { // error handling... } else { // read hstdoutrd , hstderrrd needed until process terminated... closehandle(pi.hthread); closehandle(pi.hprocess); } closehandle(hstdoutrd); closehandle(hstdoutwr); closehandle(hstderrrd); closehandle(hstderrwr);
Comments
Post a Comment