Javascript String to C++ char pointer -LPSTR buffer in JSCTypes

人走茶凉 提交于 2019-12-23 05:36:07

问题


I am accessing the DLL from JavaScript using JSCTypes. I have to receive data by passing a character buffer to the following API,

__declspec(dllexport) WORD WINAPI receive( LPWORD  lpwBufferSize,
                                           LPSTR   lpsBuffer);

My jsctypes looks like this,

let receive = libs.dll.declare("receive",
                               ctypes.stdcall_abi,  
                               ctypes.int32_t,           // Return type - return code
                               ctypes.int32_t.ptr,       // buffer size
                               ctypes.char.ptr,          // Buffer
                               );
var bufferSize = new ctypes.int32_t(3000000).address(); //3000000
var buffer = new ctypes.char().address();
let rvopen = receive(bufferSize, buffer);
return buffer.readString()

With above code, I could receive data for the first time correctly but xulrunner crashes on receive function call in the subsequent times. I tried to reproduce this produce this issue with a common DLL available on windows. This throws an exception, uncaught exception: TypeError: ctypes.char.array(500).address is not a function

var hostName = exports.getString = function() {
    let lib = ctypes.open('Ws2_32.dll');
    let gethostname = lib.declare("gethostname",
                                  ctypes.default_abi,
                                  ctypes.int,
                                  ctypes.char.ptr,
                                  ctypes.int);
    var myArray = ctypes.char.array(500).address();
    gethostname(myArray, 500);
    return myArray.readString();
};

If I drop the address API call and try it as below,

var myArray = ctypes.char.array(64);

I run into this issue, although in C++ arrays are considered as pointers.

'uncaught exception: TypeError: expected type pointer, got ctypes.char.array(640000)' in file '' at line 0, col 0

I don't have access to any of the dll's source code. I just have the include file(.h) for the DLL. I am a Java developer and not sure if I can debug without the source code Any help appreciated!


回答1:


Have finally found a solution,

<code>
 let charArray= ctypes.ArrayType(ctypes.char);
 let myArray = new charArray(500);      
</code>

and the function prototype is the same




回答2:


If I had to guess, I would say that you need to allocate the buffer to the right size. Maybe:

var buffer = new ctypes.char().array(3000000).address();

Try using a debugger with a breakpoint set in the "receive" function to see what data is being passed from JS.



来源:https://stackoverflow.com/questions/6936567/javascript-string-to-c-char-pointer-lpstr-buffer-in-jsctypes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!