LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Redirecting output from command prompt and saving it to an array?

Hello everyone,

 

I have a program that executes a file, and have used the following code to run the file:

int exec=0;

LaunchExecutableEx ("cmd.exe /c config.exe parameters ", LE_HIDE, &exec); 

 

When the config.exe is ran, it generates output on the command screen which I would like to redirect and store in an array. Is this possible?

 

I am trying to avoid redirecting the output to a file and storing it from there.

 

Thanks!

0 Kudos
Message 1 of 8
(3,949 Views)

Hi!

 

Could you elaborate on why this is needed? The current methodology I can see working is using pipes, this is discussed in the below linked stackoverflow thread:

 

https://stackoverflow.com/questions/1404189/how-to-read-redirect-output-of-a-dos-command-to-a-progra...

 

Although I believe popen would only redirect the settings when the pipe is closed. To get continual data I would try looking into the first suggestion of passing a STARTUPINFO structure with the handles. There is a microsoft example from first glance:

 

https://support.microsoft.com/en-us/help/190351/how-to-spawn-console-processes-with-redirected-stand...

 

Hopefully this will help provide a solution,

 

Ed

Message 2 of 8
(3,908 Views)

Hello Ed,

 

Thanks for the response. I've tried using popen but it doesn't seem to be supported in LabWindows. Whenever I try to compile the program, it doesn't recognize that function. Do you have any idea why? 

 

Thanks! 

0 Kudos
Message 3 of 8
(3,889 Views)

You will need to include the headers for this Windows function which is included in the requirements section for the MSDN page:

 

https://msdn.microsoft.com/en-us/library/96ayss4b.aspx

Message 4 of 8
(3,884 Views)

I've included all the required headers, and still get this message:

 

20, 17 error: implicit declaration of function '_popen' is invalid in C99. Make sure that you include the function prototype.

 

Thanks again for all your help I really appreciate it! 

0 Kudos
Message 5 of 8
(3,878 Views)

These issues are C and Win32 based and not explicitly LabWindows/ANSI C, the solution is to declare the function as described in the below thread:

 

https://stackoverflow.com/questions/15850042/xcode-warning-implicit-declaration-of-function-is-inval...

 

Therefore include just after your header declaration and before main, the line:

 

FILE *_popen(  
const char *command,  
const char *mode   
);  
Message 6 of 8
(3,873 Views)

So I tried that, and then I received this error:

 

error: Undefined symbol '__pclose' referenced in "c:\Users\mjj\Desktop\test\cvibuild.test\Debug\test.obj".

error: Undefined symbol '__popen' referenced in "c:\Users\mj\Desktop\test\cvibuild.test\Debug\test.obj".

 

Is there a library I have to include? 

 

Thanks again for your help and your fast responses!

0 Kudos
Message 7 of 8
(3,871 Views)

I was unable to get _popen to work, but ended up finding a code someone had written that performed exactly what I wanted to do, and it works great. Below is the link to that code:

 

https://cboard.cprogramming.com/windows-programming/65209-encapsulating-cmd-exe.html?highlight=creat...

 

Thanks again for all the help! 

0 Kudos
Message 8 of 8
(3,854 Views)