Javascript 字符串到 C++ JSCTypes 中的字符指针 -LPSTR 缓冲区

发布于 2024-11-27 20:17:18 字数 1779 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-04 20:17:18

终于找到解决办法了,

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

函数原型是一样的

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

与往事干杯 2024-12-04 20:17:18

如果我不得不猜测,我会说您需要将缓冲区分配到正确的大小。也许:

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

尝试使用在“receive”函数中设置断点的调试器来查看从 JS 传递的数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文