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

Need a working example of Firefox ctypes output string parameter

本文关键字:参数 字符串 输出 工作 ctypes Firefox 一个      更新时间:2023-09-26

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

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

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

  1. 我应该如何分配内存在我的C dll中?我应该用吗malloc?如果是的话,结果会怎样自由了吗
  2. 您将如何分配和处理wchar_t*的out参数?我愿意吗将其作为我以前解密的数据
  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