Mozilla use a C DLL with js-ctypes

冷暖自知 提交于 2019-12-31 04:14:47

问题


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



来源:https://stackoverflow.com/questions/9152142/mozilla-use-a-c-dll-with-js-ctypes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!