LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

GetLastError returns 0

In some routines I use fopen() to open files.
In the help it says that fopen sets errno to a nonzero value, and if this value happens to be EACCES (among others) that you can get more specific information using GetLastError from the Windows SDK.

The problem is: GetLastError returns 0 (no error).

My code:

#include
#include
DWORD ErrNr, ErrNr2;
if ((DataFilePtr = fopen (PathName, "wb"))== NULL) {
ErrNr = errno;
ErrNr2 = GetLastError();
<> DisplayErrors()
}

When the application breaks (at the <> point)
ErrNr == 13 (EACCES) but ErrNr2 == 0.

What am I doing wrong?

The thing I'm trying to do is to get system messages describing the error, so I don't have to type the text myself (if (errno==EACCES) Messa
gePopup(..... ), if (errno == ...) etc etc).
0 Kudos
Message 1 of 2
(4,012 Views)
It looks like fopen sets the errno, then clears the error code accessed by GetLastError. In the end, you need to call FormatMessage to get the system error message. Pass errno set by fopen directly to FormatMessage, don't use GetLastError.
// get the error message from the errno set by fopen
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errno,
LANG_NEUTRAL,
errorMsg,
256,
NULL);
See the attached files for a working example which tries to open a non-existent file to force an error.
Message 2 of 2
(4,012 Views)