需要一个Firefox ctypes输出字符串参数的工作示例
Need a working example of Firefox ctypes output string parameter
嗨,我有一个XPCOM组件,我现在正在将其转换为使用ctypes。
我能够创建采用wchar_t*的函数,并使用ctypes.jshar.ptr定义这些函数。这一切都很好,但当我需要创建wchar_t指针和指针数组时,我如何使用输出参数?
我读了很多书,我很困惑。
- 我应该如何分配内存在我的C dll中?我应该用吗malloc?如果是的话,结果会怎样自由了吗
- 您将如何分配和处理wchar_t*的out参数?我愿意吗将其作为我以前解密的数据
- 我应该如何处理wchar_t字符串阵列
有人能给我一些代码示例吗?比如说如何处理这样的函数?(在C端,使用malloc?或者我应该使用什么来覆盖内存,在javascript端,应该如何处理)?
int MyFunc(wchar_t** outString, wchar_t*** outStringArray)
谢谢!
我想我可以帮你解决第二个问题。
在C端,接受输出参数的方法看起来像:
const char* useAnOutputParam(const char** outParam) {
const char* str = "You invoked useAnOutputParam'0";
const char* outStr = "This is your outParam'0";
*outParam = outStr;
return str;
}
在JavaScript方面:
Components.utils.import("resource://gre/modules/ctypes.jsm");
/**
* PlayingWithJsCtypes namespace.
*/
if ("undefined" == typeof(PlayingWithJsCtypes)) {
var PlayingWithJsCtypes = {};
};
var myLib = {
lib: null,
init: function() {
//Open the library you want to call
this.lib = ctypes.open("/path/to/library/libTestLibraryC.dylib");
//Declare the function you want to call
this.useAnOutputParam = this.lib.declare("useAnOutputParam",
ctypes.default_abi,
ctypes.char.ptr,
ctypes.char.ptr.ptr);
},
useAnOutputParam: function(outputParam) {
return this.useAnOutputParam(outputParam);
},
//need to close the library once we're finished with it
close: function() {
this.coreFoundationLib.close();
}
};
PlayingWithJsCtypes.BrowserOverlay = {
/*
* This is called from the XUL code
*/
doSomething : function(aEvent) {
myLib.init();
//Instantiate the output parameter
let outputParam = new ctypes.char.ptr();
//Pass through the address of the output parameter
let retVal = myLib.useAnOutputParam(outputParam.address());
alert ("retVal.readString() " + retVal.readString());
alert ("outputParam.toString() " + outputParam.readString());
myLib.close();
}
};
此论坛帖子有助于:http://old.nabble.com/-js-ctypes--How-to-handle-pointers-and-get-multiple-values-td27959903.html
相关文章:
- 从查询字符串参数推断出正确的数据类型
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 从带参数的字符串变量调用函数中的函数
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何格式化match()参数以定位电子邮件中的字符串
- 将JSON字符串转换为函数参数
- 如何用javascript替换Jquery字符串上的新参数
- 模版启动错误-类型错误(“参数”url'必须是字符串,而不是“+类型的url”)
- 在 URL 字符串中传递参数字符串
- 如何在附加的 html 中的字符串上连接 javascript 函数中的参数字符串
- jQuery:从参数字符串设置表单状态
- 使用 cookie 传递 URL 参数字符串
- 寻找一个正则表达式来解析 JS 的参数字符串
- 有必要限制node.js中GET请求参数字符串的大小吗
- 使用jquery获取javascript中的参数字符串
- 使用JavaScript更改url参数字符串
- 如何在提交GET表单后将变量插入到生成的url参数字符串中
- JavaScript:向JavaScript函数发送以逗号分隔的参数字符串
- 无法通过XMLHttpRequest发送参数字符串
- 从Javascript中获取url参数字符串