dll 中的向后兼容性
我确实有三个dll。
- a.dll - 多年前发布
- b.dll - 发布时间不长
- c.dll - 发布不久
每个都包含相同的函数 - 不幸的是参数不同。 所以我确实有以下方法
aMethod(param1)
aMethod(param1, param2)
aMethod(param1, param2, param3)
我的任务是制作一个向后兼容的新dll(或多个新dll)。 但据我从 Google 了解到,不可能重载 dll 中的方法。
有谁能告诉我如何优雅地解决这个问题吗?
I do have three dlls.
- a.dll - released many years ago
- b.dll - released not so many years
- c.dll - released shortly
Each one contains the same function - unfortunatelly with different parameters.
so I do have the following Methods
aMethod(param1)
aMethod(param1, param2)
aMethod(param1, param2, param3)
My Task is to make a new dll (or new dlls) wich is backward compatible.
But as far as I've learned from Google there is no possibility to overload methods in a dll.
Does any one have a tip how I can solve this problem elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以重载 DLL 中的函数签名。但是,从 DLL 导出的函数名称必须是唯一的 - 这是 Windows 要求,而不是 Delphi 要求。因此,将 Delphi 中的函数声明为重载,但确保它们使用您定义的特定、唯一的名称导出。从新的一体化 DLL 导入的客户端将需要使用您定义的唯一名称进行导入。
Delphi 中的默认行为是导出函数通过函数名称导出,简单明了。如果您想做重载,您需要更多地参与并自己定义导出名称。
但请注意,这不会生成可放入需要 a.dll 的旧应用程序中的 DLL。此解决方案向后兼容源代码,但不向后兼容二进制文件。
您很可能无法创建与您过去的所有三个 DLL 版本二进制兼容的新 DLL,因为旧的 exe 二进制文件引用相同的函数名称,但期望不同的行为(不同的参数列表)。
另请注意,如果您的三个 dll 版本实际上具有不同的文件名(a、b、c),则这一点有点没有实际意义 - 静态函数绑定绑定到 dll 名称 + 函数名称。如果您希望新的 DLL 能够与旧的 exe 一起使用,您是否打算将新的 DLL 复制三次,分别命名为 a、b 和 c?这看起来很奇怪而且适得其反。就像睡狗一样,让旧的 DLL 躺着吧。除非你绝对需要修复一些严重的错误,否则不要管它们。
You can overload function signatures in a DLL. However, the function names exported from the DLL must be unique - this is a Windows requirement, not a Delphi requirement. So, declare your functions in Delphi as overloads, but make sure they are exported with specific, unique names that you define. Clients that import from your new all-in-one DLL will need to import using those unique names you defined.
The default behavior in Delphi is that exported functions are exported by the name of the function, plain and simple. If you want to do overloads, you need to get more involved and define the export names yourself.
Note however that this will not produce a DLL that can be dropped into an older application that is expecting your a.dll. This solution is backward compatible for source, but not backward compatible for binaries.
You will most likely not be able to create a new DLL that is binary compatible with all three of your past DLL versions, because the old exe binaries are referring to the same function name but expecting different behavior (different param lists).
Note also that if your three dll versions actually have different file names (a,b,c) then the point is somewhat moot - static function binding binds to dll name + function name. If you want your new DLL to work with the old exes, are you planning to copy the new dll three times to filenames a, b, and c? That seems odd and counterproductive. As with sleeping dogs, let the old DLLs lie. Leave them alone unless you absolutely have to fix some critical bug.
直到我知道你可以使用方法重载,你只需要在同一个dll中实现以前的和新的版本。在 Delphi 中,您需要使用重载指令。请参阅此链接:
http://delphi.about.com/od/objectpascalide/ a/重载.htm
Until I know you can use method overloading, you only need to implement the previous and new versions in the same dll. In Delphi you need to use the overload directive. See this link:
http://delphi.about.com/od/objectpascalide/a/overloading.htm
使这个方法更通用-将其更改为类似的东西-我不熟悉Delphi,所以这个示例是在C#中:
或在C中:
然后根据版本你可以使用强制转换。请注意,args 也可以是集合对象。
HTH。
Make this method more generic - change it to something like that - I'm not familiar with Delphi, so this example is in C#:
or in C:
Then according to the version you can use casting. Note that args can be also a collection object.
HTH.