供应用程序使用的特殊DLL [英] Special DLL for applications to use

查看:73
本文介绍了供应用程序使用的特殊DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Dll有疑问.我正在从事C编程和使用Visual Studio.所有代码(Dll和应用程序)都在C中.

假设我有一个DLL版本1.0,并且我想指定我的应用程序A使用的版本,DLL版本2.0也将用于应用程序B.并且可能DLL版本3.0可以链接到应用程序A和B.防止错误链接的方法?

原因是我有不同版本的dll或仅出于测试目的而构建的某些dll.我喜欢在配置文件或dll(可能是一些资源文件)中放入一些东西,以防止意外链接错误的dll.

任何想法将不胜感激!

在以下OP中添加了后续说明:

首先,非常感谢这两个答案!
我想可能需要提出一个特定的问题:如果使用了错误版本的DLL,我实际上想停止编译/链接过程.
这是我通常会遇到的过程:
一个或几个人将构建一个DLL(或几个).这些DLL将传递给另一个小组(应用程序工程师),以构建最终的应用程序(例如,更改GUI).有时,人们会忘记用于特定应用程序的DLL版本.当添加/增强了多个相似的功能时,这是正确的.
由于DLL和应用程序中的功能是在头文件中定义的,并且几乎总是使用相同的原型,因此将它们混合在一起非常容易.我最初想到的是,可能有一些方法可以在给定的DLL中以及一些配置文件(甚至头文件)中放入允许的"应用程序版本列表.如果他知道自己在做什么,我不认为我想完全阻止某人构建或使用DLL.但是在编译/链接期间发出警告消息可能很好.
所有这些都是为了改善内部开发过程.我对最终用户而言并不是这个意思,因为我们应该为最终产品安装正确的代码.
有什么建议吗?

解决方案

我唯一能想到的就是在DLL中有某种区别(我假设是动态链接):
1)DLL名称中调用了不同的版本(可以在LoadLibrary上进行唯一寻址).
2)至少一个(或多个)唯一函数调用,以通过在LoadLibrary之后搜索导出函数来区分.
3)调用您导出的功能之一(或视情况导入)以请求版本号,您应该能够在应用中尽早调用它,然后再尝试以任何方式使用它.


查看Microsoft并行技术
http://msdn.microsoft.com/en-us/library/aa376307(VS .85).aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/aa375155(VS .85).aspx [ ^ ]

来自MSDN:
出于以下原因,鼓励开发人员使用并行程序集创建隔离的应用程序,并将现有应用程序更新为隔离的应用程序:
*并行程序集减少了DLL版本冲突的可能性.
*并行程序集共享使多个版本的COM或Windows程序集可以同时运行.
*部署后,应用程序和管理员可以在全局配置或每个应用程序配置的基础上更新程序集配置.例如,可以将应用程序更新为使用包含更新的并排程序集,而不必重新安装该应用程序."

可能正是您要寻找的.

对于您的更新问题,请使用预处理程序指令的组合来实现:

在您的DLL中:

#define DLL_VER2



在您的应用程序中(此时必须将您的DLL设置为依赖项,这将使其首先编译):

#ifndef DLL_VER2
#error The current application requires DLL_VER2 for compatibility.
#endif



当然,请确保您的错误声明对其他开发人员有意义.


I have a question regarding Dlls. I am working on C programming and using visual studio. All codes (both Dlls and applications) are in C.

Assume I have a DLL version 1.0, and I like to specify this version being used by my application A, also DLL Version 2.0 will be for Application B. And probably DLL Version 3.0 can be linked to both applications A and B. Is there a way to prevent incorrectly link?

The reason is that I have different version of dlls or some dlls built for testing purpose only. I like to put something either in a configuration file or inside dll (may be some resource file) to stop accidentally linking of wrong dlls.

Any idea will be appreciated!

Added follow up clarification from OP below:

First of all, many thanks to both answers!
I think I may need to make the question a bit specific: I actually want to stop the compiling/linking process if the incorrect version of DLL is used.
Here is process I normally face:
One or a few people will build a DLL (or a few). These DLLs will be passed to another group (application engineers) for building the final application (GUI changes for example). Sometimes, the people will forget which version of DLL is for a particular application. This is true when multiple similar features are added/enhanced.
Since functions in DLL and in application are defined in header file and almost always with same prototype, it is very easy to mix the things together. What I initially thought was there might be some ways to put a list of "allowed" application versions in a given DLLs as well as some configuration file (or even header file). I don''t think I want to completely stop someone to build or use DLL if he knows what he is doing. But a warning message during compiling/linking is probably good.
All these things are just to improve the internal development process. I don''t mean this idea for end-user as we should have installed correct code for our final product.
Any suggestion?

解决方案

Only way I can think of is to have some sort of difference in the DLLs (I''m assuming dynamic linking):
1) Different version called out in the DLL name (that can be addressed uniquely on LoadLibrary).
2) At least one (or more) unique function calls to differentiate by searching export functions after LoadLibrary.
3) Have one of your exported functions (or imported depending on point of view) be a call to request the version number, you should be able to call it early in app before trying to use it in any way).


Check out the Microsoft Side-by-side technology
http://msdn.microsoft.com/en-us/library/aa376307(VS.85).aspx[^]
http://msdn.microsoft.com/en-us/library/aa375155(VS.85).aspx[^]

From the MSDN:
"Developers are encouraged to use side-by-side assemblies to create isolated applications, and to update existing applications into isolated applications for the following reasons:
* Side-by-side assemblies reduce the possibility of DLL version conflicts.
* Side-by-side assembly sharing enables multiple versions of COM or Windows assemblies to run at the same time.
* Applications and administrators can update assembly configuration on either a global or per-application configuration basis after deployment. For example, an application can be updated to use a side-by-side assembly that includes an update without having to reinstall the application."

Might be what you''re looking for.


With your updated question, use combination of pre-processor directives to achieve this:

within your DLL:

#define DLL_VER2



within your application (your DLL must be set as a dependency at this point, that''ll make it compile first):

#ifndef DLL_VER2
#error The current application requires DLL_VER2 for compatibility.
#endif



Of course, make sure that your error statements will make sense to the other developers.


这篇关于供应用程序使用的特殊DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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