How do I convert OVERLAPPED struct to js-ctypes?

93 Views Asked by At

I'm trying to get ReadFileEx working in js-ctypes and ran into a bit of an obstacle converting the OVERLAPPED struct to js-ctypes. Any tips on the translation would be much appreciated:

typedef struct _OVERLAPPED {
  ULONG_PTR Internal;
  ULONG_PTR InternalHigh;
  union {
    struct {
      DWORD Offset;
      DWORD OffsetHigh;
    };
    PVOID  Pointer;
  };
  HANDLE    hEvent;
} OVERLAPPED, *LPOVERLAPPED;

This is not right, but I started with

const struct_overlapped = new ctypes.StructType("overlapped",
  [ {"Internal": ctypes.uint64_t.ptr},
    {"InternalHigh": ctypes.uint64_t.ptr},
    [ [ {"Offset": ctypes.uint32_t},
        {"OffsetHigh": ctypes.uint32_t} ],
      {"Pointer": ctypes.voidptr_t} ],
    {"hEvent": ctypes.voidptr_t} ]);

but I'm not sure how to handle the "union" and the second struct.

Update. Maybe something like this?

const struct_offset = new ctypes.StructType("offsetStruct",
  [ {"Offset": ctypes.uint32_t},
    {"OffsetHigh": ctypes.uint32_t} ]);

const struct_overlapped = new ctypes.StructType("overlapped",
  [ {"Internal": ctypes.uint64_t.ptr},
    {"InternalHigh": ctypes.uint64_t.ptr},
    [ {"offsetStruct": ctypes.struct_offset},
      {"Pointer": ctypes.voidptr_t} ],
    {"hEvent": ctypes.voidptr_t} ]);

But I get an error

Message: Error: struct field descriptors must contain one property

Update 2. Maybe the first attempt will work?

1

There are 1 best solutions below

3
Noitidart On

Before getting in overlapped structs and completion routines, the solution to these synchronus blocking APIs such as WinAPI, COM, Objective-C, GTK, XCB, X11, etc etc etc is to use works.

Note that some callbacks in GTK and ObjC and X11 have to be on the main thread. And its not possible to use dispatch_queue to set up the callback on the mainthread from workers due to an unfixable bug.

Windows is full on fun with workers no sweat.

I personally love PromiseWorkers - https://developer.mozilla.org/en-US/docs/Web/API/PromiseWorker - I hacked it up so I can call functions from the mainthread from the PromiseWorker (you might not need this so I wouldn't recommend copying the PromiseWorker boiler plate from my addons)