tags:

views:

47

answers:

1

Hi, 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

+7  A: 

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",
Pavel Minaev
Hi, I teste you sugestion but I wanna know why to use CreateProcessA instead CreateProcess.
Leandro
And, thanks, it works very well.
Leandro
`CreateProcessA` is a version that takes `LPCSTR` (i.e. `const char*`) as the first argument. `CreateProcessW` is another version that takes `LPCWSTR` (i.e. `const wchar_t*`). In Windows headers, there's a `#define CreateProcess CreateProcessA` or `#define CreateProcess CreateProcessW`, chosen depending on whether you compile with Unicode enabled (i.e. `/D_UNICODE`) or not. The default for new C++ projects in Visual Studio is Unicode enabled. Also, it is better to use `L""` over `CreateProcessA`, because Unicode APIs are native on WinNT family, and ANSI APIs convert to Unicode and call `...W`.
Pavel Minaev
What's the diference between run my project with debug and release? Debug doesn't work.
Leandro
"doesn't work" is an error-description that is nobody telling anything.
Georg Fritzsche
@Leandro: ask it as a separate question.
Pavel Minaev