问题描述
我想制作一个简单,简单的DLL,它导出一个或两个函数,然后尝试从另一个程序调用它...到目前为止我看到的是复杂的事情,将事情联系在一起的不同方法,我甚至没有开始实现存在的奇怪问题...我只是想开始,做这样的事情:制作一个导出一些函数的DLL,如
int add2(int num){
return num + 2 ;
}
int mult(int num1,int num2){
int product;
product = num1 * num2;
退货;
}
我正在用MinGW进行编译,我想在C ,但是如果在C ++中有任何真正的差异,我也想知道这些。我想知道如何将该DLL加载到另一个C(和C ++)程序中,然后从中调用这些函数。
我的目标是在使用DLL进行一些操作之后,通过将DLL加载到视觉基础(Visual Studio 6,我只是想要)来制作一个VB前端的C(++)代码为这些表单上的对象做一些表单和事件,调用DLL)。
我需要知道如何调用gcc(/ g ++)来创建一个DLL,而且还如何写(/生成)一个导出文件...以及我可以/不能在DLL中做什么(像我可以从VB前端通过指针/引用获取参数?DLL可以调用理论功能在前端?或者有一个函数采取一个函数指针(我甚至不知道是否可能)从VB并调用它?)我相当肯定我不能通过一个变体到DLL ...但这是我真正知道的。
再次更新
好吧,我想出了如何使用gcc编译,以使dll运行
gcc -c -DBUILD_DLL dll.c
gcc -shared -o mydll.dll dll.o -Wl, - out-implib,libmessage.a
然后我有另一个程序加载它并测试这些功能,它工作得很好,
非常感谢对于建议,
但我尝试加载它与VB6,像这样
公共声明函数add2 LibC: \c\dll\mydll.dll(num As Integer)As Integer
然后我只是从表单中调用add2(text1.text),但是它给了我一个运行时错误:
C:\c中找不到DLL入口点add2 \dll\mydll.dll
这是我为DLL编译的代码:
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
EXPORT int __stdcall add2(int num){
return num + 2;
}
EXPORT int __stdcall mul(int num1,int num2){
return num1 * num2;
}
从C程序调用这样的工作,但是:
#include< stdio.h>
#include< windows.h>
int main(){
HANDLE ldll;
int(* add2)(int);
int(* mul)(int,int);
ldll = LoadLibrary(mydll.dll);
if(ldll>(void *)HINSTANCE_ERROR){
add2 = GetProcAddress(ldll,add2);
mul = GetProcAddress(ldll,mul);
printf(add2(3):%d\\\
mul(4,5):%d,add2(3),mul(4,5));
} else {
printf(ERROR。);
}
}
任何想法?
解决了
为了解决以前的问题,我只需要像下面这样编译:
gcc -c -DBUILD_DLL dll.c
gcc -shared -o mydll.dll dll.o -Wl, - add-stdcall-alias
并在VB6中使用此API调用
公共声明函数add2 LibC:\c\dll\mydll_
(ByVal num As Integer)As Integer
我学会了不要忘记明确指定ByVal或ByRef - 我刚刚收回了我通过的参数的地址, -3048。
关于使用MinGW构建DLL,以下是一些非常简短的说明。
首先,您需要标记您的导出功能,因此可以由DLL的调用者使用它们。为此,请修改它们,使其看起来像(例如)
__ declspec(dllexport)int add2(int num){
返回num + 2;
}
然后,假设你的函数在一个名为funcs.c的文件中,你可以编译它们:
gcc -shared -o mylib.dll funcs.c
/ pre>
-shared标志告诉gcc创建一个DLL。
要检查DLL是否实际导出功能,获取免费的工具,并使用它来检查DLL。
对于一个可以自动化构建DLL所需的所有标志等的免费IDE,请查看优秀的,它与MinGW非常相似。
编辑:本主题,请参阅MinGW Wiki上的文章。
I want to make a simple, simple DLL which exports one or two functions, then try to call it from another program... Everywhere I've looked so far, is for complicated matters, different ways of linking things together, weird problems that I haven't even begun to realize exist yet... I just want to get started, by doing something like so:
Make a DLL which exports some functions, like,
int add2(int num){ return num + 2; } int mult(int num1, int num2){ int product; product = num1 * num2; return product; }
I'm compiling with MinGW, I'd like to do this in C, but if there's any real differences doing it in C++, I'd like to know those also. I want to know how to load that DLL into another C (and C++) program, and then call those functions from it.My goal here, after playing around with DLLs for a bit, is to make a VB front-end for C(++) code, by loading DLLs into visual basic (I have visual studio 6, I just want to make some forms and events for the objects on those forms, which call the DLL).
I need to know how to call gcc (/g++) to make it create a DLL, but also how to write (/generate) an exports file... and what I can/cannot do in a DLL (like, can I take arguments by pointer/reference from the VB front-end? Can the DLL call a theoretical function in the front-end? Or have a function take a "function pointer" (I don't even know if that's possible) from VB and call it?) I'm fairly certain I can't pass a variant to the DLL...but that's all I know really.
update again
Okay, I figured out how to compile it with gcc, to make the dll I ran
gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--out-implib,libmessage.a
and then I had another program load it and test the functions, and it worked great,thanks so much for the advice,but I tried loading it with VB6, like this
Public Declare Function add2 Lib "C:\c\dll\mydll.dll" (num As Integer) As Integer
then I just called add2(text1.text) from a form, but it gave me a runtime error:
"Can't find DLL entry point add2 in C:\c\dll\mydll.dll"
this is the code I compiled for the DLL:
#ifdef BUILD_DLL #define EXPORT __declspec(dllexport) #else #define EXPORT __declspec(dllimport) #endif EXPORT int __stdcall add2(int num){ return num + 2; } EXPORT int __stdcall mul(int num1, int num2){ return num1 * num2; }
calling it from the C program like this worked, though:
#include<stdio.h> #include<windows.h> int main(){ HANDLE ldll; int (*add2)(int); int (*mul)(int,int); ldll = LoadLibrary("mydll.dll"); if(ldll>(void*)HINSTANCE_ERROR){ add2 = GetProcAddress(ldll, "add2"); mul = GetProcAddress(ldll, "mul"); printf("add2(3): %d\nmul(4,5): %d", add2(3), mul(4,5)); } else { printf("ERROR."); } }
any ideas?
solved it
To solve the previous problem, I just had to compile it like so:
gcc -c -DBUILD_DLL dll.c gcc -shared -o mydll.dll dll.o -Wl,--add-stdcall-alias
and use this API call in VB6
Public Declare Function add2 Lib "C:\c\dll\mydll" _ (ByVal num As Integer) As Integer
I learned not to forget to specify ByVal or ByRef explicitly--I was just getting back the address of the argument I passed, it looked like, -3048.
解决方案Regarding building a DLL using MinGW, here are some very brief instructions.
First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)
__declspec( dllexport ) int add2(int num){ return num + 2; }
then, assuming your functions are in a file called funcs.c, you can compile them:
gcc -shared -o mylib.dll funcs.c
The -shared flag tells gcc to create a DLL.
To check if the DLL has actually exported the functions, get hold of the free Dependency Walker tool and use it to examine the DLL.
For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.
Edit: For more details on this subject, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.
这篇关于在C / C ++中编译DLL,然后从另一个程序中调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!