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

Javascript 需要一个Firefox ctypes输出字符串参数的工作示例,javascript,firefox,firefox-addon,ctypes,xpcom,Javascript,Firefox,Firefox Addon,Ctypes,Xpcom,嗨,我有一个XPCOM组件,我现在正在转换为使用ctypes 我能够创建采用wchar_t*的函数,并使用ctypes.jschar.ptr定义函数。 这一切都很好,但是当我需要创建wchar\t指针和指针数组时,如何使用输出参数呢 我读了很多书,我很困惑 我应该如何分配内存 在我的C dll中?我应该用吗 马洛克?如果是这样的话,那会怎样呢 释放 您将如何分配和处理 wchar_t*的out参数?我会吗 从javascript将其作为 CData我以前迟到过吗 我应该如何处理wchar\t字符

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

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

我读了很多书,我很困惑

  • 我应该如何分配内存 在我的C dll中?我应该用吗 马洛克?如果是这样的话,那会怎样呢 释放
  • 您将如何分配和处理 wchar_t*的out参数?我会吗 从javascript将其作为 CData我以前迟到过吗
  • 我应该如何处理wchar\t字符串 阵列
  • 谁能给我一些代码示例,比如如何处理这样的函数? (无论是在C端,使用malloc?还是在javascript端,我应该使用什么来分配内存,这应该如何处理)


    谢谢

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

    在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();
        }
    };
    
    该论坛帖子有助于:

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