从C ++调用dll函数 [英] Calling a dll function from C++

查看:160
本文介绍了从C ++调用dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数在dll,我想从我的C ++应用程序调用。 dll也是在C + +,并有一个def文件,它显示了dll中存在的函数。我使用visual studio 2010和我配置它使用dll文件通过添加链接器附加库目录中的dll的目录,然后在链接器的输入中添加DLLname.lib。现在dll中的所有命名空间都可用,但我需要的函数不可用,因为它们不在任何命名空间下。如何访问这些功能?这些函数在dll中声明为这样的

I have a function inside a dll that I would like to call from my C++ application. The dll is also made in C++ and has a def file which shows the functions present in the dll. I am using visual studio 2010 and I configured it to use the dll file by adding the directory of the dll in the linker "Additional Library directories" and then adding the DLLname.lib in the "input" of the linker. Now all the namespace inside the dll are available however the Functions that I need are not available because they are not under any namespace. How do I go about accessing those functions ? Those functions were declared as such in the dll

#include "stdafx.h"
#include <stdio.h>
__declspec(dllexport) int somefunction()
{
......
            return SomeValue
}

我的问题是如何通过其dll访问我的C ++应用程序中的一些函数。

My question is how do I access somefunction in my C++ application through its dll.

推荐答案

这里似乎有些混乱。向链接器输入添加文件用于静态链接库(Windows上为.lib)。使用静态链接库,代码只是在编译时复制到您的程序中。动态链接库(Windows中的.dll)驻留在不同的文件(DLL)中,并且在运行它时由程序加载。要访问dll中的函数,有两个主要方法:

There seems to be some confusion here. Adding a file to the linker input is for statically-linked libraries (.lib on Windows). With statically-linked libraries, the code is just copied into your program at compile time. Dynamically-linked libraries (.dll in Windows) reside in different files (the DLLs) and are loaded by your program when you run it. To access a function in a dll, there's two main methods:


  • 使用 dllimport ,类似于您使用dllexport导出函数

  • Use dllimport, similarly to how you exported the functions with dllexport

加载DLL使用 LoadLibrary ,然后获取指针您的功能与 GetProcAddress 。如果你使用这个方法,你应该注意的另一件事是你应该可能使用 externC在导出的函数,以避免名称调整。如果您在使用 GetProcAddress 查找函数时遇到问题,可以使用依赖性Walker 检查DLL中的函数名称 - 根据所使用的调用约定,它们可能会稍有修改。

Load the DLL using LoadLibrary, then get a pointer to your function with GetProcAddress. If you're using this method, another thing you should note is that you should likely use extern "C" on functions you're exporting to avoid name mangling. If you're having issues finding the function with GetProcAddress, you can use Dependency Walker to examine the function names inside the DLL - they may be modified slightly depending on the calling convention used.

这篇关于从C ++调用dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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