2011-06-26 36 views
1

嗨我有一個XPCOM組件,我現在正在轉換爲使用ctypes。需要一個Firefox ctypes輸出字符串參數的工作示例

我能夠創建採用wchar_t *的函數,並使用ctypes.jschar.ptr定義函數。 這一切都很好,但是當我需要創建wchar_t指針和指針數組時,我該如何使用輸出參數?

我已經做了很多的閱讀,我很困惑。

  1. 我該如何分配我的C DLL中的內存 ?我應該使用 malloc嗎?如果是這樣會如何得到 釋放?
  2. 如何分配和處理wchar_t *的out參數 ?我會 通過它從JavaScript作爲 CData我declate之前?
  3. 我該如何處理wchar_t字符串 數組?

任何人都可以給我一些代碼示例說如何處理這樣的功能? (在事物的C端,使用malloc?或者我應該使用什麼來塗抹內存和JavaScript端,這應該如何處理)?

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

謝謝!

回答

0

我想我可以幫你解決第二個問題。

在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

相關問題