sochinho
sochinho

Reputation: 87

Execute command using Win32

I would like to execute shell command to update firmware to my procesor ATMega 2560 like this:

avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"file.cpp.hex\":a

I can do it by 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 I want to redirect output buffer, so I think I should use CreateProcess() function. I tried this but it 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);

Upvotes: 2

Views: 10086

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598134

Use CreateProcess() instead of ShellExecute(), and provide your own pipes so you can read the process's output. MSDN has an article on that topic:

Creating a Child Process with Redirected Input and 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 from hStdOutRd and hStdErrRd as needed until the process is terminated...

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}

CloseHandle(hStdOutRd);
CloseHandle(hStdOutWr);
CloseHandle(hStdErrRd);
CloseHandle(hStdErrWr);

Upvotes: 6

sochinho
sochinho

Reputation: 87

Problem was resolved! Full code:

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;

if (!CreateProcess(L"C:\\Windows\\System32\\cmd.exe",
    L" /C avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"file.cpp.hex\":a",
    NULL, NULL, 0, 0, NULL, NULL, &si, &pi))    
{
    printf("CreateProcess failed (%d).\n", GetLastError());
    return -1;
}

Upvotes: 2

Related Questions