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      
                    
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    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

    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 4 Replies

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().

It worked, thanks.

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());
    STARTUPINFO sInfo;
    PROCESS_INFORMATION pInfo;
    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();
        CloseHandle(pInfo.hProcess);
    }
    return (id);
}

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

int main(){
    std::cout<<procOpen("notepad");
    // or std::cout<<procOpen("C:\\Windows\\notepad.exe");
}
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.