本文介绍了编译C / C ++一个DLL,然后从其他程序调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个简单的,简单的DLL其中出口一两个功能,然后尝试从另一个程序调用它......无论我到目前为止看,是复杂的事情,连接东西放在一起的方式不同,那我还没有开始奇怪的问题,的实现存在尚未...我只是想上手,通过做一些像这样:

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:

请其中出口某些功能,一个DLL一样,

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

我使用MinGW编译,我想这样做在C,但如果有任何真正的区别在C ++中这样做,我想知道那些也。我想知道如何将该DLL加载到其他C(和C ++)程序,然后从中调用这些函数。
我的目标,对DLL玩弄了一会儿后,是使VB前端C(++)code,通过加载DLL到Visual Basic(我的Visual Studio 6,我只是想某些形式和活动对这些形式的对象,调用DLL)。

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

我需要知道如何调用GCC(/ G ++),使其创建一个DLL,还怎么写(/生成)的出口文件...什么我能/不能在DLL做(比如,我可以从VB前端指针/引用的论点?能否DLL调用一个理论上的功能在前端吗?还是有一个函数采取函数指针从(我甚至不知道,如果可能的话) VB和打电话了吗?)我相当肯定我无法通过变体的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.

好吧,我想通了,如何用gcc编译它,使我跑了DLL

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

然后我有另一个程序加载和测试功能,和它的工作很好,
感谢这么多的意见,
但我试着用VB6加载它,像这样

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

然后我就被称为从形式ADD2(text1.text),但它给了我一个运行时错误:

then I just called add2(text1.text) from a form, but it gave me a runtime error:

找不到DLL入口点ADD2在C:\\ C \\ DLL \\ MYDLL.DLL

"Can't find DLL entry point add2 in C:\c\dll\mydll.dll"

这是code我编译为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;
}

从C程序调用它像这样的工作,虽然:

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

}

什么想法?

要解决previous问题,我不得不编译它,像这样:

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

和使用VB6此API调用

and use this API call in VB6

Public Declare Function add2 Lib "C:\c\dll\mydll" _
    (ByVal num As Integer) As Integer

我学会了不要忘记指定BYVAL或明确的ByRef - 我刚刚回到我传递的参数的地址,它看起来像,-3048

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.

推荐答案

关于建筑使用MinGW的,这里有一些非常简单的指令的DLL。

Regarding building a DLL using MinGW, here are some very brief instructions.

首先,你需要标记你的函数用于出口,因此它们可以被DLL的调用者使用。要做到这一点,修改他们,使他们看起来像(例如)

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

那么,假设你的函数是一个名为funcs.c里,你可以编译它们:

then, assuming your functions are in a file called funcs.c, you can compile them:

gcc -shared -o mylib.dll funcs.c

该-shared标志告诉GCC创建一个DLL。

The -shared flag tells gcc to create a DLL.

要检查是否DLL已实际出口的功能,获得免费的工具的持有和使用它来检查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.

对于一个免费的IDE,将所有的标志自动化等需要构建的DLL,看看优秀的的,这与MinGW的作品非常好。

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.

编辑:有关此主题的更多详细信息,请参阅文章。

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,然后从其他程序调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:14