Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 01, 2015 ErrnoException in Windows | ||||
---|---|---|---|---|
| ||||
Could you, please, help me to understand, why code: //////////////// import std.c.windows.windows; import std.exception: ErrnoException; import std.stdio: writefln; import std.string: toStringz; void main () { CreateFileA(toStringz("nonexisting file name"), GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, null); auto ex = new ErrnoException("CreateFileA"); writefln("ex.errno=%d, ex.msg=%s, lasterror=%d", ex.errno, ex.msg, GetLastError()); } //////////////// prints: ex.errno=0, ex.msg=CreateFileA (No error), lasterror=2 I wanted it will be: ex.errno=2, ex.msg=CreateFileA (File not found), lasterror=2 |
March 01, 2015 Re: ErrnoException in Windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 Attachments: | On Sun, 01 Mar 2015 16:39:27 +0000, novice2 wrote:
> Could you, please, help me to understand, why code:
'cause winapi functions never sets `errno`. `errno` is a libc feature, and winapi knows nothing about libc. besides, `GetLastError()` is not required to return correct errno codes.
so you have to either use libc funcions, or translate `GetLastError()` codes to errno manually.
|
March 01, 2015 Re: ErrnoException in Windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | On Sunday, 1 March 2015 at 16:39:29 UTC, novice2 wrote: > I wanted it will be: > ex.errno=2, ex.msg=CreateFileA (File not found), lasterror=2 Here's the right way to do this: ////////////////////////// test.d ////////////////////////// import std.c.windows.windows; import std.string : toStringz; import std.windows.syserror : wenforce; void main () { auto handle = CreateFileA(toStringz("nonexisting"), GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, null); wenforce(handle != INVALID_HANDLE_VALUE, "CreateFileA"); } //////////////////////////////////////////////////////////// See std.windows.syserror for more information. |
March 01, 2015 Re: ErrnoException in Windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | Thans guys! wenforce not sutable - error code is lost. may be, i will use modified wenforce, wich throws ErrnoException. |
Copyright © 1999-2021 by the D Language Foundation