windows 如何从另一个 C++ win32 控制台应用程序调用 C++ Win32 DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3572874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:06:46  来源:igfitidea点击:

How Can I call a C++ Win32 DLL from another C++ win32 Console Application

c++windowsvisual-c++dllimportdllexport

提问by Simsons

What is my main concern is , I am able to write a C++ dll using VC++ . Now the dll is present in the Debug folder.

我主要关心的是,我能够使用 VC++ 编写 C++ dll。现在 dll 出现在 Debug 文件夹中。

How can I use my DLL in other C++ Console Application. How to add reference or link the DLL to the application.

如何在其他 C++ 控制台应用程序中使用我的 DLL。如何添加引用或将 DLL 链接到应用程序。

Another point, While creating a DLL , The VC++ wizard gives me thre option:

另一点,在创建 DLL 时,VC++ 向导给了我三个选项:

  1. An Empty DLL project
  2. A Simple DLL project
  3. A DLL that Exports some Symbol
  1. 一个空的 DLL 项目
  2. 一个简单的 DLL 项目
  3. 导出一些符号的 DLL

Now as per article from CPI have used the 3rd option. (Unable to follow as the dll was used by an MFC app, Some how little advanced at this point of time)

现在根据CP 的文章,我使用了第三个选项。(无法跟踪,因为 dll 被 MFC 应用程序使用,在这个时间点有点先进

Do I need to always choose the third option? What are the the other two options mean?

我需要总是选择第三个选项吗?另外两个选项是什么意思?

回答by Elemental

Not completely sure what you questions are but:

不完全确定您的问题是什么,但是:

It doesn't really matter which option you use it is just a matter of what the wizard does for you; if you use the third option then the wizard creates a bit in your header file that looks like this:

你使用哪个选项并不重要,它只是向导为你做什么的问题;如果您使用第三个选项,则向导会在您的头文件中创建一个如下所示的位:

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

The way this works is that in the DLL project TEST_EXPORTS is defined in the compiler options so TEST_API evaluates to dllexport thus telling the linker to export these symbols. If you include this header in another project it defines TEST_API as dllimport which tells the compiler to link to it at run-time in the DLL. This #define method of exporting symbols is widely used and easy to read.

它的工作方式是在 DLL 项目中 TEST_EXPORTS 在编译器选项中定义,因此 TEST_API 评估为 dllexport,从而告诉链接器导出这些符号。如果你在另一个项目中包含这个头文件,它会将 TEST_API 定义为 dllimport,它告诉编译器在运行时在 DLL 中链接到它。这种导出符号的#define 方法被广泛使用且易于阅读。

In order to call a function/class inside the DLL you need to export the symbols one of two ways: a) using the __declspec(dllexport) [this seems the more convenient option in almost all cases ]OR b) using a .DEF file in your project

为了在 DLL 中调用函数/类,您需要通过以下两种方式之一导出符号:a) 使用 __declspec(dllexport) [这在几乎所有情况下似乎都是更方便的选项]或 b) 使用 .DEF 文件在你的项目中

Also wanted to mention that you need to include either the DLL project in your solution for the .exe file OR the .lib generated by the DLL compile.

还想提一下,您需要在 .exe 文件的解决方案中包含 DLL 项目或由 DLL 编译生成的 .lib。

回答by Alex F

You can use "A DLL that Exports some Symbol" to learn how Dll project should be built. Once you understand this, use "A Simple DLL project". You can prefer to start always with "A DLL that Exports some Symbol", and change the code generated by Wizard, replacing sample exported class/function/symbol with your own code.

您可以使用“导出某些符号的 DLL”来了解应如何构建 Dll 项目。一旦你理解了这一点,使用“一个简单的 DLL 项目”。您可以更喜欢始终从“导出某个符号的 DLL”开始,然后更改向导生成的代码,用您自己的代码替换示例导出的类/函数/符号。

To reference .Dll project from a client project, add .lib file to the client project linker dependencies: Project - Properties - Linker - Input - Additional Dependencies. To ensure that .lib file can be found by linker, add directory where .lib file is placed, to the linker directories list. There are two places this can be done: locally in the client project (Project - Properties - Linker - General - Additional library directories) and globally, for all VC++ projects (Tools - Options - VC++ Directories - Libraries).

要从客户端项目引用 .Dll 项目,请将 .lib 文件添加到客户端项目链接器依赖项:项目 - 属性 - 链接器 - 输入 - 附加依赖项。为确保链接器可以找到 .lib 文件,请将放置 .lib 文件的目录添加到链接器目录列表中。有两个地方可以做到这一点:在客户端项目本地(项目 - 属性 - 链接器 - 常规 - 附加库目录)和全局,对于所有 VC++ 项目(工具 - 选项 - VC++ 目录 - 库)。

The last thing is to ensure that DLL can be loaded by client .exe at runtime. Dll must be in the current directory, executable directory, Windows, system directory, or available through PATH variable.

最后一件事是确保 DLL 可以在运行时被客户端 .exe 加载。dll必须在当前目录、可执行目录、Windows、系统目录下,或者通过PATH变量可用。

回答by Swapnil

The DLL can be imported by specifying it as a dependency in project settings of the Console application within Visual Studio as described by Alex Farber. You have to make sure that the application is able to find the DLL by placing the DLL at any location specified the PATH variable. You can also load the DLL programmatically within you application using LoadLibrary method (see documentation here http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx) and call a method exported within the DLL using function GetProcAddress ( refer http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx).

可以通过在 Visual Studio 中控制台应用程序的项目设置中将其指定为依赖项来导入 DLL,如 Alex Farber 所述。您必须通过将 DLL 放置在 PATH 变量指定的任何位置来确保应用程序能够找到 DLL。您还可以使用 LoadLibrary 方法在应用程序中以编程方式加载 DLL(请参阅此处的文档http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx)并调用在 DLL 中导出的方法使用函数 GetProcAddress(请参阅http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx)。

回答by Subodha Charles

Go through the following steps to set up two projects. http://msdn.microsoft.com/en-us/library/ms235636.aspx

通过以下步骤来设置两个项目。 http://msdn.microsoft.com/en-us/library/ms235636.aspx

In those instructions, the new project is added to the same solution that contains the DLL. This might not be the case in most of the situations. If you have two separate projects which you want to handle, slightly alter the above procedure as follows.

在这些说明中,新项目被添加到包含 DLL 的同一解决方案中。在大多数情况下,情况可能并非如此。如果您要处理两个单独的项目,请按如下方式稍微更改上述步骤。

  1. In the DLL project, make sure you have configured to create a DLL file. To do that, click on the project in the solutions explorer and go to properties. Under Configuration Properties >> General, change the Configuration Type to "Dynamic Library (.dll)". Now, compile the dynamic link library by choosing Build, Build Solution on the menu bar.

  2. Create the other project and the files as mentioned in the link. To use the dll in the app, you must reference it. To do this, add the lib file created from the dynamic library. The lib file is created in the same folder as the dll. If Visual studio was used in the debug mode, it will be in Project>>Folder>>Debug. If Release mode was used, Project Folder>>Release. To use the lib file in the app, Go to Project >> Properties >> Linker >> Input >> Additional Dependencies and add the name of the lib file to the list ("AFR24x7.lib" in my case).

  3. To ensure that .lib file can be found by linker, add directory where .lib file is placed, to the linker directories list. There are two places this can be done: locally in the client project (Project>>Properties>>Linker>>General>>Additional library directories) and globally, for all VC++ projects (Tools>>Options>>VC++ Directories>>Libraries).

  4. Add the include file as mentioned in the link.

  5. Copy the created DLL file and paste it in the app's release and debug folders (If you are using both of them).

  6. Complete the rest of the steps other than setting the dependencies given in the link.

  1. 在 DLL 项目中,确保您已配置为创建 DLL 文件。为此,请单击解决方案资源管理器中的项目并转到属性。在 Configuration Properties >> General 下,将 Configuration Type 更改为“Dynamic Library (.dll)”。现在,通过在菜单栏上选择 Build、Build Solution 来编译动态链接库。

  2. 创建链接中提到的另一个项目和文件。要在应用程序中使用 dll,您必须引用它。为此,请添加从动态库创建的 lib 文件。lib 文件创建在与 dll 相同的文件夹中。如果在调试模式下使用 Visual Studio,它将在项目>>文件夹>>调试中。如果使用发布模式,项目文件夹>>发布。要在应用程序中使用 lib 文件,请转到 Project >> Properties >> Linker >> Input >> Additional Dependencies 并将 lib 文件的名称添加到列表中(在我的情况下为“AFR24x7.lib”)。

  3. 为确保链接器可以找到 .lib 文件,请将放置 .lib 文件的目录添加到链接器目录列表中。有两个地方可以做到这一点:在客户端项目本地(项目>>属性>>链接器>>常规>>附加库目录)和全局,对于所有 VC++ 项目(工具>>选项>>VC++ 目录>>库) )。

  4. 添加链接中提到的包含文件。

  5. 复制创建的 DLL 文件并将其粘贴到应用程序的发布和调试文件夹中(如果您同时使用它们)。

  6. 完成除设置链接中给出的依赖项以外的其余步骤。

Hope this will help.

希望这会有所帮助。