0

I should call a DLL (Win x64) function from JS code.

Definitions in C:

typedef void*  HNDL;
ABCD_DLL_DECL DWORD WINAPI  ABCD_Create(HNDL* pHNDL);

So the HNDL is a pointer and the create function will give back an object pointer (pHNDL).

I have tried to use these in JS:

const Handle = koffi.pointer("Handle", koffi.opaque());
const ABCD_Create = lib.func("uint __stdcall ABCD_Create(_Inout_ Handle *pHandle)";

let hHandle;
let err = ABCD_Create(hHandle);

or

const ABCD_Create = lib.func("uint __stdcall ABCD_Create(_Inout_ void *phandle)";

or

const Handle = koffi.opaque();
const ABCD_Create = lib.func("ABCD_Create", "uint", [koffi.out(koffi.pointer(Handle, 2))]);

I have tried a lot of combination of these and other samples in examples from documentation of koffi, but when I call the create function I always get the "Process exited with code 3221225477" in VSCode. (The original code works perfectly in VC and Delphi...) This error number is the 0xC0000005. It means access violation. So I think somewhere the parameter passing is wrong.

How should I define it and call the create function correctly (with koffi)?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
droland
  • 19
  • 3

1 Answers1

0

I have found a similar example in koffi tests (sqlite.js). So the following declaration works:

const Handle = koffi.opaque("Handle");
const ABCD_Create = lib.func("ABCD_Create","uint", koffi.out(koffi.pointer(Handle, 2))]";

... and the calling:

let phandle = [null];
let handle = null;

const err = ABCD_Create(phandle);
if (err != 0) {
  console.error(`Create failed. Error = ${err}`);
  return;
}

handle = phandle[0];
droland
  • 19
  • 3