LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling command line EXE from my CVI program

I have a command line EXE which I need to control from my CVI program. This
third party app when run, prints data received from our device on the
console window. For starters, all I need is a way to start this app and
capture the text output from it and append to a list box. Later I will have
to send commands to this app.

Maybe this has been answered earlier but I couldn't find on NI web site
(search tool is not good).

In my previous projects, I have used Tcl to control command line apps and
put GUIs on top. In Tcl, I can get a handle to the app and read/write to it
like file (in sync as well as async. manner). Can I do same in CVI?

Thanks,


vishi
0 Kudos
Message 1 of 10
(6,425 Views)
Hi vishi,
if your exe accept redirection of output, you can run it using LaunchExecutableEx and redirect output to a file, next read the file content and fill the list box as needed.

Using LaunchExecutableEx permits you to hide the exe window, which can be useful. It is also an asyncronous execution, too, that is if you need yo wait for the exe termination you need to monitor it, as in this example:

SetWaitCursor (1);
LaunchExecutableEx ("command.com /C dir *.exe > c:\\temp\\temp.txt", LE_HIDE, &cH);
while (!ExecutableHasTerminated (cH)) {
ProcessSystemEvents ();
}
SetWaitCursor (0);


To run the exe in a sync manner, you can use the system () command, but in this case you cannot hide the exe window. The same example as above sh
oud bi this one:

err = system ("command.com /C dir *.exe > c:\\temp\\temp.txt");

Hope this helps
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 10
(6,425 Views)
Thanks,

But in case I need to send something to this exe, how would I go about it.

vishi

"Roberto Bozzolo" wrote in message
news:50650000000500000033C30000-1031838699000@exchange.ni.com...
> Hi vishi,
> if your exe accept redirection of output, you can run it using
> LaunchExecutableEx and redirect output to a file, next read the file
> content and fill the list box as needed.
>
> Using LaunchExecutableEx permits you to hide the exe window, which can
> be useful. It is also an asyncronous execution, too, that is if you
> need yo wait for the exe termination you need to monitor it, as in
> this example:
>
> SetWaitCursor (1);
> LaunchExecutableEx ("command.com /C dir *.exe >
> c:\\temp\\temp.txt", LE_HIDE, &cH);
> while (!ExecutableHasTerm
inated (cH)) {
> ProcessSystemEvents ();
> }
> SetWaitCursor (0);

>
> To run the exe in a sync manner, you can use the system () command,
> but in this case you cannot hide the exe window. The same example as
> above shoud bi this one:
>
> err = system ("command.com /C dir *.exe >
> c:\\temp\\temp.txt");

>
> Hope this helps
> Roberto
0 Kudos
Message 3 of 10
(6,425 Views)
Well, there are many ways to interact with a running app.
First of all, if your app accepts command-line parameters, you can pass them both with system () and LaunchExecutableEx ().
Other ways of interaction can be via DDE, ActiveX of some API, but they rely on the app itself being built so as to support those instruments.
Your app documentation should explain if there is some way of interaction between programs or not.

Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 10
(6,425 Views)
Sorry if I was not clear, I am talking about controlling something which I
can not change, like telnet.exe which comes with windows. When I open a DOS
box (under windows2000) and type telnet.exe, basically telnet.exe is taking
input rom stdin and dumping to stdout. Now I am looking for a way where I
can redirect these handles to my app in such a way that the exe thinks a
person is typing commands on the prompt and seeing the output. Tcl/Expect
does this in a snap but I need to use CVI do it. If someone know how to do
this in VC++, that's fine. Otherwise I will have to download Expect code and
see how they do it.

vishi

"Roberto Bozzolo" wrote in message
news:506500000005000000C1C30000-1042324653000@exchange.ni.com...
> Well,
there are many ways to interact with a running app.
> First of all, if your app accepts command-line parameters, you can
> pass them both with system () and LaunchExecutableEx ().
> Other ways of interaction can be via DDE, ActiveX of some API, but
> they rely on the app itself being built so as to support those
> instruments.
> Your app documentation should explain if there is some way of
> interaction between programs or not.
>
> Roberto
0 Kudos
Message 5 of 10
(6,425 Views)
Al Stevens created a C++ class to do this called ConsoleApp and described how it
works in Oct '02 DDJ.
It uses the Win32 API call CreateProcess which let you substitute your own file
handles for the console app's stdin and stdout.

You can download the original code from here:
http://www.ddj.com/ftp/2002/2002_10/cpro1002.zip

The latest updates can be found here:
http://www.alstevens.com/quincy.html

Nick
0 Kudos
Message 6 of 10
(6,425 Views)
Thanks Nick, I will try it.

vishi

"Nick J" wrote in message
news:3e24d5bf@newsgroups....
> Al Stevens created a C++ class to do this called ConsoleApp and described
how it
> works in Oct '02 DDJ.
> It uses the Win32 API call CreateProcess which let you substitute your own
file
> handles for the console app's stdin and stdout.
>
> You can download the original code from here:
> http://www.ddj.com/ftp/2002/2002_10/cpro1002.zip
>
> The latest updates can be found here:
> http://www.alstevens.com/quincy.html
>
> Nick
>
>
0 Kudos
Message 7 of 10
(6,425 Views)

Hi Roberto,

I am trying to read the output from a command line EXE using system() or LaunchExecutableEx(), but I haven't been able to.

Normally in cmd I would send the following:

C:\TestTool\TestTool.exe enter

And the response I get is:

PASS

From cmd I can add "|clip" to copy the response to the Clipboard, like this:

C:\TestTool\TestTool.exe enter |clip

Now I don't see a response in the console, but "PASS" is in the Clipboard and I can paste it.

I can also do what you suggested in this topic:

C:\TestTool\TestTool.exe enter > output.txt

And the response will appear in C:\TestTool\output.txt

 

The problem is that when I use system() or LaunchExecutableEx() in my CVI application I don't get the response.

system("C:\TestTool\TestTool.exe enter |clip");

system("C:\TestTool\TestTool.exe enter > output.txt");

LaunchExecutableEx("C:\TestTool\TestTool.exe enter |clip",  gWindowState, &gExeHndl);

LaunchExecutableEx("C:\TestTool\TestTool.exe enter > output.txt",  gWindowState, &gExeHndl);

 

Any clue?

0 Kudos
Message 8 of 10
(3,815 Views)

Well, this works for me with a OS shell command. It is not guaranteed it works with your program as well, but you can easily see if '| clip' keyword works by lanching the program interactively and pasting output (if any) to notepad.

int CVICALLBACK test (int panel, int control, int event,
					  void *callbackData, int eventData1, int eventData2)
{
	char	*msg = NULL;

	if (event != EVENT_COMMIT) return 0;

	system ("cmd /c dir | clip");
	ClipboardGetText (&msg, NULL);

	if (msg)
		MessagePopup ("Clipboard", msg);
	else
		MessagePopup ("Clipboard", "No text was available");
	if (msg) free (msg);
	return 0;
}

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 9 of 10
(3,809 Views)

Hi Roberto,

 

Thanks much for your quick response.

I had to do a batch file to make it work, i. e.:

system("C:\TestTool\TestTool_Enter.bat");

And this is the content of the batch file.

C:\TestTool\TestTool.exe enter |clip

Thank you,

JIBRAN

0 Kudos
Message 10 of 10
(3,795 Views)