LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ERRORLEVEL and getenv.

I am running a batch file via CVI and I want to check if the command I execute in the batch file is successful.  My code is:

 

sysReturn = system("WifiConfig.bat");
pBatchFileReturn = getenv("ERRORLEVEL");

 

The problem I am having is that getenv always returns a NULL.  My expectation is that the return value is 0.  (I also tried modifying the batch file so that the return value is non-zero.  For example:  exit \b 5.)

 

I have run the batch file independent from CVI so I know it works.  That is, I can run the batch file via the command prompt and check the value of %ERRORCODE% and get the expected value (either 0 or whatever non-zero value I programmed into the batch file. 

 

I used getenv to get other environmental variables from my machine (found in Control Panel ->System -> Advanced System Settings -> Environmental Variables).  I did not see ERRORLEVEL explicitly listed, but it obviously exists since I can get its value from the command prompt.

 

Any thoughts?

 

Thanks.

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

Corey,

 

This issue may be related to the fact that a CVI application is not set to be a console application by default. You can change this option by going to build»target settings, then selecting the "Create console application" option. Without this option, the application will run in its own instance of the console, and may not properly read %ERRORLEVEL% in the calling console instance. 

Kirk L. | Applications Engineer | National Instruments
0 Kudos
Message 2 of 7
(5,174 Views)

Another possibility can be found here:

 

http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx

 

 

Kirk L. | Applications Engineer | National Instruments
0 Kudos
Message 3 of 7
(5,173 Views)

I tried putting CVI in console mode - no luck.  I also tried the "GetEnvironmentVariable" function from the winapi library.  Using this funciton I don't get a value for ERRORLEVEL and I was able to check the function "GetLastError" to see what when wrong.  The error I get back is ERROR_ENVVAR_NOT_FOUND. 

 

I know I can create my own environment varibable and manipulate it using "setx" in the batch file but the value change does not take effect until I restart my computer. 

 

I had come across the MSDN blog post you suggested before.  I've read it multiple times to try to compare it to my experience.  It seems like ERRORLEVEL (the environment variable) only(??) exists in the context of a command prompt window.  I tried his script:

rem this next command sets the error level to zero
@echo off
CMD /C EXIT 0
set ERRORLEVEL=1
if ERRORLEVEL 1 echo Does this print?
echo %ERRORLEVEL%

 

In a new command prompt window if I execute "echo %ERRORLEVEL" I get a 0.  After running the script (which outputsa a 1 to the screen), executing "echo %ERRORLEVEL" returns a 1.  Starting over with a new command prompt and running "echo %ERRORLEVEL"  I get a 0 again.

 

I could create a custom environment variable that was updated without having to reset my computer, that should be an acceptable approach.

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

Corey,

 

Are you able to retrieve any other environment variables in CVI? Can you check and see if CVI can retrieve an environment variable created and set by your batch file?

Kirk L. | Applications Engineer | National Instruments
0 Kudos
Message 5 of 7
(5,147 Views)

I can definitely read other environment variables (%systemroot% for example).  I think I tried creating and reading from the batch file and it didn't work - but I have tried several things so I should confirm this by trying again.

 

For ERRORLEVEL I am pretty convinced that it only exists in the context of the command prompt window.  I think my options at this point are trying to create an environment variable and reading it (though I think I've seen that reading an update to that variable requires a computer restart) or writing an output to a text file and parsing it.

0 Kudos
Message 6 of 7
(5,131 Views)

Corey,

 

I can confirm that the latter is correct; created environment variables will require a computer restart to see updates. As circituous as it sounds, the text file may be the best option.

Kirk L. | Applications Engineer | National Instruments
0 Kudos
Message 7 of 7
(5,114 Views)