11-12-2015 05:33 PM
After some searching, I don't think I've found a forum response that addresses my issue. Hopefully, I did not overlook something.
I have a LabWindows CVI tool that needs to interface (read from / write to) a launched executable on STDIO. Unfortunately, I'm unable to modify the aforementioned executable and must use it as is.
Launching the executable from LabWindows seems OK. I use the following command:
err = LaunchExecutableEx("cmd /c test -i InputCommands.ini", LE_SHOWNORMAL, &testHandle);
This "test.exe" gets initialized by "InputCommands.ini" and outputs a line of status information every second. Ideally, I would like to:
- monitor (read from STDO) the status
- based on read data, determine when to quit the test (write to STDI)
- record (read from STDO) the final status data.
I've seen many suggestions for redirecting Standard Output to a file from which I could simultaneously read and monitor. I've tried the redirection portion with the following command. This definitely redirects the output, but leaves me with the problem (1.) I describe below.
err = LaunchExecutableEx("cmd /c test -i InputCommands.ini > output.txt", LE_SHOWNORMAL, &testHandle);
My points of frustration are:
1. How do I send commands to this launched executable telling it to stop? I need to send 'q' followed by 'quit' to the program to stop the executable.
printf("q\n");
This method sends a 'q' to STDIO, which the launched executable blissfully ignores. It seems like I'm missing the obvious, which is no doubt true...
2. Is there some way to directly read the Standard Output without the interim step of redirecting to a file?
I appreciate the thoughts.
11-13-2015 01:37 PM
Hey alwaysabeginner,
The best way to end the execution of an .exe that you started with LaunchExecutableEx would be to use the CVI function, TerminateExecutable, with the handle returned from LaunchExecutableEx. You can see the help for this function here: http://zone.ni.com/reference/en-XX/help/370051AC-01/cvi/libref/cviterminateexecutable/ .
As for reading from the console, CVI is designed to interface with its own instance of stdio which is why the quit command is ignored and why you can’t easily read from Window’s command prompt, so the solution of reading and writing from that file is probably still going to be your best option.
Hope this helps,
Kevin
11-13-2015 04:37 PM
Hi Kevin,
Thank you for the information.
After further experimentation, the following perhaps clunky method enabled LabWindows to send keypresses to the console:
//Declare Variables HWND WindowHandle; //Window Handle for STDIO LRESULT KeyResult; //Keypress Return Result
int testHandle; //Executable Handle //Launch the "test.exe" executable with the assembled InputCommands.ini commands, redirect to Output.txt err = LaunchExecutableEx("cmd /c test -i InputCommands.ini > Output.txt",LE_SHOWNORMAL, &testHandle); LE_SHOWNORMAL, &satfeHandle);
//Delay unique to my testing Delay(10); //Collect the Handle for the Standard I/O window with FindWindowEx() WindowHandle = FindWindowEx( NULL, //Parent Class - if NULL assume desktop NULL, //Child Class - if NULL assume child of desktop "ConsoleWindowClass", //Found STDIO Class information using AutoIt v3 Window Finder! "Standard I/O" //This is the title of the Standard I/O window in question ); //Wait 2 seconds for Window Handle return Delay(2); //Send the 'q' message to quit the test and wait 1 second
//Information on Keypress Processing
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646268(v=vs.85).aspx KeyResult = SendMessage(WindowHandle,WM_CHAR,'q',1); Delay(1); //Send the remaining 'quit' message KeyResult = SendMessage(WindowHandle,WM_CHAR,'q',1); KeyResult = SendMessage(WindowHandle,WM_CHAR,'u',1); KeyResult = SendMessage(WindowHandle,WM_CHAR,'i',1); KeyResult = SendMessage(WindowHandle,WM_CHAR,'t',1); KeyResult = SendMessage(WindowHandle,WM_CHAR,0x0D,1); //Carriage Return
The AutoIt v3 Window Info utility proved helpful in finding out the Class information for the FindWindowEx() command.