Search code examples
c++shellexecute

Why does ShellExecute not work?


#include "ShellAPI.h";
ShellExecute(Handle,NULL,"file.txt",NULL,NULL,SW_RESTORE);

Why does this code not work?

Here the error screen: a busy cat


Solution

  • Several problems:

    • You need to include windows.h before you can include shellapi.h.
    • When including system headers you should use <> around the header instead of "".
    • You should not have a semicolon ; after your #include directive.
    • Your ShellExecute() call needs to be in a function, probably int main(void).
    • Handle is not defined. You probably want NULL per the ShellExecute() documentation (which you should have already read).

    #include <windows.h>
    #include <ShellAPI.h>
    
    int main(void) {
        ShellExecute(NULL, NULL, "file.txt", NULL, NULL, SW_RESTORE);
        return 0;
    }