Header Header

Examples
[Error handling]

Table of contents

gxerror01 - Set, get error example

Shows the way of setting and reading the error message buffer.

When specifying the needed include files we have included the gxsdldr.c loader file directly:

#include "gxsd.h"
#include "gxerror.h"
#include "gxsdldr.c"

Starting the main function and defining the a variable for the error code:

int main(void) {
    int err_code;

Defining a character buffer for the error message (Unicode version):

    /* Unicode version */
    {
        wchar_t err_str[256];

Setting new error code and message:

        /* Updates the error buffer */
        gx_seterror(GX_EINVAL, L"(Test string [%i %hs %ls])", (int)1234, "ABC", L"ABC");

Getting the error code and message set:

        /* Gets the error string and code */
        err_code = gx_geterror(0, err_str, sizeof(err_str)/sizeof(wchar_t)-1);

Displaying the error code and message:

        err_str[sizeof(err_str)/sizeof(wchar_t)-1] = 0;
        printf("Error code: %i, Description: '%ls'\n", err_code, err_str);
    }

And the same in ASCII version:

    /* ASCII version */
    {
        char err_str[256];

        /* Updates the error buffer */
        gx_seterrora(GX_EINVAL, "(Test string [%i %hs %ls])", (int)1234, "ABC", L"ABC");

        /* Gets the error string and code */
        err_code = gx_geterrora(0, err_str, sizeof(err_str)/sizeof(char)-1);

        err_str[sizeof(err_str)/sizeof(char)-1] = 0;
        printf("Error code: %i, Description: '%hs'\n", err_code, err_str);
    }

And the sample program ends:

    return 0;
}

gxerror02 - Clear error example

Shows the way of clearing the error message buffer.

When specifying the needed include files we have included the gxsdldr.c loader file directly:

#include "gxsd.h"
#include "gxerror.h"
#include "gxsdldr.c"

Starting the main function and defining the a variable for the error code and a character buffer for the error message:

int main(void) {
    int err_code;
    char err_str[256];
    err_str[sizeof(err_str)-1] = 0;

Clearing the error buffer:

    /* Clears the error buffer */
    gx_clearerror();

Getting and displaying the error code (0 - GX_ENOERR) and message (''):

    /* Gets the error code and description text */
    gx_geterrora(&err_code, err_str, sizeof(err_str)-1);

    /* Displays the error code and description text to STDOUT */
    printf("1. Error code: %i, Description: '%s'\n", err_code, err_str);

Setting and displaying the new error code and message:

    /* Sets a new error code and description text */
    gx_seterror(GX_EINVAL, (const wchar_t *)L"%hs", "(Error occurred in my test application)");

    /* Gets second error code and description text */
    gx_geterrora(&err_code, err_str, sizeof(err_str)-1);

    /* Displays second error code and description text to STDOUT */
    printf("2. Error code: %i, Description: '%s'\n", err_code, err_str);

And the sample program ends:

    return 0;
}

gxerror03 - Append, prepend error example

Shows the way of setting and appending to the error message buffer.

When specifying the needed include files we have included the gxsdldr.c loader file directly:

#include "gxsd.h"
#include "gxerror.h"
#include "gxsdldr.c"

Starting the main function and defining the a variable for the error code and a character buffer for the error message:

int main(void) {
    int err_code;
    char err_str[256];

Setting new error code and message:

    /* Sets some error code and description text (no format string) */
    gx_seterror(GX_EINVAL, (const wchar_t *)L"%hs", "Line #1: Error occurred");

Appending new error message to the back of the error buffer and overriding the error code:

    gx_appenderror(GX_EREAD, (const wchar_t *)L"%hs", "Line #2: Read error occurred");

Appending new error message to the front of the error buffer and overriding the error code:

    gx_prependerror(GX_EWRITE, (const wchar_t *)L"%hs", "Line #0: Write error occurred");

Getting and displaying the error code and message:

    /* Gets the error code and description text */
    gx_geterrora(&err_code, err_str, sizeof(err_str)-1);

    /* Displays the error code and description text to STDOUT */
    err_str[sizeof(err_str)-1] = 0;
    printf("Error code: %08x\n Description: '%s'\n", err_code, err_str);

And the sample program ends:

    return 0;
}

gxerror04 - Threaded error handling example

Demonstrates that the error handling is thread safe. Two threads are started that on startup set different error codes. On loop each thread displays it's thread id and the error code set. If everything is OK then the error codes displayed must differ from thread to thread.

When specifying the needed include files we have included the gxsdldr.c loader file directly:

#include "gxsd.h"
#include "gxerror.h"
#include "gxsdldr.c"

Including platform specific interface(s):

    #include <windows.h>

Defining a thread function that has the error code as parameter:

    DWORD WINAPI my_thread_func(LPVOID err_arg) {
        int ix;
        int err_code = (int)err_arg;
        gx_seterrorcode(err_code);

        err_code = 0;
        for(ix = 0; ix < 5; ix++) {
            gx_geterrora(&err_code, (char *)0, 0);
            printf("Thread %08x: error code is %08x\n",
                    (int)GetCurrentThreadId(), err_code);
            Sleep(2000);
        }

        ExitThread(0);
        return 0;
    }

Starting the main function:

    int main(void) {

Defining the two threads:

        HANDLE thread1, thread2;

Creating and starting the first thread:

        /* Creates the first thread with GX_ECREAT */
        thread1 = CreateThread(0, 0, my_thread_func, (LPVOID)GX_ECREAT, 0, NULL); 

        if(thread1 == NULL) {
            fprintf(stderr, "CreateThread() error: %i\n", GetLastError());
            return 1;
        }

Waiting for a short time:

        Sleep(1000);

Creating and starting the second thread:

        /* Creates the second thread with GX_EREAD */
        thread2 = CreateThread(0, 0, my_thread_func, (LPVOID)GX_EREAD, 0, NULL); 

        if(thread2 == 0) {
            fprintf(stderr, "CreateThread() error: %i\n", GetLastError());
            return 1;
        }

Waiting for the threads to finish their job and ending the sample program:

        /* Synchronizes */
        WaitForSingleObject(thread1, INFINITE);
        CloseHandle(thread1); 
        WaitForSingleObject(thread2, INFINITE);
        CloseHandle(thread2); 

        return 0;
    }


Generated  for GX
(c) Adaptive Recognition