Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winlean definition issues #12327

Open
euantorano opened this issue Oct 1, 2019 · 5 comments
Open

Winlean definition issues #12327

euantorano opened this issue Oct 1, 2019 · 5 comments

Comments

@euantorano
Copy link
Contributor

The winlean module has a couple type definitions that don't quite match up to Microsoft's documentation:

  • DWORD: defined as int32, Microsoft documents it as typedef unsigned long DWORD;:

A 32-bit unsigned integer. The range is 0 through 4294967295 decimal.
This type is declared in IntSafe.h as follows:

  • HANDLE: defined as int, Microsoft documents it as typedef PVOID HANDLE;:

A handle to an object.

There may well be others, but these are the two I've noticed so far. This is causing issues in my serial package.

It seems the DWORD issue was already reported as #9848

The HANDLE definition also means that the INVALID_HANDLE_VALUE constant doesn't work as this is defined as -1 by Microsoft, which isn't a valid int.

Changing these may break things, but for the sake of correctness (and fixing my library 😉) I think it's worth investigating.

euantorano added a commit to euantorano/Nim that referenced this issue Oct 1, 2019
Build is currently failing, need to work through all definitions to check them
@euantorano euantorano changed the title Winlean definition issues: Winlean definition issues Oct 1, 2019
@ringabout
Copy link
Member

ringabout commented Jul 29, 2020

Edited:
It seems to be consistent with posix definitions. I don't know whether it is right?

# posix.nim
SockLen* {.importc: "socklen_t", header: "<sys/socket.h>".} = cuint

winlean defines SockLen = cuint but it should be cint.

https://nim-lang.org/docs/winlean.html#SockLen

proc accept(s: SocketHandle; a: ptr SockAddr; addrlen: ptr SockLen): SocketHandle {.
    stdcall, importc: "accept", dynlib: ws2dll.} 

In MSDN:
https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept

SOCKET WSAAPI accept(
  SOCKET   s,
  sockaddr *addr,
  int      *addrlen
);

Another thing which can be improved, but it's ok for me.
It also uses pointer instead of ptr char or cstring to stand for char * in c.

proc getsockopt(s: SocketHandle; level, optname: cint; optval: pointer;
               optlen: ptr SockLen): cint {.stdcall, importc: "getsockopt",
                                        dynlib: ws2dll.}

@timotheecour
Copy link
Member

note regarding WINBOOL:

WINBOOL is currently WINBOOL* = int32; this is wrong on 2 levels:

  • according to https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types WINBOOL is:
    BOOLEAN = BYTE = unsigned char (and is explicitly mentioned as being 8 bits, unlike int32) = cuchar in nim
  • furthermore, since windows uses opposite convention as posix (0 means FALSE aka error on windows APIs usually, whereas 0 means success on posix), we should use distinct cuchar and introduce an auxiliary template proc isSuccess(a): bool to work cross platform and be self descriptive in code; would avoid the current guesswork when reading this:
proc closeHandleCheck(handle: Handle) {.inline.} =
    if handle.closeHandle() == 0:
      raiseOSError(osLastError())

links

@ringabout
Copy link
Member

ringabout commented Jan 22, 2021

# winlean
type
  LONG* = int32
  ULONG* = int32
  PULONG* = ptr int

this is not correct too! It should be

type
  LONG* = int32
  ULONG* = cuint
  PULONG* = ptr ULONG

@euantorano
Copy link
Contributor Author

I’m going to try get tome this weekend to at least take a first pass at this and see how much would break.

@HugoGranstrom
Copy link
Contributor

I'll chime in and add that AsyncHttpClient which uses AsyncFile uses DWORD for its offset:

ol.offset = DWORD(f.offset and 0xffffffff)

This means it's limited to roughly 2GB files because DWORD = int32 in winlean:
DWORD* = int32

But DOWRD = uint32 here:
DWORD = uint32

So using HttpClient you can download files up to 4GB. This inconsistency alone has bugged me recently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants