JSCTypes中的C ++ char指针-LPSTR缓冲区的JavaScript字符串 [英] Javascript String to C++ char pointer -LPSTR buffer in JSCTypes

查看:224
本文介绍了JSCTypes中的C ++ char指针-LPSTR缓冲区的JavaScript字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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,//返回类型 - 返回码
ctypes .int32_t.ptr,//缓冲区大小
ctypes.char.ptr,//缓冲区
);
var bufferSize = new ctypes.int32_t(3000000).address(); // 3000000
var buffer = new ctypes.char()。address();
让rvopen = receive(bufferSize,buffer);
return buffer.readString()

使用上面的代码,我可以接收第一个时间正确,但xulrunner在接收函数调用后崩溃。
我试图重现这个产生这个问题与Windows上可用的常见DLL。这引发了一个异常,
未被捕获的异常:TypeError:ctypes.char.array(500).address不是一个函数

$ $ p $ $ $ $ c> 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);
返回myArray.readString();
};

如果我放弃地址API调用并按如下方式尝试:

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

我遇到这个问题,虽然在C ++数组中被认为是指针。
$ blockquote
未捕获的异常:类型错误:预期类型指针,在文件''的第0行,第0列中得到ctypes.char.array(640000)'


我无法访问任何dll的源代码。我只有DLL的包含文件(.h)。我是一个Java开发人员,不知道我是否可以在没有源代码的情况下进行调试
任何帮助表示感谢!

解决方案

终于找到了解决办法,

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

和函数原型是一样的


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!

解决方案

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

这篇关于JSCTypes中的C ++ char指针-LPSTR缓冲区的JavaScript字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆