Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

firefox addon - Build a DLL to be used by Mozilla js-ctypes

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...