Javascript 字符串到 C++ JSCTypes 中的字符指针 -LPSTR 缓冲区
我正在使用 JSCTypes 从 JavaScript 访问 DLL。我必须通过将字符缓冲区传递给以下 API 来接收数据,
__declspec(dllexport) WORD WINAPI receive( LPWORD lpwBufferSize,
LPSTR lpsBuffer);
我的 jsctypes 如下所示,
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()
使用上面的代码,我可以第一次正确接收数据,但 xulrunner 在随后的时间调用接收函数时崩溃。 我尝试使用 Windows 上可用的通用 DLL 来重现此问题。这会引发异常, 未捕获的异常: 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();
};
如果我放弃地址 API 调用并按如下方式尝试,
var myArray = ctypes.char.array(64);
我会遇到此问题,尽管在 C++ 中数组被视为指针。
“未捕获的异常:TypeError:预期类型指针,在文件“第 0 行第 0 列处获取了 ctypes.char.array(640000)”
我无权访问任何dll的源代码。我只有 DLL 的包含文件(.h)。我是一名 Java 开发人员,不确定是否可以在没有源代码的情况下进行调试 任何帮助表示赞赏!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于找到解决办法了,
函数原型是一样的
Have finally found a solution,
and the function prototype is the same
如果我不得不猜测,我会说您需要将缓冲区分配到正确的大小。也许:
尝试使用在“receive”函数中设置断点的调试器来查看从 JS 传递的数据。
If I had to guess, I would say that you need to allocate the buffer to the right size. Maybe:
Try using a debugger with a breakpoint set in the "receive" function to see what data is being passed from JS.