Mozilla use a C DLL with js-ctypes

2019-03-01 12:06发布

问题:

I'm tying to build a dll, and then use it with a Firefox extension.

I managed to build a DLL using gcc under Windows :

#include<stdio.h>
int add(int a,int b)
{
    return(a+b);
}

I try now to use it through my dll. After reading some posts, especially this one, I couldn't manage to make this work: Reference a binary-component to js-ctypes

Each time I try ctypes.open, I have the error message: couldn't load the library. However, the DLL path is correct. Here is the JS code:

Components.utils.import("resource://gre/modules/ctypes.jsm");

AddonManager.getAddonByID("greenfox@octo.com", function(addon)
{
    var libcPath = addon.getResourceURI("components/library.dll");

    if (libcPath instanceof Components.interfaces.nsIURI)
    {
        var libc = ctypes.open(libcPath.path);

        var libc = ctypes.open(libc);

        /* import a function */
        var puts = libc.declare("add", /* function name */
                   ctypes.default_abi, /* call ABI */
                   ctypes.int32_t, /* return type */
                   ctypes.int32_t, /* argument type */
                   ctypes.int32_t /* argument type */
          );

          var ret = puts(1,2);

          alert("1+2="+ret);

    }

Do you have any idea?

回答1:

The path part of the URI is not what you want to have here - you want the file path:

if (libcPath instanceof Components.interfaces.nsIFileURL)
{
    var libc = ctypes.open(libcPath.file.path);

Documentation