简体   繁体   English

如何在Windows 10 Visual Studio中使用必需的.dll进行构建

[英]How to build with required `.dll` in Windows 10 Visual Studio

I'm trying to build a project. 我正在尝试建立一个项目。 I have a.lib file that I need to use in my project. 我有一个a.lib文件,需要在我的项目中使用。 I know that there are two ways to use this lib: 我知道使用此库有两种方法:

  • add it using #pragma comment(lib, "a.lib") 使用#pragma comment(lib, "a.lib")添加它
  • add it to linker dependencies Configuration Properties -> Linker -> Input -> Additional Dependencies 将其添加到链接器依赖项Configuration Properties -> Linker -> Input -> Additional Dependencies

Now, a.lib uses StackWalk64 function DbgHelp . 现在, a.lib使用StackWalk64函数DbgHelp This library is supplied as DbgHelp.lib and DbgHelp.dll . 该库以DbgHelp.libDbgHelp.dll I know that I can use it as a lib using two ways listed above. 我知道我可以使用上面列出的两种方法将它用作lib But what if I don't want to include it into my project and want to use DbgHelp.dll , how can I do that in Visual Studio? 但是,如果我不想将其包含在我的项目中并想使用DbgHelp.dll ,该怎么办?如何在Visual Studio中做到这一点?

If you want to call a function that is within a DLL, but don't want to link to the LIB file that imports these functions for you, then you can use LoadLibrary and GetProcAddress . 如果要调用DLL中的函数,但又不想链接到为您导入这些函数的LIB文件,则可以使用LoadLibraryGetProcAddress (Though if you have the import library and can link to it, why do you want to load these functions manually?) (尽管如果您具有导入库并可以链接到导入库,为什么要手动加载这些功能?)

IF YOU HAVE THE DbgHelp.lib IMPORT LIBRARY, USE IT! 如果您DbgHelp.lib导入库,请使用它! LOADING FUNCTIONS MAUNALLY IS ERROR-PRONE IF NOT DONE RESPONSIBLY. 如果不负责任的话,加载功能会出错。 USE WITH CAUTION! 小心使用!

// Type definition for a function pointer that can call the function
typedef BOOL (WINAPI *StackWalk64_func)
(
    DWORD,
    HANDLE,
    HANDLE,
    LPSTACKFRAME64,
    PVOID,
    PREAD_PROCESS_MEMORY_ROUTINE64,
    PFUNCTION_TABLE_ACCESS_ROUTINE64,
    PGET_MODULE_BASE_ROUTINE64,
    PTRANSLATE_ADDRESS_ROUTINE64
);

// Within a function . . .

HMODULE hDbgHelpDll = LoadLibrary(TEXT("DbgHelp.dll"));
if (hDbgHelpDll == NULL)
{
    // handle error and return
}

StackWalk64_func funStackWalk64
    = (StackWalk64_func)GetProcAddress(hDbgHelpDll, "StackWalk64");

if (funStackWalk64 == NULL)
{
    // handle error and return
}

// funStackWalk64 is valid and ready to use

Now you can call funStackWalk64 like the function StackWalk64 , and pass the function pointer around the place. 现在,您可以像函数StackWalk64一样调用funStackWalk64 ,并将函数指针传递到该位置。 When you're done using the library, you should free the module handle: 使用完库后,应释放模块句柄:

FreeLibrary(hDbgHelpDll);

Usually (and in Visual C++ specifically,) the lib file that comes with a dll is what is called an "import library". 通常(特别是在Visual C ++中), dll附带的lib文件就是所谓的“导入库”。 It means that the library has no actual function bodies in it; 这意味着该库中没有实际的功能主体; it is just there to appease the linker and instruct it to generate an EXE (or DLL) that would use the first dll at load time . 它只是在这里安抚链接器并指示它生成一个EXE(或DLL),该EXE将在加载时使用第一个dll

It is so in this case too. 在这种情况下也是如此。 Since you don't have access to the source code for DbgHelp so that you can build it as a real static library, you need to make do with the small import library at link time and the dll file at load/run time. 由于您无权访问DbgHelp的源代码,因此无法将其构建为真正的静态库,因此需要在链接时使用小型导入库,并在加载/运行时使用dll文件。

Note: the whole linking and dynamic linking concepts and mechanisms are obviously a lot more complex than what I have room here to discuss. 注意:整个链接和动态链接的概念和机制显然比我这里讨论的要复杂得多。 So, the explanation above is quite narrow and specific to your question. 因此,以上解释非常狭窄,仅针对您的问题。

Please read up: http://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/ 请阅读: http : //www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/

Implicit Linkage with an import library (using .lib) 与导入库的隐式链接(使用.lib)

In this case the static-library is an "import library" , which automates the process of determining the effective functions in the DLL. 在这种情况下,静态库是一个“导入库” ,它可以自动确定DLL中的有效功能。 This is called implicit dynamic linkage . 这称为隐式动态链接

Explicit Linkage 显式链接

If you don't want to use the import library you have to determine all functions by yourself, create corresponding pointers to the addresses of the procedures and use them after that. 如果您不想使用导入库,则必须自己确定所有功能,请创建指向过程地址的相应指针,然后再使用它们。 Usually there's some InitDLL() function in your client code, which does this. 通常,您的客户端代码中有一些InitDLL()函数可以执行此操作。

See: https://msdn.microsoft.com/de-de/library/64tkc9y5.aspx 请参阅: https : //msdn.microsoft.com/de-de/library/64tkc9y5.aspx

The "GetProcAddress"-function can be used to obtain a handle to the function and call it. “ GetProcAddress”功能可用于获取该函数的句柄并调用它。

This is called explicit dynamic linkage and requires also the calls to LoadLibrary() and FreeLibrary() on Windows. 这称为显式动态链接,并且还要求在Windows上调用LoadLibrary()和FreeLibrary()。

More Info: http://www.equestionanswers.com/dll/what-is-implicit-and-explicit-linking-in-dynamic-loading.php 更多信息: http : //www.equestionanswers.com/dll/what-is-implicit-and-explicit-linking-in-dynamic-loading.php

Explicit Linkage on Linux Linux上的显式链接

For linux/unix things work differently. 对于linux / unix,工作原理有所不同。 If you want to read up: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html 如果您想阅读: http : //www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

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

相关问题 CMake 使用 Visual Studio 在 Windows 10 上构建问题 - CMake Build Issues on Windows 10 with Visual Studio 如何设置Visual Studio将所需的DLL文件复制到发行版本的发行目录中? - How to setup Visual Studio to copy required DLL files into the release directory in a release build? Windows 8.1项目的Windows 10 Visual Studio 2013生成错误 - Windows 10 Visual Studio 2013 build error for Windows 8.1 project 使用Visual Studio 2015为Windows 7和Windows 10构建应用程序 - Build applications for both Windows 7 and Windows 10 with Visual Studio 2015 无法使用Visual Studio 2015在Windows 10上构建LLVM - Cannot build LLVM on Windows 10 using Visual Studio 2015 在 Visual Studio 2019 中构建与 Windows XP、7 和 10 兼容的项目 - Build project compatible with Windows XP, 7 and 10 in Visual Studio 2019 如何在Windows 10上使用Visual Studio 2015 x64配置和构建Tesseract OCR C ++ - How to configure and build Tesseract OCR C++ using Visual Studio 2015 x64 on Windows 10 如何使用 Visual Studio 2017 在 Windows 上构建 OpenSSL? - How to build OpenSSL on Windows with Visual Studio 2017? 如何以与Visual Studio中相同的方式在Qt Creator中生成dll? - How to build dll in Qt creator the same way as in Visual Studio? 如何在独立的dll中构建visual studio 2010解决方案? - How to build a visual studio 2010 solution into a standalone dll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM