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

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

问题描述

我有两个C DLL需要访问同一个可执行文件。我有两个库的头文件和.LIB文件。不幸的是,我需要访问的一部分功能具有完全相同的名称。迄今为止我所能想到的最好的解决方案是使用LoadLibrary加载一个DLL,并使用GetProcAddress显式调用它的方法。有没有办法让我隐含地加载这两个库,并以某种方式给编译器一个提示,在一种情况下,我想在DLL A中调用OpenApi,在另一种情况下,我想在DLL B中调用OpenApi?



我正在使用Visual Studio 2008和相应的C运行时库(msvcr90.dll)在C ++中开发我的可执行文件。





评论者Ilya询问下面我不喜欢GetProcAddress解决方案。我不喜欢它有两个原因:


  1. 它使代码更复杂。调用函数的一行代码被三行代码替换,一行用于定义函数签名,一个用于调用GetProcAddress,一个用于实际调用该函数。

  2. 更容易出现运行时错误。如果我拼写了函数名称或弄乱了签名,我没有看到错误,直到运行时。说我决定整合一个新版本的dll,其中一个方法名称已经改变了,它会编译得很好,直到实际调用GetProcAddress才有可能会遇到问题,甚至可能在测试中错过。


解决方案

以前,您可以使用链接器文件。您可能仍然可以,但是已经很久以来.def文件被广泛使用,很难找到文档。



当前的MSDN文档将IMPORTS指令列为'保留关键字'。我不知道这是否意味着他们删除了这些功能,或者他们只是不想支持它。



这是介绍IMPORTS指令的页面:



http:// www .digitalmars.com / ctg / ctgDefFiles.html#进口



其他kludgy替代方案是:


  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天全站免登陆