VC++: #import directive: how to specify a library version?
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.
精彩评论