Koromix / koffi

Fast and easy-to-use C FFI module for Node.js
https://koffi.dev/
MIT License
199 stars 3 forks source link

How can I access the data pointed to by a pointer returned from a DLL using Koffi? #206

Open AndySkaura opened 5 days ago

AndySkaura commented 5 days ago

This is my Node.js calling code. In this code, bits is a pointer that is declared and assigned within the DLL. How can I access the raw data pointed to by this pointer in Node.js? I have reviewed the documentation but couldn’t find a way to access it.

// 定义所需类型
const PInt = koffi.pointer('PInt', 'int32'); 
const PByte = koffi.pointer('PByte', 'uint8_t*'); 

// 加载 DLL
const dllPath = './static/NodeDLL.dll';
const nodeDLL = koffi.load(dllPath);

// 定义 GetBmpBits 函数
const GetBmpBits = nodeDLL.func('bool GetBmpBits(_Inout_ PInt AWidth,_Inout_ PInt AHeight,_Inout_ PByte ABits,_Inout_ PInt ASize)');

const width = new Int32Array(1);
const height = new Int32Array(1);
const size = new Int32Array(1);
let bits = new Uint8Array(1);     // This is a pointer.

width[0] = 1920;
height[0] = 1080;

const success = GetBmpBits(width, height, bits, size);
if (success) {
    //how to get the bitmap bits?
} else {
    console.error("Failed to get bitmap bits.");
}

Sorry, my English expression might be unclear. Thank you for providing the library, It helps me a lot.

AndySkaura commented 5 days ago

I successfully accessed the data within the pointer. The main issue in the code above is in let bits = new Uint8Array(4);. I suspect this object caused the pointer to become invalid. Changing it to let bits = [null]solved the problem. Although the returned object appears empty, it is still a valid pointer. Using koffi.decode(bits[0], koffi.array('uint8', bitLength)) effectively retrieves the values.

Is it possible to add an object in JavaScript to simulate a pointer?