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

发布于 11-17 06:23 字数 520 浏览 5 评论 0原文

您好,我有一个 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)

谢谢!

Hi I have an XPCOM component which I'm now converting to use ctypes.

I was able to create functions that take a wchar_t* and I define the functions using ctypes.jschar.ptr.
This all works great, but how do I use output parameters when I need to create wchar_t pointers and array of pointers?

I have done a lot of reading and I'm confused.

  1. How should I allocate the memory
    inside my C dll? should I use
    malloc? if so how would that get
    freed up?
  2. How would you allocate and handle an
    out parameter of wchar_t * ? would I
    pass it in from javascript as a
    CData I declate before?
  3. How should I handle a wchar_t string
    array?

Can anyone give me some code examples of say how to handle a function like this?
(both on the C side of things, using malloc? or what should I use to allcoate the memory and on the javascript side, how this should be handled)?

int MyFunc(wchar_t** outString, wchar_t*** outStringArray)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一腔孤↑勇2024-11-24 06:23:31

我想我可以帮助回答你的第二个问题。

在 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

I think I can help with your second question.

On the C side, your method which accepts an output parameter looks like:

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;
}

And on the JavaScript side:

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();
    }
};

This forum post helped: http://old.nabble.com/-js-ctypes--How-to-handle-pointers-and-get-multiple-values-td27959903.html

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