Execute External Program

Execute external program

borrowed this shamely from here

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {
System.out.println(line);
}

More information here

Other issues on how to pass commands here and here

How Can I execute external program with arguments in Java

There is a exec method specifically overloaded for your purposes. It takes as an argument String array which holds the command and the arguments for the command. That seems to be exactly what you need. Please look at the API

How do I execute an external program within C code in Linux with arguments?

For a simple way, use system():

#include <stdlib.h>
...
int status = system("./foo 1 2 3");

system() will wait for foo to complete execution, then return a status variable which you can use to check e.g. exitcode (the command's exitcode gets multiplied by 256, so divide system()'s return value by that to get the actual exitcode: int exitcode = status / 256).

The manpage for wait() (in section 2, man 2 wait on your Linux system) lists the various macros you can use to examine the status, the most interesting ones would be WIFEXITED and WEXITSTATUS.

Alternatively, if you need to read foo's standard output, use popen(3), which returns a file pointer (FILE *); interacting with the command's standard input/output is then the same as reading from or writing to a file.

How do I execute an external program from Perl and have the perl script continue

  • To call your program, you should pass the program arguments as separate arguments to exec. (See https://perldoc.perl.org/functions/exec.html.)
  • Since you want the script to proceed without waiting for the program to return, you should use fork (see https://perldoc.perl.org/functions/fork.html) so that you have two separate processes running in parallel (one running the program, one running the rest of the script).

So:

my $child_pid = fork();
die "Couldn't fork" unless defined $child_pid;
if (! $child_pid) {
exec '/usr/bin/dotnet', '/usr/local/myprogram/myprogram.dll', 'arg1', 'arg2', 'moreargs';
die "Couldn't exec myprogram: $!";
}

# rest of script

wait();

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


Related Topics



Leave a reply



Submit