Home > Blockchain >  How to run a file in c ?
How to run a file in c ?

Time:12-31

Code looks like:

#include <iostream>
#include <windows.h>
#include <stdio.h>

using namespace std;

int main(){
    string command;
    #define cfile
        ShellExecute(GetDesktopWindow(), "open", NULL, "file.exe" SW_SHOWNORMAL)

    cin >> command;
    
    if (command=="file");
    {
        cfile;
    }   
}

The file's directory is in the environment variables of Windows, so it should be fine running it by just "file.exe". I see this working for other people sometimes, however I have no clue why it doesn't work for me.

CodePudding user response:

Right off the bat, you're missing a comma between parameters, it should look like this:

// still wrong
#define cfile ShellExecute(GetDesktopWindow(), "open", NULL, "file.exe", SW_SHOWNORMAL)

Now that we got the most basic syntax problems fixed, other problems are:

  • You never set the parent of the process the desktop window, use a null parent instead.
  • You can use null as the verb if you're just opening a file anyway
  • However a null file makes no sense
  • You're also missing the working folder

So with those in mind, you should use this instead:

#define cfile ShellExecute(NULL, NULL, "file.exe", NULL, NULL, SW_SHOWNORMAL)
  • Related