I am wrapping the Windows API CreateProcess.

Here is my code.

int __stdcall _Execute(LPTSTR program, LPTSTR workingdir, WORD show){

    // Check if program has content
    if (!program || !*program){
        MessageBox(NULL, L"You must provide path to executable", L"Error", MB_OK);
        return 0;
    }

    // Create buffers and copy program param to szCmd buffer
    TCHAR szDir[MAX_PATH + 1], szCmd[MAX_PATH + 1];
    wcscpy_s(szCmd, program);

    // Check if workingdir has content
    if (!workingdir || !*workingdir){
        // If not, use current dir
        GetCurrentDirectoryW(sizeof(szDir), szDir);
    }else{
        // If so, use it
        wcscpy_s(szDir, workingdir);
    }

    // Create structs
    PROCESS_INFORMATION     pi;
    STARTUPINFO         si;

    // zero and fill structs
    //TODO - check std flags.
    SecureZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = show;
    SecureZeroMemory(&pi, sizeof(pi));

    // Create the process or return fail
    if (!CreateProcess(NULL, szCmd, NULL, NULL, TRUE, 0, NULL, szDir, &si, &pi))
    {
        return 0;
    }

    // Close thread and return win
    CloseHandle(pi.hThread);
    return 1;
}

If I call it like this for example...

_Execute("C:\windows\notepad.exe C:\file.txt", "C:\windows\system32", SHOW);

Notepad will startup and load file.txt.

But if I try...

_Execute("C:\Windows\System32\cmd.exe ping google.co.uk", "C:\windows\system32", SHOW);

I only get my command prompt open and set at system dir.
I expected it to run the ping command.

I'm hoping for hints and tips as to my mistakes.

If you want to run a command in a command prompt you need to use the /c or /k switches before the command line, i.e. "C:\Windows\System32\cmd.exe /c ping google.co.uk"

Try cmd /? at a command prompt

Great, thanks, I'm not very familiar with command line.
Appreciate your time.

I'm not going to mark this as solved yet although you solved my initiall question as I'll probably be back before I've finished wrapping the whole thing.

Thanks Banfa.

Next part of project is reading the std output from that ping command.

I've fell at the first hurdle though.

I think I need to use API GetStdHandle()

HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);

msdn says...

If the function succeeds, the return value is a handle to the specified device, or a redirected handle set by a previous call to SetStdHandle. The handle has GENERIC_READ and GENERIC_WRITE access rights, unless the application has used SetStdHandle to set a standard handle with lesser access.

If the function fails, the return value is INVALID_HANDLE_VALUE.

But I get an error in IDE (vs2010)...

error C2090: function returns array

I find very little about this in web search.

Any ideas why this occurs?

You could pipe the cmd text to a file and then parse that file for the information that you need.

This is slightly stretching my, now 10 year out of date, recolection of WIN API programming but ...

I do not think GetStdHandle is likely to do what you want (with the caveat that of course I have no idea what you want so maybe it does) since it returns the standard handle for the current process but you want to set the stand output handle of your created process.

I seem to remember doing this by creating a pipe (CreatePipe) before creating the process and setting this as the std out handle in the STARTUPINFO structure passed to the process. Then you can read (and parse) the started applications output from the pipe.

Duh! What a dummie I am, just found
#define stdout (&__iob_func()[1]) in stdio.h

Thanks for suggestion NathanOliver.

Banfa, what you described is exactly what I'm after.

The caller of my function should be able to read the std output from the supplied program after its creation.

I will take my first steps into pipe creation, I was hoping that the caller might accomplish that from the process pid if I return it.

Thank you very much again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.