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);
}
}