我如何呼叫 C++来自另一个 C++ 的 Win32 DLL win32 控制台应用程序

发布于 2024-09-16 05:33:46 字数 434 浏览 8 评论 0 原文

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

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

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

  1. 一个空的 DLL 项目
  2. 一个简单的 DLL 项目
  3. 一个现在导出一些符号的 DLL,

根据 CP 我使用了第三个选项。 (无法理解,因为该 dll 已被 MFC 应用程序使用,此时进步一点点

我是否需要始终选择第三个选项?另外两个选项是什么意思?

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.

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

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

  1. An Empty DLL project
  2. A Simple DLL project
  3. A DLL that Exports some Symbol

Now as per article from CP I 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私藏温柔 2024-09-23 05:33:46

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

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

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

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

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

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

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.

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

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.

无所的.畏惧 2024-09-23 05:33:46

您可以使用“A DLL that Exports some Symbol”来了解如何构建Dll项目。一旦理解了这一点,就可以使用“一个简单的 DLL 项目”。您可以选择始终从“导出某些符号的 DLL”开始,并更改向导生成的代码,用您自己的代码替换示例导出的类/函数/符号。

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

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

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.

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).

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.

挽容 2024-09-23 05:33:46

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

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 ).

白鸥掠海 2024-09-23 05:33:46

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

在这些说明中,新项目将添加到同一解决方案中
包含 DLL。大多数情况下可能并非如此
情况。如果您有两个独立的项目
处理,将上述过程稍微改变如下。

  1. 在 DLL 项目中,确保您已配置为创建 DLL
    文件。为此,请单击解决方案资源管理器中的项目,然后
    转到属性。在配置属性>>下一般,改变
    将配置类型设置为“动态库 (.dll)”。现在,编译
    通过选择菜单上的“Build”、“Build Solution”来动态链接库

  2. 创建链接中提到的其他项目和文件。到
    在应用程序中使用dll,您必须引用它。为此,请添加
    从动态库创建的lib文件。 lib文件已创建
    与 dll 在同一文件夹中。如果使用Visual Studio
    调试模式下,将处于“项目”>“文件夹”>“调试”。如果发布模式是
    使用过,项目文件夹>>发布。要在应用程序中使用 lib 文件,请转至
    项目>>属性>>链接器>>输入>>附加依赖项
    并将 lib 文件的名称添加到列表中(我的目录中的“AFR24x7.lib”)
    案例)。

  3. 为了确保链接器可以找到.lib文件,请添加目录
    .lib 文件被放置到链接器目录列表中。有两个
    可以在以下位置完成此操作:在客户端项目本地(项目>>属性>>链接器>>常规>>其他库目录)和
    全局,对于所有 VC++ 项目(工具>>选项>>VC++ 目录>>库)。

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

  5. 复制创建的 DLL 文件并将其粘贴到应用程序的版本中,然后
    debug 文件夹(如果您同时使用这两个文件夹)。

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

希望这会有所帮助。

Go through the following steps to set up two projects.
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.

  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.

Hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文