LabWindows/CVI

取消
显示结果 
搜索替代 
您的意思是: 

Read command prompt output

已解决!
转到解答

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 项奖励
1 条消息(共 7 条)
9,312 次查看

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 项奖励
2 条消息(共 7 条)
9,291 次查看

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 项奖励
3 条消息(共 7 条)
9,284 次查看

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 项奖励
4 条消息(共 7 条)
9,278 次查看

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 项奖励
5 条消息(共 7 条)
9,266 次查看
解答
已被主题作者 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

6 条消息(共 7 条)
9,251 次查看

Works perfect!!! Thanks alot...

0 项奖励
7 条消息(共 7 条)
9,237 次查看