How To Start Another Executable.

I have some code here that is supposed to open cmd:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <windows.h>

int main(void)
{
	LPCTSTR lpApplicationName = "C:/Windows/System32/cmd.exe"; /* The program to be executed */

	LPSTARTUPINFO lpStartupInfo;
	LPPROCESS_INFORMATION lpProcessInfo;

	memset(&lpStartupInfo, 0, sizeof(lpStartupInfo));
	memset(&lpProcessInfo, 0, sizeof(lpProcessInfo));
              
	/* Create the process */
	if (!CreateProcess(lpApplicationName, NULL, NULL, NULL, NULL, NULL, NULL, NULL, lpStartupInfo, lpProcessInfo))
	{
	    std::cerr << "Uh-Oh! CreateProcess() failed to start program \"" << lpApplicationName << "\"\n";
	    exit(-1);
	}
	
	std::cout << "Started program \"" << lpApplicationName << "\" successfully\n";
std::cin.ignore();
return(0);
}


The problem is, it crashes right around the CreateProcess(...) part.
Any idea why?
Last edited on
Anything? I have been researching and cannot find anything.
Tried debugging it? Also Windows may not handle the path with forward slashes, try changing them all too \\
Also Windows may not handle the path with forward slashes, try changing them all too \\


Windows handles forward slashes just fine.

What do you think LP signifies in LPSTARTUPINFO and LPPROCESS_INFORMATION?

Why are you using NULL for nonoptional non-pointer parameters to CreateProcess? Did you bother looking up the function?
You are not allocation memory for PROCESS_INFORMATION and STARTUPINFO structures, that's why it crashes, you only allocate a pointer to these structures, but not structures itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>

int main(void)
{
	LPCTSTR lpApplicationName = "C:/Windows/System32/cmd.exe"; /* The program to be executed */

	STARTUPINFO lpStartupInfo;
	PROCESS_INFORMATION lpProcessInfo;

	memset(&lpStartupInfo, 0, sizeof(lpStartupInfo));
	memset(&lpProcessInfo, 0, sizeof(lpProcessInfo));
              lpStstupInfo.cb = sizeof (STARTUPINFO);  // required
              
	/* Create the process */
	if (!CreateProcessA(lpApplicationName, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lpStartupInfo, &lpProcessInfo))
	{
	    std::cerr << "Uh-Oh! CreateProcess() failed to start program \"" << lpApplicationName << "\"\n";
	    exit(-1);
	}
	
	std::cout << "Started program \"" << lpApplicationName << "\" successfully\n";
std::cin.ignore();
return(0);
}




The code is NOT tested, could still have errors in it.
Got it thanks guys. For those who want fixed code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

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

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return (0);
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // 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() );
        return (0);
    }

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

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}
Topic archived. No new replies allowed.