Reputation: 327
I have a problem with my main program to which after all the calls I make after running the program it just opens then closes. It doesn't find any error in my code or anything, so I tried creating a new project and it does the same thing with no error.
I even tried example code:
int APIENTRY WinMain (HINSTANCE ...)
ShellExecute(NULL, (LPCWSTR)"open", (LPCWSTR)"cmd.exe", NULL, NULL, SW_SHOW);
and it still just opens and closes. What is the issue here? I can provide the output log if necessary but I have no idea.
Upvotes: 0
Views: 136
Reputation: 612954
I'm not sure I understand what you mean about opening and closing. But this code is very wrong:
ShellExecute(NULL, (LPCWSTR)"open", (LPCWSTR)"cmd.exe",
NULL, NULL, SW_SHOW);
The casts are no good. The two string literals are pointers to arrays of char
casting them to const wchar_t*
doesn't change what they are. It just asks the compiler to trust that you know better than it does. You don't. They are really not const wchar_t*
. All your cast achieves is to allow you to break type safety and convert an informative compile time error into a cryptic run time failure. Until you have a deep understanding of the language you should refrain from casting.
You need to use wide literals:
ShellExecute(NULL, L"open", L"cmd.exe", NULL, NULL, SW_SHOW);
You also appear to be doing no error checking. That's quite hard with ShellExecute
. Try ShellExecuteEx
instead and take heed of the value it returns.
Upvotes: 5