为什么不创建或调用我的IE MIME过滤器? [英] Why isn't my IE MIME filter being created or called?

查看:279
本文介绍了为什么不创建或调用我的IE MIME过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个MIME过滤器,对网页接收的资源进行一些自定义处理,然后将它们传递到我们的Windows应用程序中的Web浏览器控件。应用程序是用C#编写的,如果可能,我想在托管代码中编写MIME过滤器。我遇到麻烦了:我的过滤器对象似乎没有得到调用。



这是我的代码。对不起,这么长,但我想我可能在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读取(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(); //未实现
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)
{
... //这从不叫
}

...
}

内部类FilterUtils
{
///< summary>
///为当前进程注册MIME过滤器
///< / 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 * /);
}
}

据我所知,应该发生的是应该调用MimeFilter.CreateInstance()方法。它不会被调用,也没有任何其他方法在MimeFilter。



我会感谢任何人可以提供任何帮助。感谢。

解决方案

结束MSDN文档和Visual Studio 2008不同意默认COM可见性。 MSDN文档说,默认情况下COM对所有.NET类型都可见,但Visual Studio 2008类库项目模板禁用AssemblyInfo.cs文件中的COM可见性。



解决方案:删除该行

  [assembly:ComVisible(false)] 
pre>

从AssemblyInfo.cs文件。



我也可能有错误的COM接口声明。我已用此CodeProject文档替换它们。



我发现的唯一可读的MIME过滤器示例代码是此处和(简要)此处记录< a>。


I'm trying to create a MIME filter to do some custom processing of resources received with web pages before passing them to the web browser control in our Windows application. The application is written in C#, and I'd like to write the MIME filter in managed code as well, if possible. I'm having trouble with it though: my filter object doesn't seem to be getting called at all.

Here's my code. Sorry it's so long, but I think I might be defining something incorrectly in the COM interfaces, so I'm including those also. Any ideas what I'm doing wrong?

[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 */);
    }
}

As far as I can tell, the next thing that should happen is that the MimeFilter.CreateInstance() method should be called. It's not getting called, nor are any of the other methods in MimeFilter.

I'd appreciate any help anyone can offer. Thanks.

解决方案

Turns out the MSDN documentation and Visual Studio 2008 disagree about default COM visibility. The MSDN documentation says that all .NET types are visible to COM by default, but the Visual Studio 2008 class library project template disables COM visibility in the AssemblyInfo.cs file.

Solution: remove the line

[assembly: ComVisible(false)]

from the AssemblyInfo.cs file.

I may have also had bad COM interface declarations. I've replaced them with the ones from this CodeProject document.

The only readable MIME filter sample code I've found is available here and (briefly) documented here.

这篇关于为什么不创建或调用我的IE MIME过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆