gpt4 book ai didi

javascript - 需要 Firefox ctypes 输出字符串参数的工作示例

转载 作者:行者123 更新时间:2023-11-28 10:16:36 25 4
gpt4 key购买 nike

您好,我有一个 XPCOM 组件,我现在正在将其转换为使用 ctypes。

我能够创建采用 wchar_t* 的函数,并使用 ctypes.jschar.ptr 定义函数。这一切都很好,但是当我需要创建 wchar_t 指针和指针数组时,如何使用输出参数?

我读了很多书,但我很困惑。

  1. 我应该如何分配内存在我的 C dll 中?我应该使用分配?如果是的话那会怎样被释放了?
  2. 您将如何分配和处理wchar_t * 的输出参数?我可以吗从 javascript 将其作为我之前声明过CData吗?
  3. 我应该如何处理 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

关于javascript - 需要 Firefox ctypes 输出字符串参数的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com