简体   繁体   English

C ++ API DLL项目

[英]C++ API DLL Project

I have a C++ project which uses an API that comes with DLL and LIB files, as well as a header file. 我有一个C ++项目,该项目使用DLL和LIB文件以及头文件随附的API。 I want to create a win32 forms project. 我想创建一个win32窗体项目。

My question is how do I link the project with the dll and lib files. 我的问题是如何将项目与dll和lib文件链接。

But the main question is, how do I subscribe to events? 但是主要的问题是,我该如何订阅活动? In the API I can call functions, which come back as on event response, which in a MFC project I would generally subscribe in the message loop. 在API中,我可以调用函数,这些函数在事件响应时返回,而在MFC项目中,我通常会在消息循环中进行订阅。 But in the Win32 there is no message loop. 但是在Win32中没有消息循环。

Maybe I am complicating the situation myself and it is easier than I imagine. 也许我自己使情况复杂化了,这比我想象的容易。 Please clarify this situation for me. 请为我澄清这种情况。

Thank you. 谢谢。

It's always hard to answer when someone asks more than one question... In the case of your "main question", please show the relevant API call for subscribing to events. 当有人问多个问题时,总是很难回答...就您的“主要问题”而言,请显示相关的API调用以订阅事件。 You are wrong about there being no message loop in Win32... It's just that there's no MFC message loop. 您认为Win32中没有消息循环是错误的...只是没有MFC消息循环。

I'm answering the easy question for now... "how do you link the DLL". 我现在正在回答一个简单的问题……“如何链接DLL”。 I am kinda assuming you are using Visual Studio. 我有点假设您正在使用Visual Studio。 Is that correct? 那是对的吗? The way I do this, when I'm in a hurry is like so: 我急着想的时候是这样的:

#include "mylib.h"
#pragma comment(lib, "mylib.lib")

That's all there is to it. 这里的所有都是它的。 Of course I want the header, but while I'm at it I tell the compiler to link the library too. 当然我想要头文件,但是当我在头文件时,我告诉编译器也链接库。

I seem to recall other times where I simply dragged the library file into the project's solution view, effectively adding it as a source. 我似乎还记得其他时候,我只是将库文件拖到项目的解决方案视图中,有效地将其添加为源。 That also works. 那也行。

The long way is to edit your project settings, go to the Linker section, and under the Input subsection, you add your library to the Additional Dependencies list. 一种很长的方法是编辑项目设置,转到“ 链接器”部分,然后在“ 输入”子部分下,将库添加到“ 其他依赖项”列表中。 You need to do that for all configurations. 您需要对所有配置执行此操作。 There are ways to maintain this properly but I won't go into it here. 有一些方法可以适当地维护它,但我这里不再赘述。

If you are not using Visual Studio, please disregard my answer. 如果您不使用Visual Studio,请忽略我的回答。

You cannot link the project with dll files, you could link your project with lib files, you could add lib file names in your project settings dialog (linker) 您不能将项目与dll文件链接,可以将项目与lib文件链接,可以在项目设置对话框(链接器)中添加lib文件名

Or, you add this to your code 或者,您将其添加到代码中

#pragma comment(lib,"xxxx.lib")

Dll files will be loaded at run-time. DLL文件将在运行时加载。

For message loop, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644928(v=vs.85).aspx 有关消息循环,请访问http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms644928(v=vs.85).aspx

Or this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644996(v=vs.85).aspx#modeless_box 或这样: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms644996(v=vs.85).aspx#modeless_box

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM