C# 为什么不是';是否正在创建或调用我的IE MIME筛选器?

C# 为什么不是';是否正在创建或调用我的IE MIME筛选器?,c#,.net,com,interop,C#,.net,Com,Interop,我正在尝试创建一个MIME过滤器,以便在将网页中接收到的资源传递给Windows应用程序中的web浏览器控件之前,对这些资源进行一些自定义处理。应用程序是用C#编写的,如果可能的话,我还想用托管代码编写MIME过滤器。但是我遇到了麻烦:我的filter对象似乎根本没有被调用 这是我的密码。抱歉,太长了,但是我想我可能在COM接口中定义了一些错误的东西,所以我也包括了这些。知道我做错了什么吗 [ComImport] [InterfaceType(ComInterfaceType.Interface

我正在尝试创建一个MIME过滤器,以便在将网页中接收到的资源传递给Windows应用程序中的web浏览器控件之前,对这些资源进行一些自定义处理。应用程序是用C#编写的,如果可能的话,我还想用托管代码编写MIME过滤器。但是我遇到了麻烦:我的filter对象似乎根本没有被调用

这是我的密码。抱歉,太长了,但是我想我可能在COM接口中定义了一些错误的东西,所以我也包括了这些。知道我做错了什么吗

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("00000001-0000-0000-C000-000000000046")]
public interface IClassFactory
{
    void CreateInstance([MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, ref Guid riid, out IntPtr ppvObject);
    void LockServer(bool fLock);
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("79EAC9E4-BAF9-11CE-8C82-00AA004BA90B")]
public interface IInternetProtocol : IInternetProtocolRoot
{
    void LockRequest(Int32 dwOptions);
    [PreserveSig]
    Int32 Read(IntPtr pv, UInt32 cb, out UInt32 pcbRead);
    void Seek(Int64 dlibMove, UInt32 dwOrigin, out UInt64 plibNewPosition);
    void UnlockRequest();
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("79EAC9E3-BAF9-11CE-8C82-00AA004BA90B")]
public interface IInternetProtocolRoot
{
    void Abort(Int32 hrReason, Int32 dwOptions);
    void Continue(IntPtr pProtocolData);
    void Resume();
    void Start([MarshalAs(UnmanagedType.LPWStr)] string szUrl, IInternetProtocolSink pOIProtSink,
               IntPtr pOIBindInfo, UInt32 grfPI, IntPtr dwReserved);
    void Suspend();
    void Terminate(Int32 dwOptions);
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("79EAC9E5-BAF9-11CE-8C82-00AA004BA90B")]
public interface IInternetProtocolSink
{
    void Switch(IntPtr pProtocolData);
    void ReportProgress(UInt32 ulStatusCode, [MarshalAs(UnmanagedType.LPWStr)] string szStatusText);
    void ReportData(UInt32 grfBSCF, UInt32 ulProgress, UInt32 ulProgressMax);
    void ReportResult(Int32 hrResult, UInt32 dwError, [MarshalAs(UnmanagedType.LPWStr)] string szResult);
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("79eac9e7-baf9-11ce-8c82-00aa004ba90b")]
public interface IInternetSession
{
    void CreateBinding(); // Not Implemented
    void GetCache(); // Not Implemented
    void GetSessionOption(); // Not Implemented
    void RegisterMimeFilter([MarshalAs(UnmanagedType.Interface)] IClassFactory pCF, ref Guid rclsid, [MarshalAs(UnmanagedType.LPWStr)] string pwzType);
    void RegisterNameSpace([MarshalAs(UnmanagedType.Interface)] IClassFactory pCF, ref Guid rclsid, [MarshalAs(UnmanagedType.LPWStr)] string pwzProtocol,
                           UInt32 cPatterns, [MarshalAs(UnmanagedType.LPWStr)] string ppwzPatterns, UInt32 dwReserved);
    void SetCache(); // Not Implemented
    void SetSessionOption(); // Not Implemented
    void UnregisterMimeFilter(IClassFactory pCF, [MarshalAs(UnmanagedType.LPWStr)] string pwzType);
    void UnregisterNameSpace(IClassFactory pCF, [MarshalAs(UnmanagedType.LPWStr)] string pwzProtocol);
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("C3ED53DA-EC0E-4625-AB0C-9837D0D0D59D")]
public interface _MimeFilter : IClassFactory, IInternetProtocol, IInternetProtocolRoot, IInternetProtocolSink
{
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("C3ED53DA-EC0E-4625-AB0C-9837D0D0D59D")]
public class MimeFilter : _MimeFilter
{
    #region [ IClassFactory ]
    public void CreateInstance([MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, ref Guid riid, out IntPtr ppvObject)
    {
        ... // This is never called
    }

    ...
}

internal class FilterUtils
{
    /// <summary>
    /// Registers the MIME filter for the current process
    /// </summary>
    public static void RegisterFilter()
    {
        IInternetSession session = null;
        int status = NativeMethods.CoInternetGetSession(0, ref session, 0);
        MimeFilter mf = new MimeFilter();
        Guid id = new Guid("C3ED53DA-EC0E-4625-AB0C-9837D0D0D59D");
        session.RegisterMimeFilter(mf, ref id, "text/html; charset=UTF-8");
    }

    private static class NativeMethods
    {
        [DllImport("urlmon.dll")]
        public static extern int CoInternetGetSession(UInt32 dwSessionMode /* = 0 */, ref IInternetSession ppIInternetSession, UInt32 dwReserved /* = 0 */);
    }
}
[ComImport]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
[Guid(“00000001-0000-0000-C000-0000000000 46”)]
公共接口IClassFactory
{
void CreateInstance([marshallas(UnmanagedType.IUnknown)]对象pUnkOuter,ref Guid riid,out IntPtr ppvObject);
void LockServer(bool fLock);
}
[ComImport]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
[Guid(“79EAC9E4-BAF9-11CE-8C82-00AA004BA90B”)]
公共接口IInternetProtocolRoot:IInternetProtocolRoot
{
无效锁定请求(Int32 dwOptions);
[信号]
Int32读取(IntPtr pv、UInt32 cb、out UInt32 pcbRead);
无效寻道(Int64 dlibMove、UInt32 dwOrigin、out UInt64 PLIBNEW位置);
void UnlockRequest();
}
[ComImport]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
[Guid(“79EAC9E3-BAF9-11CE-8C82-00AA004BA90B”)]
公共接口IInternetProtocolRoot
{
无效中止(Int32 hr原因,Int32 dwOptions);
继续作废(IntPtr pProtocolData);
作废简历();
void Start([Marshallas(UnmanagedType.LPWStr)]字符串szUrl,IInternetProtocolSink pOIProtSink,
IntPtr pOIBindInfo、UInt32 grfPI、IntPtr DWI(保留);
无效暂停();
无效终止(Int32 dwOptions);
}
[ComImport]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
[Guid(“79EAC9E5-BAF9-11CE-8C82-00AA004BA90B”)]
公共接口IInternetProtocolSink
{
无效开关(IntPtr pProtocolData);
void ReportProgress(UInt32 ulStatusCode[marshallas(UnmanagedType.LPWStr)]字符串szStatusText);
无效报告数据(UInt32 grfBSCF、UInt32 ulProgress、UInt32 ulProgress MAX);
void ReportResult(Int32 hrResult,UInt32 dwError,[marshallas(UnmanagedType.LPWStr)]字符串szResult);
}
[ComImport]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
[Guid(“79eac9e7-baf9-11ce-8c82-00aa004ba90b”)]
公共接口IInternetSession
{
void CreateBinding();//未实现
void GetCache();//未实现
void GetSessionOption();//未实现
无效注册表项筛选器([Marshallas(UnmanagedType.Interface)]IClassFactory pCF,参考Guid rclsid,[Marshallas(UnmanagedType.LPWStr)]字符串pwzType;
无效RegisterNameSpace([Marshallas(UnmanagedType.Interface)]IClassFactory pCF,参考Guid rclsid,[Marshallas(UnmanagedType.LPWStr)]字符串pwzProtocol,
UInt32 cPatterns,[marshallas(UnmanagedType.LPWStr)]字符串ppwzPatterns,UInt32 dwReserved);
void SetCache();//未实现
void SetSessionOption();//未实现
void UnregisterMimeFilter(IClassFactory pCF,[Marshallas(UnmanagedType.LPWStr)]字符串pwzType;
void UnregisterNameSpace(IClassFactory pCF,[Marshallas(UnmanagedType.LPWStr)]字符串pwzProtocol);
}
[ComVisible(true)]
[接口类型(ComInterfaceType.InterfaceIsDual)]
[Guid(“C3ED53DA-EC0E-4625-AB0C-9837D0D0D59D”)]
公共接口\u MimeFilter:IClassFactory、IInternetProtocolRoot、IInternetProtocolSink
{
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(“C3ED53DA-EC0E-4625-AB0C-9837D0D0D59D”)]
公共类MimeFilter:\u MimeFilter
{
#地区[IClassFactory]
public void CreateInstance([marshallas(UnmanagedType.IUnknown)]对象pUnkOuter,ref Guid riid,out IntPtr ppvObject)
{
…//这从来没有被称为
}
...
}
内部类筛选器
{
/// 
///为当前进程注册MIME筛选器
/// 
公共静态无效注册表筛选器()
{
IInternetSession会话=空;
int status=NativeMethods.CoInternetGetSession(0,ref session,0);
MimeFilter mf=新的MimeFilter();
Guid id=新Guid(“C3ED53DA-EC0E-4625-AB0C-9837D0D59D”);
registerTimeFilter(mf,ref-id,“text/html;charset=UTF-8”);
}
私有静态类NativeMethods
{
[DllImport(“urlmon.dll”)]
公共静态外部int CointInternetGetSession(UInt32 dwSessionMode/*=0*/,ref IInternetSession ppIInternetSession,UInt32 dwReserved/*=0*/);
}
}
据我所知,接下来应该发生的事情是调用MimeFilter.CreateInstance()方法。它不会被调用,MimeFilter中的任何其他方法也不会被调用


我很感激任何人能提供的帮助。谢谢。

证明MSDN文档和Visual Studio 2008在默认COM可见性方面存在分歧。MSDN文档说明默认情况下所有.NET类型对COM都可见,但Visual Studio 2008类库项目模板在AssemblyInfo.cs文件中禁用COM可见性

解决方案:删除该行

[assembly: ComVisible(false)]
从AssemblyInfo.cs文件

我可能也有不好的COM接口声明。我已将它们替换为来自的


我找到的唯一可读的MIME过滤器示例代码是可用的,并且(简单地说)。

Greg,你能发布你更新的示例吗?正如你所说,MIME过滤器的示例很难获得。谢谢或添加到