简体   繁体   English

如何确定在Delphi XE2中注册的OCX的版本号

[英]How to determine the version number of a registered OCX in Delphi XE2

I have an ActiveX component in XE2 that connects to a database to handle transaction processing, and it includes a check of its version against that of the database to determine if they are compatible. 我在XE2中有一个ActiveX组件,该组件连接到数据库以处理事务处理,并且它包括对照数据库版本检查其版本,以确定它们是否兼容。

Unlike C#, where the AssemblyVersion attribute can be obtained at runtime using the Application.ProductVersion property, Delphi XE2 has no such built-in property for getting the version information included in the project options, with the preferred method being to use GetFileVersionInfo, passing in the full path to the program, or in this case the OCX. 与C#不同,在C#中,可以在运行时使用Application.ProductVersion属性获取AssemblyVersion属性,而Delphi XE2没有此类内置属性来获取项目选项中包含的版本信息,首选方法是使用GetFileVersionInfo,传入程序或OCX的完整路径。

Originally the OCX was always installed to the Windows System path but we have changed our installation process so that the OCX is installed to the same folder as the executable that uses it, which is a location determined by the user. 最初,OCX始终安装在Windows系统路径中,但我们已更改了安装过程,以便将OCX安装到与使用它的可执行文件相同的文件夹中,该文件夹由用户确定。

What I need is a consistent method that I can use from within the OCX code to obtain the installation folder from the registry across the multitude of Windows environments. 我需要的是一种一致的方法,我可以从OCX代码中使用该方法从多个Windows环境中的注册表中获取安装文件夹。 I assume it would have something to do with the GUID's defined in the _TLB.pas file. 我认为这与_TLB.pas文件中定义的GUID有关。

You don't need to query the Registry at all. 您根本不需要查询注册表。 The OCX can retreive its own filename by passing Delphi's global HInstance variable as the module handle to the Win32 API GetModuleFileName() function, then it can pass that filename to GetFileVersionInfo() . OCX可以通过将Delphi的全局HInstance变量作为模块句柄传递给Win32 API GetModuleFileName()函数来获取自己的文件名,然后可以将该文件名传递给GetFileVersionInfo()

Although, a better way for the OCX to access its own version, without resorting to GetFileVersionInfo() , is to use Find/Load/LockResource() to access its own version resource directly, then no filename is needed at all. 虽然,对于OCX访问自己的版本,无需采取更好的方式GetFileVersionInfo()是用Find/Load/LockResource()来访问自己的版本资源直接,则不需要的文件名都没有。 You can copy the version resource data into a temp buffer and pass that to VerQueryValue() to retrieve the resource's VS_FIXEDFILEINFO structure (retrieving anything else from the resource gets a bit trickier because GetFileVersionInfo() prepares certain lookup data that VerQueryValue() then uses). 您可以将版本资源数据复制到临时缓冲区中,并将其传递给VerQueryValue()以检索资源的VS_FIXEDFILEINFO结构(从资源中检索其他任何内容都比较棘手,因为GetFileVersionInfo()准备了VerQueryValue()然后使用的某些查找数据) 。

You certainly don't need to use the registry here. 您当然不需要在这里使用注册表。 Not least because there could potentially be multiple versions of the DLL on the machine, because you are installing to the executable directory of any program that uses the DLL. 尤其重要的是,由于要安装到使用DLL的任何程序的可执行目录中,因此计算机上可能存在DLL的多个版本。

You could certainly read the version resource as Remy describes. 您当然可以阅读Remy描述的版本资源。 But another alternative would be to include a constant in your program that encodes the database version compatibility. 但是另一种选择是在程序中包含一个编码数据库版本兼容性的常量。

const
  DatabaseVersion = 1;

Check this constant against the value read from the database and fail if not compatible. 根据从数据库读取的值检查此常量,如果不兼容则失败。

To me this makes a little more sense as it separates the version of the DLL from the version of the database. 对我来说,这更有意义,因为它将DLL的版本与数据库的版本分开了。 The two are not necessarily linked. 两者不一定链接。 You could, and probably do, update the DLL without changing the database structure. 您可以并且可能确实在不更改数据库结构的情况下更新了DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM