ShellExecute syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbrandt
    New Member
    • Aug 2007
    • 2

    ShellExecute syntax

    HINSTANCE hInst = ShellExecute(0,
    "open",
    "iexplore.e xe",
    Open,
    0,
    SW_SHOWNORMAL);

    After reading multiple articles, the above syntax should be the proper way to execute a shell, shouldn't it? But how come in the final line, ("SW_SHOWNORMAL "), it gives me this error:

    error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The error is not on the final line, it is reported on that line because it is the last line of the statement.

    If you read the error message you can seen that the error is with parameter 2 which is "open" which also happens to be const char [5] as reported.

    The compiler can not covert type const char [5] to LPCWSTR. This is because LPCWSTR is basically defined as const wchar_t * and the const char [5] is treated as const char *, these are pointers to incompatible types.

    ShellExecute has 2 versions, ANSI and UNICODE strings. You have compiled your code for UNICODE but are supplying ShellExecute an ANSI string "open".

    If you compile your code for ANSI then the error will go away because ShellExecute will expect LPCSTR which is const char *. Alternitively you will need to convert your strings to UNICODE.

    Comment

    Working...