热门标签 | 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,以使其工作。


推荐阅读
  • 本文探讨了如何通过预处理器开关选择不同的类实现,并解决在特定情况下遇到的链接器错误。 ... [详细]
  • 本文详细介绍了Linux内核中misc设备驱动框架的实现原理及应用方法,包括misc设备的基本概念、驱动框架的初始化过程、数据结构分析以及设备的注册与注销流程。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文深入探讨了如何通过调整InnoDB的关键配置参数来优化MySQL的随机IO性能,涵盖了缓存、日志文件、预读机制等多个方面,帮助读者全面提升数据库系统的性能。 ... [详细]
  • 本文深入探讨了Linux系统中网卡绑定(bonding)的七种工作模式。网卡绑定技术通过将多个物理网卡组合成一个逻辑网卡,实现网络冗余、带宽聚合和负载均衡,在生产环境中广泛应用。文章详细介绍了每种模式的特点、适用场景及配置方法。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 配置多VLAN环境下的透明SQUID代理
    本文介绍如何在包含多个VLAN的网络环境中配置SQUID作为透明网关。网络拓扑包括Cisco 3750交换机、PANABIT防火墙和SQUID服务器,所有设备均部署在ESXi虚拟化平台上。 ... [详细]
  • 本文探讨了如何在iOS开发环境中,特别是在Xcode 6.1中,设置和应用自定义文本样式。我们将详细介绍实现方法,并提供一些实用的技巧。 ... [详细]
  • 本文介绍如何使用MFC和ADO技术调用SQL Server中的存储过程,以查询指定小区在特定时间段内的通话统计数据。通过用户界面选择小区ID、开始时间和结束时间,系统将计算并展示小时级的通话量、拥塞率及半速率通话比例。 ... [详细]
  • 在PHP后端开发中遇到一个难题:通过第三方类文件发送短信功能返回的JSON字符串无法解析。本文将探讨可能的原因并提供解决方案。 ... [详细]
  • springMVC JRS303验证 ... [详细]
  • 树莓派摄像头配置与应用指南
    本文详细介绍了如何在树莓派上配置和使用摄像头,包括启用摄像头接口、简单的图片和视频捕捉方法以及如何通过网络实时传输视频流。 ... [详细]
  • Spring Security核心概念与应用实践
    本文详细介绍了Spring Security的核心机制,包括其作为一系列过滤器的工作原理,如何实现用户认证与授权,以及常见的配置方法和高级特性如CSRF防护。 ... [详细]
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社区 版权所有