I am using koffi (koffi.dev) to read my dll function
My dll: mylib.dll
Above is my js code:
const koffi = require('koffi');
const lib = koffi.load('mylib.dll');
// console.log(lib) // this load ok
const setup = lib.func('setup', 'int', ['const wchar_t *', 'wchar_t **']);
// console.log(setup) // this function load ok
const inputJson = {
"config": {
"pass": "123"
}
}
const jsonString = JSON.stringify(inputJson);
// Function to convert a string to a buffer with wide characters (UTF-16)
function stringToWideCharBuffer(str) {
const buffer = Buffer.alloc((str.length + 1) * 2); // +1 for null-terminator, *2 for 2 bytes per wchar
buffer.write(str, 0, 'utf16le');
return buffer;
}
const jsonConfig = stringToWideCharBuffer(jsonString);
// Allocate memory for the output buffer
const jsonOutBuffer = koffi.alloc('wchar_t*', 1024+1); // I am trying to another way but do not able to get the output
// Step 4: Call the function
const result = setup(jsonConfig, jsonOutBuffer);
console.log(result) // result is 0 meaning it successfully, but output come from jsonOutBuffer
// Step 5: Process the result
// Check the result and process the output
if (result === 0) {
// Convert the wide character buffer to a UTF-8 string
console.log('Output JSON:', koffi.decode(jsonOutBuffer, 'wchar_t', -1)); // thinking to get the value at here
} else {
console.error('setup failed with code:', result);
}
I am thinking that my output value is incorrect, could you help me to point the output value as well?