1

正如标题所说,虽然我想我真正的意思是“以后再使用它们”。

设置

我遵循了这个答案:

https://stackoverflow.com/a/13219631/696407

它创建了一个非常简单的 dll

#include <stdio.h>
extern "C"
{
    __declspec(dllexport) void DisplayHelloFromMyDLL()
    {
        printf ("Hello DLL.\n");
    }
}

我现在有一个为发布而编译的 dll:

  • DLLTest.dll
  • DLLTest.exp
  • DLLTest.lib
  • DLLTest.pdb

当我通过 dumpbin 运行 DllTest.dll 时,我发现这一行:

1    0 00001000 DisplayHelloFromMyDLL = _DisplayHelloFromMyDLL

使用 DLL

要在新解决方案中使用该功能,我相信我必须

  1. 在新解决方案中启动项目
  2. 将DLL的位置添加到项目下
    • 特性
      • 配置属性
        • 链接器
          • 一般的
            • 其他图书馆目录
  3. 在下面添加 .lib 文件
    • 特性
      • 配置属性
        • 链接器
          • 输入
            • 附加依赖项

并且,在那里添加了.lib,下一步是...... hvæt?

我现在的代码:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    while(1)
    {
        DisplayHelloFromMyDLL();
    }
    return 0;
}

但这不起作用。

编辑:我猜“不起作用”是模糊的。函数得到Error: identifier "DisplayHelloFromMyDLL" is undefined

(附带问题:我的函数是调用DisplayHelloFromMyDLL();还是_DisplayHelloFromMyDLL();?)

4

2 回答 2

2

You need .h for compiler (use with #include, and add the folder to .h file as relative path to Configuration Properties > C/C++ > General > Additional Include Directories). Aside from .lib for linker you also need .dll to actually run the test application.

EDIT: There are two types of DLL's that you can make. First are C-like DLL's, with functions that have signatures as if they are written in C instead of in C++. All Windows DLL's (user32.dll, shell32.dll, version.dll) are built as such. The other are C++ DLL's, with functions that are part of the class. MFC and Standard C++ Libraries are such.

If you want to make a C++ DLL then you have to declare all classes that are part of interface as __declspec(dllexport) in your DLL project and __declspec(dllimport) in all projects that would use DLL. Usually the same file is used for this, but with a macro that is defined accordingly to one or the other. If you create a DLL from Visual Studio project template you would see this code.

Your case is actually the simpler case, as you want C-like DLL. You don't have to fiddle with this __declspec rubbish, but you need one additional .def file in DLL project. This should be the content of the .def file:

LIBRARY MyApi

EXPORTS
    DisplayHelloFromMyDLL

Your header file (.h file) should look like this:

#pragma once

#ifndef HELLO_DLL_INCLUDED
#define HELLO_DLL_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

void DisplayHelloFromMyDLL();

#ifdef __cplusplus
};
#endif

#endif // HELLO_DLL_INCLUDED

__declspec(dllimport) tells the compiler that this function (or class) is defined somewhere else, and that linker will find it and link it. __declspec(dllexport) tells the compiler (and linker) that this function (or class) should be exported and be part of DLL interface. If class has neither of those then it's just a class that should be defined in the same project.

于 2013-09-06T09:36:51.457 回答
0

要使用您的 .dll,您需要两个东西,一个头文件和一个 .lib。

头文件是为了让编译器知道某处有一个名为 的函数DisplayHelloFromMyDLL()。在这一点上,它在哪里并不重要,只是你已经告诉编译器它在某个地方。链接器将处理 where 位。

.lib 文件用于链接器。它告诉链接器DisplayHelloFromMyDLL()位于 .dll 中,并且(在您的情况下)该 dll 的名称是 DllTest.dll。当您的程序启动时,Windows 加载程序将使用此信息将 .dll 加载到您的进程中,并将执行任何地址修复以确保DisplayHelloFromMyDLL()在您的应用程序中调用调用您的 .dll 中的函数。

您实际上并不需要 .dll 来构建可执行文件,只需运行它即可。

于 2013-09-06T09:54:10.113 回答