导出MSVS 2013中的函数在C/C++ DLL中供Mozilla js-ctypes使用

use*_*353 3 dll firefox-addon jsctypes firefox-addon-sdk

我试图通过FireFox的js-ctypes从MSVS 2013 C/C++ DLL访问一些导出的函数.我试过了 :

这是我的DLL代码:

#define DllExport extern "C" __declspec(dllexport)

DllExport void Test()
{
    ::MessageBox(NULL, _T("Test!"), _T("Title"), MB_OK);
}
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么,似乎我总是得到这个错误:

 console.error: myxpi:
   Message: Error: couldn't find function symbol in library
   Stack:
     openScratchpad@resource://gre/modules/addons/XPIProvider.jsm -> jar:file:///c:/users/kgk/appdata/local/temp/tmpdyrqfd.mozrunner/
 extensions/jid1-QEiY1nT1Uinqug@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-qeiy1nt1uinqug-at-jetpack/myxpi/lib/main.js:34:18
 button<.onClick@resource://gre/modules/addons/XPIProvider.jsm -> jar:file:///c:/users/kgk/appdata/local/temp/tmpdyrqfd.mozrunner/extensions/jid1-QEiY1nT1Uinqug@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-qeiy1nt1uinqug-at-jetpack/myxpi/lib/main.js:16:9
Run Code Online (Sandbox Code Playgroud)

有谁知道什么是正确的设置?

FF是32位(据我所知)但我不知道它是否使用像python这样的东西来加载DLL.

我认为只要导出函数使用了适当的声明(例如__cdecl),"编译为"就无关紧要了.

我不确定这会产生什么(但我的项目设置是为了__cdecl):

#define DllExport extern "C" __declspec(dllexport)
Run Code Online (Sandbox Code Playgroud)

但我也尝试过更换它并使用DEF文件......

知道为什么似乎什么都没有?

相关问题:

在Visual Studio中制作适合由Mozilla中的js-ctypes使用的C DLL

构建一个由Mozilla js-ctypes使用的DLL

use*_*353 5

好.这是我用过的:

  • 构建32位平台的设置(来自配置管理器).
  • 调用约定:__ cdecl(/ Gd)
  • 编译为:编译为C++代码(/ TP)
  • 不使用DEF文件(链接器 - >输入 - >模块定义文件)
  • 这段代码:

    #define DllExport extern "C" __declspec(dllexport)
    DllExport void Test()
    {
            ::MessageBox(NULL, _T("Test!"), _T("Title"), MB_OK);
    }
    
    Run Code Online (Sandbox Code Playgroud)

JS:

    var lib = ctypes.open("C:\\Users\\user\\Desktop\\myXPI\\lib\\MyAddonCore.dll");
    var test = lib.declare("Test", ctypes.winapi_abi, ctypes.void_t);
    test();
    lib.close();
Run Code Online (Sandbox Code Playgroud)

你必须为void没有参数的函数定义一个参数(它是返回值,Kinjal Dixit如下所示)!

不幸的是,这没有找到DLL路径(我想知道为什么......:|):

var lib = ctypes.open(self.data.url('MyAddonCore.dll'));
Run Code Online (Sandbox Code Playgroud)

干杯!


更新:

这里有一些代码来获取DLL路径:

http://www.acnenomor.com/3342758p1/how-to-load-dll-from-sdk-addon-data-folder

    const {Cc, Cu, Ci} = require("chrome");
    Cu.import("resource://gre/modules/Services.jsm");
    const ResProtocolHandler = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
    const ChromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIChromeRegistry);

    function resolveToFile(uri) {
        switch (uri.scheme) {
            case "chrome":
                return resolveToFile(ChromeRegistry.convertChromeURL(uri));
            case "resource":
                return resolveToFile(Services.io.newURI(ResProtocolHandler.resolveURI(uri), null, null));
            case "file":
                return uri.QueryInterface(Ci.nsIFileURL).file;
            default:
                throw new Error("Cannot resolve");
        }
    }

    var self = require("sdk/self");
    let dll = self.data.url("test.dll");
    dll = resolveToFile(Services.io.newURI(dll, null, null));
    console.log(dll.path); // dll.path is the full, platform-dependent path for the file.
Run Code Online (Sandbox Code Playgroud)