构建一个供 Mozilla js-ctypes 使用的 DLL [英] Build a DLL to be used by Mozilla js-ctypes

查看:37
本文介绍了构建一个供 Mozilla js-ctypes 使用的 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考我的第一篇文章:Mozilla 使用 C DLL 和 js-ctypes

我正在尝试构建一个要从 Mozilla Firefox 扩展中使用的 DLL.我创建了一些 C 代码并用 GCC 编译它.

I'm trying to build a DLL to be used from a Mozilla Firefox extension. I created a little C code and compiled it with GCC.

这是C代码:

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

这里是编译行:

gcc -c library.c
gcc -shared -o library.dll library.o -Wl

DLL 编译好了,我可以用 dllexp 打开它,可以看到暴露的 add() 方法.

The DLL is well compiled, I can open it with dllexp and can see the add() method exposed.

问题是,当我尝试从扩展程序中使用它时,总是收到消息:错误:无法打开库

The problem is, when I try to use it from my extension, I always get the message: Error: couldn't open library

这是我的 Javascript 调用:

Here is my Javascript call:

var libc = ctypes.open("C:\WINDOWS\system32\user32.dll"); //OK
var libc2 = ctypes.open("C:\WINDOWS\system32\library.dll"); //KO

Firefox 似乎无法打开 DLL,但我想知道为什么.我没有看到任何关于为 Firefox 扩展构建 DLL 的内容,看来我们可以使用每个经典的 DLL 库.

It seems the DLL cannot be opened by Firefox, but I wonder why. I don't see anything about building DLL for Firefox extension, it seems we can use every classic DLL library.

有什么想法吗?谢谢

推荐答案

如果你像这样编译库,你会得到对 msvcrt.dll 的依赖,这可能无法在你的系统上解析(需要可再发行包),在我的上它工作正常.你可以在不依赖 CRT 的情况下编译你的库,你只需要自己定义 DllMain:

If you compile the library like that you get a dependency on msvcrt.dll which probably cannot be resolved on your system (redistributable package required), on mine it works fine. You can compile your library without the dependency on the CRT, you just have to define DllMain yourself:

#include<windows.h>

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  return TRUE;
}

int add(int a,int b)
{
    return(a+b);
}

链接步骤如下所示:

gcc -shared -nostdlib -o library.dll library.o -Wl,-e_DllMain@12

那时您不能使用 CRT 功能 - 我找不到在 Windows 上使用 GCC 静态编译运行时的方法(另一方面,Visual C++ 可以做到).

You cannot use CRT functionality then - I couldn't find a way to compile the runtime statically with GCC on Windows (Visual C++ on the other hand does it just fine).

这篇关于构建一个供 Mozilla js-ctypes 使用的 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆