Hello everyone,

I was working on a program that was meant to open something the user put in. Basically, the user would put in the path and then the program would open whatever was there. I can't work it though, every time I put in the file path, it says "CreateProcess failed (2)". Anyways, here is the code, and thanks in advance for the help.

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <string>
#include <windows.h>
#include <fstream>
#include <tchar.h>

using namespace std;
    int main()
        cout << "What would you like to open?";
        char way[50];
        cin >> way[50];
        char *path = way;
    // the createprocess code starts here      

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        path,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
        printf( "CreateProcess failed (%d).\n", GetLastError() );

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    // createprocess code ends here

    return EXIT_SUCCESS;

Edited 6 Years Ago by some: n/a

Error code 2 is ERROR_FILE_NOT_FOUND. If the path you entered contains spaces, the statement

cin >> way[50];

will only read the first "word" of the path. Use getline().

So.........what is the solution please? For a mere mortal, these postings do not offer any solution !!!

Well, it's not hard to figure that yourself, given the present code...

DWORD WINAPI procOpen(std::string path){
    LPSTR run=const_cast<char *>(path.c_str());
    ZeroMemory(&sInfo, sizeof(sInfo));
    sInfo.cb = sizeof(sInfo);
    ZeroMemory(&pInfo, sizeof(pInfo));
    DWORD id;
    if (CreateProcess(NULL, run, NULL, NULL, false, 0, NULL, NULL, &sInfo, &pInfo)) {
        id = GetCurrentProcessId();
    return (id);

This, for example, will try to open the program indicated by the argument path.
Here's a simple example:

int main(){
    // or std::cout<<procOpen("C:\\Windows\\notepad.exe");

Edited 3 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.