How do I declare an IStream in idl so visual studio maps it to s.w.interop.comtypes?

此生再无相见时 提交于 2019-12-01 12:25:14

This doesn't need fixing, the interop wrapper created from the type library will be fine. The ComTypes.IStream declaration is there to allow managed code to implement a COM server that implements an IStream or takes one as an argument. Lots of .NET framework classes do.

What you are seeing and experiencing is a feature (often annoying one) of the MIDL compiler. Any type that is referenced inside the "library" section will have its definition injected into the tlb (type library); with the exception of the IUnknown interface and the MIDL base types, ( and maybe a couple more primitive types). Those weird methods you are seeing on "your" IStream are coming from the base type, ISequentialStream. You have a few options:

  1. Wrestle with the MIDL compiler for days to try to get it from injecting IStream (and all its other supporting types). I've done this. It was also with Mscoree. The problem is that the MIDL compiler automatically imports "oaidl.idl" when it encounters a library statement. So those types (Stream and such) are already injected into the current IDL context before you have a chance to do anything about it. This is a poorly documented feature. Bottom line is its going to do it no matter what you do, unless you remove the hard references to IStream (use PVOID [*] stream).

  2. Ignore it. Don't even use it. Use System.Runtime.InteropServices.ComTypes.IStream instead. As long as the interface you are using (in .Net) is marked with the correct attributes (Guid, InterfaceType, etc), they are interchangeable. You'll have to either edit the type library or interop code so it will accept what type you wish to pass it. I'd go with object so you can use any IStream interface (with a valid definition). And also don't use the old TlbImp tool. Use the new TlbImp2 (written in C# and open source) https://clrinterop.codeplex.com/releases/view/17579. It allows you to really customize the conversion from TLB to Managed and you can have it produce source files instead of/in addition to a compiled interop dll.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!