热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从c++调用DLL中的函数-CallingfunctionsinaDLLfromC++

IhaveasolutioninVS2008with2projectsinit.OneisaDLLwritteninC++andtheotherisas

I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like know how to call the functions in the DLL from the application.

我在VS 2008有一个解决方案,里面有两个项目。一个是用c++编写的DLL,另一个是从一个空白项目中创建的简单的c++控制台应用程序。我想知道如何从应用程序调用DLL中的函数。

Assume I am starting with a blank C++ project and that I want to call a function called int IsolatedFunction(int someParam)

假设我从一个空白的c++项目开始,我想调用一个名为int IsolatedFunction(int someParam)的函数

How do I call it?

我怎么称呼它?

6 个解决方案

#1


20  

There are many ways to do this but I think one of the easiest options is to link the application to the DLL at link time and then use a definition file to define the symbols to be exported from the DLL.

有很多方法可以做到这一点,但我认为最简单的方法之一是在链接时将应用程序链接到DLL,然后使用定义文件定义要从DLL导出的符号。

CAVEAT: The definition file approach works bests for undecorated symbol names. If you want to export decorated symbols then it is probably better to NOT USE the definition file approach.

注意:定义文件方法对于未修饰的符号名称效果最好。如果您想要导出装饰符号,那么最好不要使用定义文件方法。

Here is an simple example on how this is done.

这里有一个简单的例子说明如何实现这一点。

Step 1: Define the function in the export.h file.

步骤1:在导出中定义函数。h文件。

int WINAPI IsolatedFunction(const char *title, const char *test);

Step 2: Define the function in the export.cpp file.

步骤2:在导出中定义函数。cpp文件。

#include 

int WINAPI IsolatedFunction(const char *title, const char *test)
{
    MessageBox(0, title, test, MB_OK);
    return 1;
}

Step 3: Define the function as an export in the export.def defintion file.

步骤3:将函数定义为出口.def定义文件中的导出。

EXPORTS    IsolatedFunction          @1

Step 4: Create a DLL project and add the export.cpp and export.def files to this project. Building this project will create an export.dll and an export.lib file.

步骤4:创建一个DLL项目并添加导出。cpp和export.def文件到这个项目。构建这个项目将创建一个导出。dll和出口。lib文件。

The following two steps link to the DLL at link time. If you don't want to define the entry points at link time, ignore the next two steps and use the LoadLibrary and GetProcAddress to load the function entry point at runtime.

以下两个步骤在链接时链接到DLL。如果您不想在链接时定义入口点,请忽略接下来的两个步骤,并使用LoadLibrary和GetProcAddress在运行时加载函数入口点。

Step 5: Create a Test application project to use the dll by adding the export.lib file to the project. Copy the export.dll file to ths same location as the Test console executable.

步骤5:创建一个测试应用程序项目,通过添加导出来使用dll。项目的库文件。复制导出。dll文件到与测试控制台可执行文件相同的位置。

Step 6: Call the IsolatedFunction function from within the Test application as shown below.

步骤6:从测试应用程序中调用隔离函数,如下所示。

#include "stdafx.h"

// get the function prototype of the imported function
#include "../export/export.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // call the imported function found in the dll
    int result = IsolatedFunction("hello", "world");

    return 0;
}

#2


9  

Can also export functions from dll and import from the exe, it is more tricky at first but in the end is much easier than calling LoadLibrary/GetProcAddress. See MSDN.

也可以从dll中导出函数并从exe中导入函数,这在一开始是比较棘手的,但最终比调用LoadLibrary/GetProcAddress要容易得多。看到MSDN。

When creating the project with the VS wizard there's a check box in the dll that let you export functions.

在使用VS向导创建项目时,dll中的一个复选框允许您导出函数。

Then, in the exe application you only have to #include a header from the dll with the proper definitions, and add the dll project as a dependency to the exe application.

然后,在exe应用程序中,您只需要在dll中包含一个包含适当定义的标题,并将dll项目添加为exe应用程序的依赖项。

Check this other question if you want to investigate this point further Exporting functions from a DLL with dllexport.

如果您想进一步研究这一点,请检查另一个问题,即从使用dllexport的DLL中导出函数。

#3


6  

You can either go the LoadLibrary/GetProcAddress route (as Harper mentioned in his answer, here's link to the run-time dynamic linking MSDN sample again) or you can link your console application to the .lib produced from the DLL project and include the hea.h file with the declaration of your function (as described in the load-time dynamic linking MSDN sample)

您可以使用LoadLibrary/GetProcAddress路径(正如Harper在他的回答中提到的,这里是到运行时动态链接MSDN示例的链接),也可以将控制台应用程序链接到从DLL项目生成的.lib并包含hea。带有函数声明的h文件(如载时动态链接MSDN示例所述)

In both cases, you need to make sure your DLL exports the function you want to call properly. The easiest way to do it is by using __declspec(dllexport) on the function declaration (as shown in the creating a simple dynamic-link library MSDN sample), though you can do it also through the corresponding .def file in your DLL project.

在这两种情况下,您都需要确保您的DLL输出您想要正确调用的函数。最简单的方法是在函数声明中使用__declspec(dllexport)(如创建一个简单的动态链接库MSDN示例所示),不过你也可以通过DLL项目中相应的.def文件来实现。

For more information on the topic of DLLs, you should browse through the MSDN About Dynamic-Link Libraries topic.

有关dll主题的更多信息,请浏览MSDN关于动态链接库主题。

#4


3  

The following are the 5 steps required:

以下是所需的5个步骤:

  1. declare the function pointer
  2. 声明函数指针
  3. Load the library
  4. 加载库
  5. Get the procedure address
  6. 得到程序地址
  7. assign it to function pointer
  8. 将它赋值给函数指针。
  9. call the function using function pointer
  10. 使用函数指针调用函数

You can find the step by step VC++ IDE screen shot at http://www.softwareandfinance.com/Visual_CPP/DLLDynamicBinding.html

您可以在http://www.softwareandfinance.com/Visual_CPP/DLLDynamicBinding.html中找到逐步的vc++ IDE截图

Here is the code snippet:

下面是代码片段:

int main()
{
/***
__declspec(dllimport) bool GetWelcomeMessage(char *buf, int len); // used for static binding
 ***/
    typedef bool (*GW)(char *buf, int len);

    HMODULE hModule = LoadLibrary(TEXT("TestServer.DLL"));
    GW GetWelcomeMessage = (GW) GetProcAddress(hModule, "GetWelcomeMessage");

    char buf[128];
    if(GetWelcomeMessage(buf, 128) == true)
        std::cout <

#5


0  

Presuming you're talking about dynamic runtime loading of DLLs, you're looking for LoadLibrary and GetProAddress. There's an example on MSDN.

假设您正在讨论dll的动态运行时加载,那么您正在寻找LoadLibrary和GetProAddress。在MSDN上有一个例子。

#6


0  

When the DLL was created an import lib is usually automatically created and you should use that linked in to your program along with header files to call it but if not then you can manually call windows functions like LoadLibrary and GetProcAddress to get it working.

当DLL被创建时,通常会自动创建一个导入库,您应该使用它和头文件链接到您的程序来调用它,但是如果没有,您可以手动调用windows函数,如LoadLibrary和GetProcAddress,以使其工作。


推荐阅读
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
author-avatar
赵小坑_38825
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有