Leandro Lima
Leandro Lima

Reputation: 1170

Program stop to running when calls a process

I am trying to create a program that calls another process using CreateProcess. After some problems, I change my program to just open a known program:

if( !CreateProcess( (LPWSTR)"C:\\Program Files\\Opera\\Opera.exe",   // No module name (use command line)
    NULL,                       ,
            // 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
) 

I found this example in msdn, but every time I run my program, windows (Vista) shows a error msg: The program stop running...

Does anybody know what is the problem?

Regards, Leandro Lima

Upvotes: 0

Views: 460

Answers (1)

Pavel Minaev
Pavel Minaev

Reputation: 101555

This line is wrong:

(LPWSTR)"C:\\Program Files\\Opera\\Opera.exe"

LPWSTR is a typedef for wchar_t*. So you're casting a plain string (array of chars, which will decay to a const char*) to a wchar_t*. The end result is likely not even null-terminated!

Either use CreateProcessA and drop the cast, or use a wide string:

 L"C:\\Program Files\\Opera\\Opera.exe",

Upvotes: 7

Related Questions