06-01-2017 04:26 PM
Hello Everyone,
I'm trying to launch an executable file, but when I use the LaunchExecutable function, the command window opens and immediately closes. Below is the code I am using to launch the executable:
LaunchExecutable ("c:\\Users\\mj\\Desktop\\config.exe");
Also, I've tried looking up on the forums on how to pass the required parameters for the executable but have had no luck. Any help would be very much appreciated.
Thanks!
06-01-2017 05:35 PM
Hey mjed,
To pass parameters, you'll need to format the string input to LaunchExecutable to match how you'd pass parameters on the command line. For example, if you want to pass 3 parameters, it would looks like this:
LaunchExecutable("C:\\Users\\mj\\Desktop\\config.exe 1 2 3");
This help document gives a little more detail:
http://zone.ni.com/reference/en-XX/help/370051AC-01/cvi/libref/cvilaunchexecutable/
Cheers,
Ryan
06-02-2017 10:10 AM
I got it to work, Thanks Ryan!
06-02-2017 10:12 AM
Hey mjed,
Glad to help!
Cheers,
Ryan C.
01-10-2019 04:29 PM
I did some testing with the kinds of strings accepted by the LaunchExecutable() and System() functions. I used a program called Tftpd64.exe as the program I was trying to launch. I put a copy of it in "C:\Program Files\" and in a directory I created named "C:\Program-Files\" (using a dash instead of a space). I used the char array named "command" to create many different strings and then passed each one to the LaunchExecutable() and System() functions. After trying each one, I marked each command constructor with a comment to show which ones worked and which did not work. Using the /c option used with cmd.exe closes the window after executing the command and the /k option keeps it open. You can also replace double backslashes (\\) below with a single forward slash (/). I did the test with Windows 7 and LabWindows 2015.
char command[256] = {'\0'}; int error_status = 0; // USING THE EXECUTABLE WITH ITS PATH WORKS WITH OR WITHOUT SPACES strcpy(command, "C:\\Program-Files\\tftpd64.exe"); // works for system & launchEx. // "C:\Program-Files\Tftpd64\tftpd64.exe" strcpy(command, "C:\\Program Files\\tftpd64.exe"); // works for system & launchEx. // "C:\Program Files\Tftpd64\tftpd64.exe" // ALL REMAINING EXAMPLES USE CMD.EXE
// USING cmd.exe WITH /c OR /k OPTIONS AND start KEYWORD WORKS WITHOUT SPACES strcpy(command, "cmd /c start C:\\Program-Files\\tftpd64.exe"); // works for system & launchEx. // "cmd /c start C:\Program-Files\Tftpd64\tftpd64.exe" strcpy(command, "cmd /k start C:\\Program-Files\\tftpd64.exe"); // works for system & launchEx. // "cmd /k start C:\Program-Files\Tftpd64\tftpd64.exe" // I GUESS THE START KEYWORD IS OPTIONAL strcpy(command, "cmd /c C:\\Program-Files\\tftpd64.exe"); // works for system & launchEx. // "cmd /c C:\Program-Files\Tftpd64\tftpd64.exe" strcpy(command, "cmd /k C:\\Program-Files\\tftpd64.exe"); // works for system & launchEx. // "cmd /k C:\Program-Files\Tftpd64\tftpd64.exe" // LEAVING OUT THE /c OR /k FLAGS MAKES IT FAIL strcpy(command, "cmd C:\\Program-Files\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "cmd C:\Program-Files\Tftpd64\tftpd64.exe" strcpy(command, "start C:\\Program-Files\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "start C:\Program-Files\Tftpd64\tftpd64.exe" strcpy(command, "cmd start C:\\Program-Files\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "cmd start C:\Program-Files\Tftpd64\tftpd64.exe" // SPACES IN THE PATH ALSO MAKE IT FAIL strcpy(command, "cmd /k start C:\\Program Files\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "cmd /k start C:\Program Files\Tftpd64\tftpd64.exe" strcpy(command, "cmd /c start C:\\Program Files\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "cmd /c start C:\Program Files\Tftpd64\tftpd64.exe" // LABWINDOWS WON'T COMPILE WITH 2 SETS OF QUOTES IN A STRING strcpy(command, "cmd /k start C:\\"Program Files"\\tftpd64.exe"); // DOES NOT COMPILE!!! strcpy(command, "cmd /c start "C:\\Program Files\\tftpd64.exe""); // DOES NOT COMPILE!!! // YOU CAN, HOWEVER, BUILD UP A STRING WITH MULTIPLE QUOTE PAIRS USING strcat() char doubleQuote[2] = {0x22, 0x0}; // "'" // ENCLOSING THE FOLDER NAME HAVING THE SPACE IN QUOTES WORKS
strcpy(command, "cmd /k start C:\\");
strcat(command, doubleQuote);
strcat(command, "Program Files");
strcat(command, doubleQuote);
strcat(command, "\\tftpd64.exe"); // works for system & launchEx.
// "cmd /k start C:\"Program Files"\Tftpd64\tftpd64.exe"
// ENCLOSING THE ENTIRE PATH IN QUOTES DOES NOT WORK strcpy(command, "cmd /k start "); strcat(command, doubleQuote); strcat(command, "C:\\Program Files\\tftpd64.exe"); strcat(command, doubleQuote); // DOES NOT WORK for system & launchEx!!! // "cmd /k start "C:\Program Files\Tftpd64\tftpd64.exe"" // SINGLE QUOTES DON'T WORK strcpy(command, "cmd /c start 'C:\\Program Files\\tftpd64.exe'"); // DOES NOT WORK for system & launchEx!!! // "cmd /c start 'C:\Program Files\Tftpd64\tftpd64.exe'" strcpy(command, "cmd /c start C:\\'Program Files'\\tftpd64.exe"); // DOES NOT WORK for system & launchEx!!! // "cmd /c start C:\'Program Files'\Tftpd64\tftpd64.exe" error_status = system (command); error_status = LaunchExecutable (command);
01-19-2019 10:34 PM - edited 01-19-2019 10:39 PM
I found out that you can include one or more quotes inside a pair of quotes by preceding
it with a backslash which greatly simplifies one of my examples as shown below. // ENCLOSING THE FOLDER NAME HAVING THE SPACE IN QUOTES WORKS
strcpy(command, "cmd /k start C:\\\"Program Files\"\\tftpd64.exe");strcat(command, doubleQuote);strcat(command, "Program Files");strcat(command, doubleQuote);strcat(command, "\\tftpd64.exe");// works for system & launchEx.
// "cmd /k start C:\"Program Files"\Tftpd64\tftpd64.exe"
01-22-2019 06:26 AM
I personally would surround the whole path with double quotes rather than only path segments containing spaces. Not every command line client may be prepared to parse such paths where only part of a path is between double quotes.
strcpy(command, "cmd /k start \"C:\\Program Files\\tftpd64.exe\"");
01-22-2019 07:51 AM
I know lots of people enclose the entire path in quotation marks and perhaps that is a better standard. But I can also tell you that I've enclosed only folder and file names having spaces in quotes in cmd.exe batch files and powershell scripts and have never observed a hiccup. Here's one I've used dozens of times.
@set appname=NI-DAQmx @set keyfile=C:\"Program Files (x86)"\"National Instruments"\NI-DAQ\"DAQmx ANSI C Dev"\lib\msvc\NIDAQmx.lib @set instCmd=%instRoot%NI-DAQmx\ver16\32Bit\setup.exe /qf /AcceptLicenses yes /log %logPath%\niDaqMx.html /r:n @set shrtCut=none @call :Install