Warning when using embedded Interop-Types
I have recently updated one of my C# solutions from VS2008 to vs 2010 to utilise the Embedded interop types feature so I can stop shipping the library of interops we currently have to due to interaction with our legacy VB6 codebase.
For some reason I am getting the following message when I compile my solution:
Type library importer has encountered an interface not derived from IUnknown: '_HiddenInterface'
I have searched around and so far the only two bits of information I could find were 'just ignore it it doesn't do any harm' and 'it means the VB6 code is breaking some rules'. Since the VB codebase is开发者_运维技巧 ours and it's preferable to have 0 warnings on compile I would like to correct whatever is causing these warnings.
I'm at a loss so any advice would be great.
You will get the compiler warning if you have a reference to a COM library that contains a class with a public method that returns a VB Collection object.
The _HiddenInterface interface is contained in MSVBVM60.DLL. You can see this by opening OLE View and navigating to Type Libraries > Visual Basic for Applications (Ver 6.0). Double-click the typelib to bring up the ITypeLib Viewer window. You can see interface _HiddenInterface
in the list.
You should be able to consume the collections returned by these functions, although you cannot create an instance of one in .NET as documented in the Microsoft KB article BUG: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: "System.InvalidCastException".
This is what I found works to suppress this warning, based mostly on comments here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/7a7c352b-20cb-4931-b3b5-27e899016f75/turning-off-msbuild-warnings-msb3305?forum=msbuild
Relevant details posted by IanG:
Although in general, fixing the underlying problem is the best way to fix warnings, that's not possible when the underlying 'problem' is that the Microsoft-supplied build tooling dislikes something about a Microsoft-supplied library.
...
The difficulty here is that the 'problem' that MSBuild is identifying here isn't truly a problem at all. It's just telling us that the imported library contains some features which, if you chose to use them, would be problematic in a .NET world. Unfortunately, it generates those warnings even if your code doesn't use any of those problematic bits. This is perfectly normal when importing type libraries that weren't designed with .NET in mind. And as long as your C# code never attempts to use those features you'll be fine.
...
I've found a way to stop the warnings from appearing, but I'm not sure how reliable it is, because it uses a feature for which the I've not found any direct documentation, and the only related thing I've found carries this warning: "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code."
I've added this in the PropertyGroup at the top of my .csproj file:
<ResolveComReferenceSilent>True</ResolveComReferenceSilent>
I've not been able to find this documented directly, but looking at the targets files in the SDK, you can see that this affects the following property: http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.resolvecomreference.silent(v=vs.110).aspx but that's the one with the warning that this is for internal use.
That said, I'm not using the unsupported setting directly: I'm using it via this property. And although I've not found any docs for the property itself, there is at least one page that tells you about the property. Although it's for a quite different kind of project, http://msdn.microsoft.com/en-us/library/windows/hardware/jj659905(v=vs.85).aspx does recommend using this to remove spurious warnings from a COM reference.
One of those links takes you to Sharing compiled binaries between UWP apps and Desktop apps which tersely just says:
If you see warnings as a result of COM references, add the following to the
<PropertyGroup>
tag:<ResolveComReferenceSilent>true</ResolveComReferenceSilent>
Even though this is UWP-specific information, it seems to work fine in non-UWP projects.
To implement this in my own project, I added a new PropertyGroup
below the existing COMReference
section of the CSPROJ
file:
<ItemGroup>
<COMReference Include="SomeVB6Library">
<Guid>{.......}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<PropertyGroup>
<!-- ResolveComReferenceSilent is to remove warning MSB3305 -->
<ResolveComReferenceSilent>true</ResolveComReferenceSilent>
</PropertyGroup>
Note that for whatever reason I was not seeing the warning generated every time I built the library, only sometimes. But it did seem to consistently appear whenever I cleaned & then rebuilt the entire solution. Until I realized that I wasn't convinced whether this had worked as intended or not. But it appears that it did.
I am not sure if this could have any other undesired effects - like suppressing other warnings that you might actually need to see. I would test carefully.
精彩评论