从两个不同的C调用的DLL具有相同名称的两个函数 [英] Calling two functions with the same name from two different C DLLs

查看:172
本文介绍了从两个不同的C调用的DLL具有相同名称的两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个C的DLL,我需要在同一个可执行文件访问。我有头文件和.lib文件这两个库。不幸的是,我需要访问功能的一个子集有相同的名字。我已经能够拿出迄今最好的解决方案是使用LoadLibrary来加载这些DLL之一,并明确使用GetProcAddress的调用它的方法。有没有办法对我来说,隐式加载这两个库,并以某种方式给编译器一个暗示,在一个情况下,我想调用的OpenAPI在DLL中的一个,并在其他情况下,我想调用的OpenAPI在DLL B'

我发展我的C ++使用Visual Studio 2008的可执行文件和相应的C运行时库(MSVCR90.DLL)。

评论者伊利亚下面问什么我不喜欢有关GetProcAddress的解决方案。我不喜欢它的原因有两个:


  1. 这使得code更加复杂。 code的一行调用一个函数替换三行code,一来定义函数签名,一个调用GetProcAddress,一个实际调用该函数。

  2. 这是更容易出现运行时错误。如果我拼错函数名或弄乱签名我看不到错误,直到运行时。说我决定集成DLL和方法名的一个新版本已经改变,它会编译就好了,不会有问题,直到实际调用GetProcAddress的,这有可能即使是在测试合格错过。


解决方案

它曾经是,你可以重命名使用的接头DEF文件导入符号。你或许还可以,但它已经这么久以来的.def文件已被广泛使用,这是很难找到的文档。

目前的MSDN文档列出了Imports指令为保留关键字。我不知道这是否意味着他们删除的功能,或者他们只是不希望支持它了。

下面的描述Imports指令页面上:

http://www.digitalmars.com/ctg/ctgDefFiles.html#imports

其他的缺憾替代品是:


  1. 创建包装功能冲突的API。这些功能可以做调用LoadLibrary()/ GetProcAddress的()舞蹈。所有其它非冲突的功能可以被隐式链接为正常。其实,这种解决方案可能是最缺憾3的这个答案。


  2. 创建2个包装的DLL,这样每个环节只有一个或与冲突的名字其他库。使用在包装的DLL,简单地咚到实体图书馆的不同的名字。注意包装图书馆不需要包装的所有API的 - 他们只需要包装冲突的人


I have two C DLLs that I need to access in the same executable. I have header files and .LIB files for both libraries. Unfortunately a subset of the functions that I need to access have the exact same names. The best solution I have been able to come up with so far is to use LoadLibrary to load one of the DLLs and explicitly call its methods using GetProcAddress. Is there a way for me to implicitly load both libraries and somehow give the compiler a hint that in one case I want to call OpenApi in DLL A and in the other case I want to call OpenApi in DLL B?

I'm developing my executable in C++ using Visual Studio 2008 and the corresponding C runtime library (msvcr90.dll).

[Edit]

Commenter Ilya asks below what I don't like about the GetProcAddress solution. I don't like it for two reasons:

  1. It makes the code more complex. One line of code to call a function is replaced with three lines of code, one to define the function signature, one to call GetProcAddress, and one to actually call the function.
  2. It's more prone to run-time errors. If I misspell the function name or mess up the signature I don't see the error until run-time. Say I decide to integrate a new version of the dll and one of the method names has changed, it will compile just fine and won't have a problem until the actual call to GetProcAddress, which could possibly even be missed in a test pass.

解决方案

It used to be you could 'rename' an imported symbol using a linker .def file. You probably still can, but it's been so long since .def files have been widely used, it's difficult to locate documentation.

The current MSDN documentation lists the IMPORTS directive as a 'reserved keyword'. I'm not sure if this means they removed the functionality, or if they just don't want to support it anymore.

Here's on page that describes the IMPORTS directive:

http://www.digitalmars.com/ctg/ctgDefFiles.html#imports

Other kludgy alternatives are:

  1. create wrapper functions for the conflicting APIs. Those functions can do the LoadLibrary()/GetProcAddress() dance. All other non-conflicting functions can be implicitly linked as normal. Actually, this solution is probably the least kludgy of the 3 in this answer.

  2. create 2 wrapper DLLs such that each links only to one or the other library with the conflicting names. Use differing names in the wrapper DLLs that simply thunk over to the real librarys. Note the wrapper libraries do not need to wrap all the API's - they just need to wrap the conflicting ones.

这篇关于从两个不同的C调用的DLL具有相同名称的两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆