LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Error structure in Labwindows

void __declspec(dllexport) __stdcall MyFunction(short *errorOccurred, long *errorCode, char errorMsg[1024]) { int error = 0; ErrMsg errMsg = {'\0'}; ERRORINFO errorInfo; Error: // If an error occurred, set the error flag to cause a run-time error in TestStand. if (error < 0) { *errorOccurred = TRUE; *errorCode = error; strcpy(errorMsg, errMsg); } }

0 Kudos
Message 1 of 8
(4,475 Views)

Hi Guys,

I am very sorry to post a bad code above.

 

I used to be a C++ programmer, now I try to learn and use NI development suit to create code. I have not used them before.

Regarding to bellow CVI sample code copied from Teststand demo, I have some questions:

 

1. It declared one variable error, and use a expression if (error < 0), but where the code change the value?

2. How to understand Error: in the code? Where I can find its definition and usage introduction? Of course, I am sure it is similar to Try-Catch block in C++.

3. What are the definiton of ErrMsg and ERRORINFO? I can find them even in CVI help file.

Please help me!

Thanks a lot!

 

void __declspec(dllexport) __stdcall VacuumOn(short *errorOccurred, long *errorCode, char errorMsg[1024])
{
    int error = 0;
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;

Error:  

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    {
        *errorOccurred = TRUE;
        *errorCode = error;
        strcpy(errorMsg, errMsg);
    }
}

 

0 Kudos
Message 2 of 8
(4,444 Views)

Could some one kindly help me?

:manhappy:

0 Kudos
Message 3 of 8
(4,426 Views)

If you have a look at the contents of the CVI file "toolbox.h", you will find some macros which find common use in CVI library code. Search for "errChk" and "nullChk" and you should get the general idea of this error checking macro approach. It does not suit everyone, especially those working for companies who proscribe the use of the C goto feature.

 

JR

0 Kudos
Message 4 of 8
(4,417 Views)

Thanks for jr_2005 's kindly reply!

Now I know some basics in CVI for error handling, though it is a little strange for me now, maybe I just need to follow this rule. ^_^

No variable declaration, no key word definition! This is my another concern, how to define functions of ErrMsg and ERRORINFO? How do I use them in my code?

Thanks very much!

0 Kudos
Message 5 of 8
(4,406 Views)

Well the user program is expected to define the error variable - as in the section of code you posted yourself (int error = 0; ). Don't think of this scheme as a 'rule' - some people use it and some people don't.

 

As for ERRORINFO, this is a custom CVI structure defined in "cviauto.h". You will only need to use this if you are incorporating library functions which specify a variable of that type in their parameter list, in which case the function help will tell you exactly what to do with it.

 

I'd hazard a guess that ErrMsg is a macro for use with the TestStand code you posted, but I can't be sure as I don't use TestStand.

 

If you are uncomfortable with this overall approach, don't use it. As long as you can understand what other authors' code intends, you will be able to adapt it to your own solution.

 

JR

0 Kudos
Message 6 of 8
(4,397 Views)

Hi jr_2005, Thanks!

I have found definition of ERRORINFO, it will be:

typedef struct {
    unsigned short wCode;                       // An error code describing the error.
    SCODE sCode;                                // A return value describing the error.
    char source[ERRORINFO_SRC_BUF_SIZE];        // Source of the exception.
    char description[ERRORINFO_DESC_BUF_SIZE];  // Textual description of the error.
    char helpFile[MAX_PATHNAME_LEN];            // Help file path.
    unsigned long helpContext;                  // Help context ID.
    int errorParamPos;                          // 1 based position of invalid parameter
} ERRORINFO;

 So I can use this structure to get detailed information when any error occured, in my view, it is very useful, like Exception object in C++.

Another request to disturb you, could you show me a sample code for better error handling structure in CVI  as you think? Forgive me, I am pretty new in CVI field.

Thanks a lot!:womanvery-happy:

0 Kudos
Message 7 of 8
(4,371 Views)

I have posted a document about error checking some while ago: it doesn't make use of ERRORINFO srtucture but can give you a sample framework for you to elaborate your own error checking policy. You can find it among my contributions



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 8 of 8
(4,364 Views)