Exporting functions in MSVS 2013 in C/C++ DLL for

2019-05-11 12:12发布

问题:

I am trying to access some exported functions from a MSVS 2013 C/C++ DLL via FireFox's js-ctypes. I have tried :

  • Changing the "Compile As" settings to C and C++
  • Changing the platform bitness (32 vs 64 bits).
  • Using ALL the available ABI constants from here: https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/js-ctypes_reference/ctypes (default_abi, stdcall_abi, winapi_abi)
  • I have checked my DLL using "Dependency Walker" and made sure that its name was not decorated and that my function was indeed exported.

Here is my DLL code:

#define DllExport extern "C" __declspec(dllexport)

DllExport void Test()
{
    ::MessageBox(NULL, _T("Test!"), _T("Title"), MB_OK);
}

No matter what I try, it seems that I always get this error:

 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

Does anyone have any idea what are the proper settings?

FF is 32 bits (as far as I know) but I don't know if it uses something else like python to load the DLL.

I thought that the "Compile As" wouldn't matter as long as the exporting functions used the proper declaration (e.g. __cdecl).

I am not sure what this produces though (but my project settings are for __cdecl):

#define DllExport extern "C" __declspec(dllexport)

But I have tried replacing that too and using DEF files...

Any idea why nothing seems to wok?

Related questions:

Making a C DLL in Visual Studio suitable for use by js-ctypes in Mozilla

Build a DLL to be used by Mozilla js-ctypes

回答1:

OK. Here is what I've used:

  • Build settings for 32bit platform (from config. manager).
  • Calling convention: __cdecl (/Gd)
  • Compile As: Compile as C++ Code (/TP)
  • Using no DEF files (Linker->Input->Module Definition File)
  • This code:

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

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

You have to define a void argument for functions with no arguments (it's for the return value as Kinjal Dixit pointed out below)!

Unfortunately this didn't find the DLL path (I wonder why... :| ):

var lib = ctypes.open(self.data.url('MyAddonCore.dll'));

Cheers!


Update:

And here is some code to get the DLL path :

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.