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

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

问题描述

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



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



这是C代码:

 #包括< 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()方法。



问题是,当我尝试使用扩展名时,我总是收到以下消息:错误:无法打开库



这是我的Javascript调用:

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

似乎DLL不能被Firefox打开,但我不知道为什么。我没有看到任何关于构建DLL扩展的DLL,似乎我们可以使用每个经典的DLL库。



任何想法?谢谢

解决方案

如果你编译这样的库,你将获得依赖于 msvcrt.dll 可能无法在您的系统上解决(可再发行包< a>需要),在我的工作正常。您可以编译您的库而不依赖于CRT,您只需自己定义 DllMain

 #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 ++也是这样) 。


In reference to my first post: Mozilla use a C DLL with js-ctypes

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.

Here is the C code :

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

Here are the compilation lines:

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

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

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

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.

Any idea? Thanks

解决方案

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

And the link step looks like this:

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

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天全站免登陆