GetLastError() will work, provided there hasn't been another failed call
to mess up the error status, and provided the open failure was in fact
caused by a WIN32 API failure and not an invalid parameter or
something.
A more reliable way is to open the file yourself using CreateFile; you
can then get a corresponding FILE* by doing an
fdopen(_open_osfhandle(hdl,mde)).
--
Jim Patterson Cognos Incorporated
Sr Consulting Engineer P.O. BOX 9707
UUNET:ji...@cognos.COM 3755 Riverside Drive
PHONE:(613)738-1338 x3385 Ottawa, Ont K1G 3Z4
> Olav Tollefsen (olav.to...@nwo.mts.dec.com) wrote:
> : If my "fopen" call fails, how can I get the Win32 error code which
> : tells me exactly what went wrong?
>
> GetLastError() will work, provided there hasn't been another failed call
> to mess up the error status, and provided the open failure was in fact
> caused by a WIN32 API failure and not an invalid parameter or
> something.
If I have some code like this:
FILE* file = fopen("a.a", "w");
if (file == NULL)
{
DWORD nStatus = GetLastError();
...
In the above case, the status returned will be zero (NO_ERROR).
You'll have to use the Win32 CreateFile() to use GetLastError(). The
use of old, none thread-safe functions in the rtl is out. Pure Win32
calls are in.
// JTN (j...@jd.se)
---
* Thomas Nimstad * #pragma message("Standard disclaimer in use")
* Juristdata AB, SWEDEN * Professional development for Windows NT
* Phone +46-500 412150 * Fax +46-500 412848
My guess is that the run-time code for fopen() makes several
Win32 API calls. One of them (not the last one) does the
actual CreatFile() call. However, the API calls after that failed
call were successful. Therefore, GetLastError() returns NO_ERROR.
My suggestion would be to be consistent (i.e. either use CreateFile()
and then use GetLastError() or use fopen() and then use ferror() or
something).
Van Ly
If you are using Microsoft C/C++, the error code from the operating system
is stored in the global variable "_doserrno". If you link with multi-
thread safe libraries (LIBCMT.LIB or MSVCRT.LIB), _doserrno is #define-d
into a function since this value is per-thread. (This is also true of
errno, and the ANSI C documents allow errno to be turned into a function
call returning a pointer to an integer -- it just has to be an "lvalue",
something that can go on the left hand side of an assignment statement.)
Calling CreateFile is not at all comparable to fopen since the latter
gives you buffering.
This is not official technical support but rather a personal attempt to
be helpful.
Thanks,
Steve Salisbury
# Obligatory_ # |-------------------------------------------------------|
# disclaimer_ # | The views expressed in this message are my own and in |
# required by # | no way reflect the views of Microsoft Corporation. |
# my employer # |-------------------------------------------------------|