开发者

VC++: #import directive: how to specify a library version?

开发者 https://www.devze.com 2023-03-25 02:45 出处:网络
According to MSDN, there is version attribute but if you specify a wrong version number VC still compiles the code. For example:

According to MSDN, there is version attribute but if you specify a wrong version number VC still compiles the code. For example:

开发者_开发知识库// MSO.DLL (Microsoft Office, Object Library)
//  Office 10.0 => version(2.2)
//  Office 11.0 => version(2.3)
//  Office 12.0 => version(2.4)
#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" version(123.456) //< wrong version.

How to force the compiler to fail on such code? I would like to use only specific version of type-library.


You can't. The rules are explained in LoadRegTypeLib:

LoadRegTypeLib compares the requested version numbers against those found in the system registry, and takes one of the following actions:

  • If one of the registered libraries exactly matches both the requested major and minor version numbers, then that type library is loaded.
  • If one or more registered type libraries exactly match the requested major version number, and has a greater minor version number than that requested, the one with the greatest minor version number is loaded.
  • If none of the registered type libraries exactly match the requested major version number (or if none of those that do exactly match the major version number also have a minor version number greater than or equal to the requested minor version number), then LoadRegTypeLib returns an error.

Your case matches the 2nd bullet, not the 3rd. Microsoft does spend a lot of effort on making these type libraries backward compatible. Not taking advantage of it is easy to do. Build your project on a machine with the right type library. Copy the generated .tlh and .tli files to your project directory and check them in. Replace the #import with #includes for those files.

0

精彩评论

暂无评论...
验证码 换一张
取 消