How to Execute Another Exe from a C++ Program in Windows

How to execute another exe from a C++ program in Windows

you can use the system function

int result = system("C:\\Program Files\\Program.exe");

How to execute an external program in c

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

Execute a C program from within another C program as if it was a function call (in Windows)?

What you describe, is the basic premise of the Unix operating system. Unix was designed to allow accomplishing very complex tasks by chaining several commands, piping the (text) output of a command as the input of the next one (this was pretty revolutionary back then).

As klutt already suggested, you can accomplish the same with a Windows executable. To his list, I would add learning how to redirect the input/output of a program to a file handle.

The Windows PowerShell extended this concept to allow passing different data-types other than text, to some special executables known as cmdlets, however, to write your own, you need support from the .Net Framework or the .Net Core infrastructure, so you must do so from a managed language such as C# or C++/CLI.

Keep in mind that spawning a whole process is an extremely expensive operation (compared to simply calling a linked function), so there is some significant overhead you need to be aware of.

Run exe from another exe

cd is a "shell" command, not a program that can be executed.

So to apply it run a shell (cmd.exe under windows) and pass the command(s) you want to execute.

Do to so m Make the content of winCommand look like this:

cmd.exe /C "cd/D N:\folder0\folder1\folder2\foldern && pdftotext.exe data.pdf -layout -nopgbrk"

Please note that changing the drive and directory only applies to the environment used by cmd.exe. The program's drive and directory stay as they were before the call to system().


Update:

Looking more closely at the error message one notices the "sh: ...". This clearly indicates system() does not call cmd.exe, because it most likley would not prefix an error message like this.

From this fact I dare to conclude the code shown is called in and runs under Cygwin.

The shell(s) provided and used by Cygwin do not know the windows specific option /D to the shell command cd, thus the error.

As however the shells used by Cygwin can call cmd.exe my orignally provide approach works, although the explaination I gave is wrong, as pointed out by pmg's comment below.

Execute another EXE from an application with parameters, as admin

You could use this method:

public static int RunProcessAsAdmin(string exeName, string parameters)
{
try {
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = CurrentDirectory;
startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
startInfo.Verb = "runas";
//MLHIDE
startInfo.Arguments = parameters;
startInfo.ErrorDialog = true;

Process process = System.Diagnostics.Process.Start(startInfo);
process.WaitForExit();
return process.ExitCode;
} catch (Win32Exception ex) {
WriteLog(ex);
switch (ex.NativeErrorCode) {
case 1223:
return ex.NativeErrorCode;
default:
return ErrorReturnInteger;
}

} catch (Exception ex) {
WriteLog(ex);
return ErrorReturnInteger;
}
}

The first parameter will be your .exe file and the second one will be the parameters you want to give to your .exe file
After this you should make changes in your .exe file in the main section.
Something like:

static void Main(string[] args)
{
if (args.Length <= 1) return;

try
{
if (args.Length == 2)
{
_IpAddress = args[0];
_IpPort = args[1];
FunctionName(_IpAddress, _IpPort);
}
else
{
_return
}
}
catch (Exception)
{
throw new Exception("Invalid number of parameters!");
}
}

I hope this helps.

Running C Built .exe On Another PC

If you pick the right CPU target (Project Configuration Properties -> C/C++: Enable Enhanced Instruction Set) such that the binary code doesn't include instructions understood by only a very narrow subset of CPUs, and the default for Visual Studio is to use instructions that are supported by the widest set of x86 or x64 CPUs, then your program will run on almost any Windows computer. You need to distribute any DLLs that aren't part of the base Windows installation though, which includes any additional dynamically linked language runtimes such as the Visual C++ runtime.

A good way to derive the list of DLLs that you have to package with your executable is to create a virtual machine with a fresh Windows installation without any development tools in it and then try to run your code there. If you get an error for a missing DLL, add it and repeat. When it comes to the Visual C++ runtime, Microsoft provides installable packages for the different versions that you are allowed to distribute as part of your installation (Visual C++ Whatever Redistributable Package).

Also, mind that programs compiled for 32-bit Windows will mostly run on 64-bit versions, but the opposite is not true.



Related Topics



Leave a reply



Submit