如何在我的C ++项目中使用第三方dll [英] How to use third party dll in my c++ project

查看:54
本文介绍了如何在我的C ++项目中使用第三方dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用第三方dll.我只有一个文件* .dll.如何访问该dll中定义的几种方法.

请有人帮我.


谢谢,

i want to use third party dll in my project. i have only one file *.dll. how can i access few method which is defined in that dll.

please somebody help me.


thanks,

推荐答案

除了dll文件外,您所需的最低要求是该DLL的功能的一些文档,包括功能名称.
如果有的话,可以使用Windows的LoadLibray函数加载库.然后使用对GetProcAddress的多次调用来检索指向您要调用的函数的指针.要实际调用该函数,您需要将该指针转换为具有正确签名的函数指针,即调用约定,返回值和参数.
因此您的代码将如下所示:

The minimum you need in addition to the dll file is some documentation of the functions of that DLL, including the function names.

If you have that you can load the library with the LoadLibray function of Windows. Then use multiple calls to GetProcAddress to retrieve pointers to the functions you want to call. To actually call the function, you need to cast that pointer to a pointer to function with the correct signature, i.e. calling convention, return value, and arguments.

So your code will look something like this:

HINSTANCE hMyib = LoadLibrary (_T("MyLib.dll"));
if (hMyib == 0)
{
    // handle error
}

typedef int MyLibFunction (double arg1, int arg2);

MyLibFunction* pLibFunction =
    (MyLibFunction*) GetProcAddress (hMyib , "MyFuncName");
if (pLibFunction == 0)
{
    // handle error
}

// calling that function
int retValue = (*pLibFunction) (3.14, 42);



如果您的库包含C ++代码,则由于函数名称将被修饰(包含一些编码函数签名的附加字符),事情将变得更加复杂.

希望您能朝正确的方向开始.



If your library contains C++ code, things get more complicated as function names will be mangled (contain some addition characters that encode the signature of the function).

Hope that get''s you started in the right direction.


只需添加到以前的文章中,下载一个简单的DLL Export Viewer,即可为您提供所有可用功能的签名.
Just to add to the previous posts, download a simple DLL Export Viewer, which will give you the signature of all the functions available in a DLL.


  • Best solution: ask for the corrensponding header *.h file (and *.lib one) to the DLL developers.
  • Workaround solution, use the Dependency Walker tool[^] to discover the function (or method) signatures and use LoadLibrary, GetProcAddress, .. to dynamically bind with the DLL (see "Using Run-Time Dynamic Linking"[^]).


    这篇关于如何在我的C ++项目中使用第三方dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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