如何使用在Visual Studio中明确加载的C ++类dll [英] How to use a C++ class dll loaded explicitly in visual studio
问题描述
正是出于这里提到的原因:
http:/ /msdn.microsoft.com/en-us/library/vstudio/253b8k2c.aspx
应用程序不知道
我需要加载一个dll不能将其名称绑定到应用程序。也就是说,我不需要应用程序要求myDll.dll工作(因为在我们的配置系统中,myDll.dll不像这样命名)。但是,对于每个函数使用 GetProcAddress 看起来不是一个好主意,特别是因为它需要修饰的名称,这是容易出错的。
我想知道是否有一种方法可以继续使用__declspec(dllimport)或类似的没有dll名称绑定。
我最后的手段是创建一个C接口和使用 GetProcAddress 的类,但我认为应该有更好的方法。
编辑
我应该注意,我可以编辑库的.cpp和.h,创建一个.lib等。
我甚至(但这是非常具体的这个应用程序)创建一个类的对象包含在dll(我们有一些钩子为此)。但是,我不能使用我的dll类的头,因为那样它需要我加载myDll.dll
解决方案使用dumpbin程序列出导出的符号:
dumpbin / exports OLDNAME.dll
您将得到这种输出:
Microsoft(R)COFF / PE Dumper版本10.00.40219.01
版权所有(C)Microsoft Corporation。版权所有。
文件转储OLDNAME.dll
文件类型:DLL
此部分包含以下对OLDNAME.dll的导出
00000000特性
529B7ABB时间日期戳记Sun Dec 01 19:06:51 2013
0.00版本
1序数基数
1函数数量
1名称数量
序数提示RVA名称
1 0 00011109?MtDlladd @@ YAHHH @ Z = @ ILT + 260(?MtDlladd @@ YAHHH @ Z)
摘要
1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
4000 .text
10000 .textbss
现在创建一个包含以下内容的新文本文件: / p>
LIBRARY NEWNAME
EXPORTS
?MtDlladd @@ YAHHH @ Z
将NEWNAME替换为有问题的DLL的最终名称。
在EXPORTS下,复制由implib给出的所有函数名称。
将文本文件命名为NEWNAME.DEF
使用
lib :
lib /def:NEWNAME.DEF /OUT:NEWNAME.lib
现在您可以链接到该库,您的应用程序将需要NEWNAME.DLL
For exactly the reason mentioned here: http://msdn.microsoft.com/en-us/library/vstudio/253b8k2c.aspx
"The application does not know the name of a DLL that it will have to load until run time"
I need to load a dll that doesn't bind its name to the application. That is, I don't need the application to require "myDll.dll" to work (because in our configuration system, myDll.dll is not named like that). However, using GetProcAddress for every function doesn't seems like a good idea, specially since it needs the decorated names, and that's error prone.
I was wondering if there's a way to continue using __declspec(dllimport) or something similar without the dll name binding.
My last resort is to create a C interface and a class that uses GetProcAddress, but I think there should be a better way.
Edit:
I should note that I can edit the .cpp and .h of the library, create a .lib, etc.
I can even (but this is very specific to this application) create an object of the class contained in the dll (we have some hooks for this). However, I can't use the header of my dll class, because then it requires that I load "myDll.dll"
解决方案Use the dumpbin program to list the exported symbols:
dumpbin /exports OLDNAME.dll
You will get that kind of output:
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file OLDNAME.dll File Type: DLL Section contains the following exports for OLDNAME.dll 00000000 characteristics 529B7ABB time date stamp Sun Dec 01 19:06:51 2013 0.00 version 1 ordinal base 1 number of functions 1 number of names ordinal hint RVA name 1 0 00011109 ?MtDlladd@@YAHHH@Z = @ILT+260(?MtDlladd@@YAHHH@Z) Summary 1000 .data 1000 .idata 2000 .rdata 1000 .reloc 1000 .rsrc 4000 .text 10000 .textbss
Now create a new text file with the following contents:
LIBRARY NEWNAME EXPORTS ?MtDlladd@@YAHHH@Z
Replace NEWNAME with the final name of your problematic DLL. Under EXPORTS, copy all the function names given by implib, as is. Name that text file NEWNAME.DEF
Make a new lib file with that DEF file, using
lib
:lib /def:NEWNAME.DEF /OUT:NEWNAME.lib
Now you can link with that lib, and your application will require NEWNAME.DLL
这篇关于如何使用在Visual Studio中明确加载的C ++类dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!