0

I want to start a .exe program that is in my computer after a run my code and still do some stuff after the program opens but I'm stuck in how to open it.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define _WIN32_WINNT 0x0500
#include <windows.h>

int main () {
   POINT mouse;
   //HWND hWnd = GetConsoleWindow();
   //ShowWindow(hWnd, SW_MINIMIZE); 
   //ShowWindow(hWnd, SW_HIDE);
   // how to open program ?
   system("start C:\Riot Games\League of Legends\LeagueClient.exe");

   while (true) {
      GetCursorPos(&mouse);
      int x = mouse.x;
      int y = mouse.y;
      SetCursorPos(x + rand() % 40 - 20, y + rand() % 40 - 20);
      printf("x = %d ", mouse.x);
      printf("y = %d\n", mouse.y);
      Sleep(1);
   }
}

The system function doesn't work for me for two reasons; it pauses the code until the app is exit, and when I try to run the code it says he can't find C:Riot.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Lukas Belck
  • 30
  • 2
  • 8
  • The `system()` function waits for the executed command to complete. On Linux or Mac you would either `fork()` or run a command that starts your external executable in the background. But Windows does not have `fork()`, and I'm not sure it has background processes as such, either. You'll need an appropriate function from the Windows API. – John Bollinger Feb 11 '17 at 17:41
  • ohhh i see, thanks. I will try to find it. – Lukas Belck Feb 11 '17 at 18:04

2 Answers2

2

The are several problems is the string "start C:\Riot Games\League of Legends\LeagueClient.exe".

First of all, the \ character is used to escape characters, which means enter characters that would mean something else if they were inserted directly in the string. For example, to write " in a string, you should use \" since " on its own would mean that it's the end of the string. Similarly, \n means a line break since you can't write a line break directly in the string. Here, \R in C:\Riot Games means that you are escaping the character R which doesn't mean anything. The compiler interprets \R as a simple R (the same holds for \L) and therefore converts the string "start C:\Riot Games\League of Legends\LeagueClient.exe" to "start C:Riot GamesLeague of LegendsLeagueClient.exe". To escape the \ character, use \\. So so far, the string should be system("start C:\\Riot Games\\League of Legends\\LeagueClient.exe").

There is also another problem in that string, it's that a space in a command line usually means that you run the program with the path before the space with the parameters specified after the space. This is usually used to open a file with a program. To make it simple, "start C:\\Riot Games\\League of Legends\\LeagueClient.exe" means that you want to run the program C:\Riot and make it open the file Games\League of Legends\LeagueClient.exe. And as we said previously, the compiler turns the C:\Riot in your code to C:Riot, so it's trying to run the program C:Riot and of course it can not find it, so it gives you the error that you mentioned. Anyway, to tell the computer that the spaces are actually part of the file name, you have to put quotation marks " around the file name. As I said previously, to use quotation marks in strings, use \". So the correct way to do would be system("start \"C:\\Riot Games\\League of Legends\\LeagueClient.exe\""). Also, start opens the console, so if you would like to open the program itself, use simply system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"").

Another problem with your code is that true isn't defined in C. So you should either use while(1) or define true as a macro using #define true 1.

So a correct code would be the following:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define _WIN32_WINNT 0x0500
#include <windows.h>

#ifndef true    //test if true is defined in case it's already defined somewhere else
#define true 1
#endif

int main () {
    POINT mouse;
    //HWND hWnd = GetConsoleWindow();
    //ShowWindow(hWnd, SW_MINIMIZE); 
    //ShowWindow(hWnd, SW_HIDE);
    // how to open program ?
    system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"");

    while (true) {
        GetCursorPos(&mouse);
        int x = mouse.x;
        int y = mouse.y;
        SetCursorPos(x + rand() % 40 - 20, y + rand() % 40 - 20);
        printf("x = %d ", mouse.x);
        printf("y = %d\n", mouse.y);
        Sleep(1);
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • It worked thanks! But when i tried system(notepad) for testing it opened the notepad and stoped the program until i close the notepad but with the LeagueClient.exe for some reason it didn't stop the program any idea why ? just curious. – Lukas Belck Feb 11 '17 at 19:09
0

Use system() is not secure and good way to create process is CreateProcess() function. Other thing - system() waits until launched programm stopped, and do code after not parallel.

drem1lin
  • 331
  • 1
  • 3
  • 14