LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read command prompt output

Solved!
Go to solution

Hi together,

 

I'm executing an external command line program in my CVI application. I'm using this code (which I found somewhere around here):

 

 char command[] = "test.exe";
 STARTUPINFO si;
 PROCESS_INFORMATION piProcess;
 BOOL result;
 DWORD dwExitCode;
 int exitCode;


 ZeroMemory(&si,sizeof si);
 si.cb=sizeof si;
 result = CreateProcess(NULL,command,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|DETACHED_PROCESS,NULL,NULL,&si,&piProcess);
 
 if (result==TRUE) {
  CloseHandle(piProcess.hThread);

  if (WaitForSingleObject(piProcess.hProcess,INFINITE) != WAIT_FAILED)
   GetExitCodeProcess(piProcess.hProcess,&dwExitCode);
    
  CloseHandle(piProcess.hProcess);
 
  exitCode = dwExitCode;
 }

 

It works like it should... Now I'm wondering how it is possible to record or parse the output of the command line application...

 

Cheers

 

 

0 Kudos
Message 1 of 7
(5,986 Views)

If you can wait until the command line app has finished running to be able to read the output then it is very easy. Change the command to run to be something like "cmd.exe /c test.exe > test_output.txt". This causes the Windows Command Prompt to execute your test.exe app and redirect the output into test_output.txt.

 

If you need to process the output stream as the command line app is running, it is much more involved. I can send you the details if you need to use this approach.

 

Michael

NI

0 Kudos
Message 2 of 7
(5,965 Views)

This solution would be fine for me...

 

But if I integrate it, the cmd box pops up, whenever the external program is called and the output-file is empty 😞 

0 Kudos
Message 3 of 7
(5,958 Views)

Can you tell if the command window is from the Windows Command Prompt or is from the command line app 'test.exe'? If it is from the Command Prompt, make sure you have the '/c' option after cmd.exe.

 

It's possible that the test.exe app is writing to stderr instead of stdout. If so, update the command to pass to CreateProcess to something like:"cmd.exe /c test.exe > test_output.txt 2>&1". The '2>&1' also redirects stderr to the output file.

 

Michael

NI

0 Kudos
Message 4 of 7
(5,952 Views)

If I run 'test.exe >out.txt' in the Windows command prompt, it works fine. But if I run 'test.exe > out.txt' or 'cmd.exe /c gfdeco.exe > out.txt' under LabWindows, I get an empty output file. Even if I call an .bat file with the 2 commands inside 😞

 

What's the other way you mentioned above to log the output?

0 Kudos
Message 5 of 7
(5,940 Views)
Solution
Accepted by topic author HansiWWW

I made a few changes to the CreateProcess call and it appears to 'work'. The output was redirected into test.txt, but a black console window is displayed while test.exe is running. I set si.dwX to -10000 so that it should be offscreen.

 

char command[] = "cmd.exe /c test.exe > test.txt";
STARTUPINFO si;
PROCESS_INFORMATION piProcess;
BOOL result;
DWORD dwExitCode;
int exitCode;

ZeroMemory(&si,sizeof si);
si.dwFlags = STARTF_USEPOSITION;
si.dwX = -10000;
si.cb=sizeof si;
result = CreateProcess(NULL,command,NULL,NULL,TRUE,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&piProcess);

if (result==TRUE) {
CloseHandle(piProcess.hThread);
if (WaitForSingleObject(piProcess.hProcess,INFINITE) != WAIT_FAILED)
GetExitCodeProcess(piProcess.hProcess,&dwExitCode);

CloseHandle(piProcess.hProcess);

exitCode = dwExitCode;
}

 

Michael

NI

Message 6 of 7
(5,925 Views)

Works perfect!!! Thanks alot...

0 Kudos
Message 7 of 7
(5,911 Views)