0
#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

Community
  • 1
  • 1
GuitarFan
  • 79
  • 2
  • 9
  • Maybe because you need to put your `ShellExecute()` invocation inside of `int main(void) { }`? There are also errors inside of `ShellAPI.h` but since you didn't paste the contents of that file we can't help you with it. – cdhowie Dec 19 '14 at 23:31
  • 4
    [Did you just ask this question under a different account?](http://stackoverflow.com/questions/27574239/why-shellexecute-doesnt-work) – Blastfurnace Dec 19 '14 at 23:32
  • I've tried that, same error. I I thought that ShellAPI.h is standard. Tell me what i need to give you and i'll give you that. – GuitarFan Dec 19 '14 at 23:34
  • 1
    Why does someone put a minus ? This is a normal question! – GuitarFan Dec 19 '14 at 23:38
  • @GuitarFan Perhaps you meant `#include `. You may also need to `#include ` first. – cdhowie Dec 19 '14 at 23:39
  • cdhowie, thank you for your answer. It helps, but now error C2065: 'Handle' : undeclared identifier I don't know the handle. Handle is for some specific window, but i just try to run txt file. int main(void) { ShellExecute(Handle,NULL,"file.txt",NULL,NULL,SW_RESTORE); } – GuitarFan Dec 19 '14 at 23:50
  • @GuitarFan You would do yourself a favor to [read the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx). *"This value can be NULL if the operation is not associated with a window."* – cdhowie Dec 19 '14 at 23:55
  • 1
    From the confusion in the question, I would recommend that you learn C++ first, and then expand to `ShellExecute` later. – Raymond Chen Dec 20 '14 at 00:10

1 Answers1

8

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;
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Big thank you, but still, i have another error with your code: 1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup 1>C:\Users\Parents\Desktop\hide3\hide3\Debug\hide3.exe : fatal error LNK1120: 1 unresolved externals Do you know what is that mean? – GuitarFan Dec 20 '14 at 00:25
  • Here is my function: int main(void) { ShellExecute(NULL, NULL, L"file.txt", NULL, NULL, SW_RESTORE); return 0; } – GuitarFan Dec 20 '14 at 00:26
  • The error means you originally created a "Win32 project" but you should have created a "Win32 console project". – ScottMcP-MVP Dec 20 '14 at 00:31
  • Can not I just create a Win32 project? Why then some people tell me to use main function? ShellExecude doesn't work in win32 project? – GuitarFan Dec 20 '14 at 00:40
  • @GuitarFan "Win32 project" is for programs with a window. "Win32 console project" is for programs that will run on the console (in a command prompt). They both have access to the same general stuff (console programs can actually create windows!) but they have a different signature for the entry point. – cdhowie Dec 20 '14 at 02:12