1

为了能够过滤使用 C# Web 浏览器 (WinForms) 获取的 URL(包括 JS、图像等),唯一可包含的选项似乎是包装 HTTP 的异步可插入协议(以及后来的其他协议)。不幸的是,在几次调用后InvalidCastException,原始协议实现抛出了一个失败,这也是奇怪的部分,它似乎在失败之前成功了几次。

现在一些代码:

首先注册并附加协议的工厂:

  var ep = new FilteredHttpProtocolFactory();
  Guid id = Guid.Parse ("E00957BD-D0E1-4eb9-A025-7743FDC8B27B");
  session.RegisterNameSpace (ep, ref id, "http", 0, null, 0);

(工厂:)

[Guid ("EF474615-8079-4CFA-B114-6D1D28634DD8")]
[ComVisible (true)]
[ClassInterface (ClassInterfaceType.None)]
public class FilteredHttpProtocolFactory : IClassFactory
{
  public void CreateInstance (object pUnkOuter, Guid riid, out object ppvObject)
  {
    ppvObject = new FilteredHttpProtocol();
  }

  public void LockServer (bool fLock)
  {
  }
}

这是 IE 使用的原始 HTTP 协议,当使用它而不是包装器时,它工作得很好:

[ComImport]
[Guid ("79eac9e2-baf9-11ce-8c82-00aa004ba90b")]
public class OriginalHttpHandler
{
}

这是包装器本身:

[Guid ("E00957BD-D0E1-4eb9-A025-7743FDC8B27B")]
[ComVisible (true)]
[ClassInterface (ClassInterfaceType.None)]
[AsyncProtocol (Name = "http2", Description = "blah")]
public class FilteredHttpProtocol : IInternetProtocol, IInternetProtocolRoot
{ 
private readonly IInternetProtocol _wrapped;

public FilteredHttpProtocol ()
{
  var originalHttpHandler = new OriginalHttpHandler();
  _wrapped = (IInternetProtocol) originalHttpHandler;
}

public void Start (string szURL, IInternetProtocolSink Sink, IInternetBindInfo pOIBindInfo, uint grfPI, uint dwReserved)
{
  _wrapped.Start (szURL, Sink, pOIBindInfo, grfPI, dwReserved);
}

public void Continue (ref _tagPROTOCOLDATA pProtocolData)
{
  _wrapped.Continue (ref pProtocolData);      // <- FAILS HERE
}
   // .... other methods from IInternetProtocol
    public uint Read (IntPtr pv, uint cb, out uint pcbRead)
    {
      return _wrapped.Read (pv, cb, out pcbRead); // <- OR HERE
    }
}

所以,奇怪的部分是,构造函数被调用,Start()被调用,甚至被调用多次,直到页面的部分已经可见(!)时整个事情失败(使用Read()或),但似乎主要是一个缺少特定图像(主要是!):Continue()Read()Continue()

Unable to cast COM object of type 'Clients.Windows.Protocol.OriginalHttpHandler' 
to interface type 'Clients.Windows.Protocol.IInternetProtocol'. This operation 
failed because the QueryInterface call on the COM component for the interface 
with IID '{79EAC9E4-BAF9-11CE-8C82-00AA004BA90B}' failed due to the following 
error: No such interface supported (Exception from HRESULT: 0x80004002 E_NOINTERFACE)).

看到我已经多次将对象强制转换为所述接口(这应该导致QueryInterface()每次调用,并且在失败之前已经多次调用它(通过断点等进行验证),这个错误真的很令人费解。通过查看参考计数我已经排除了过早处置的对象(无论如何都没有意义)。

我尝试了几件事:

基本上,我想要实现的是包装 IE 的默认 http 协议实现以过滤掉 URL,包括从中检索资源的 URL。我也会对合适的替代方案感到满意,但它们必须符合 GPLv2,可与浏览器应用程序一起部署,并且不对系统的其余部分进行任何更改(即无代理)。

谢谢你的帮助 ;)

顺便说一句,这将成为我硕士论文的一部分:http: //desktopgap.codeplex.com

4

2 回答 2

0

目前我正在调查同样的行为。

我有两个想法。首先是从创建它的另一个线程调用该对象:

Method, Apartment, ThreadId, ObjId
--------------------
Constructor: , STA, 9, #1
Start: , STA, 9, #1
Continue: , STA, 9, #1
Read: , STA, 9, #1
Read: , STA, 9, #1
Lock: , STA, 9, #1
Read: , STA, 9, #1
Read: , STA, 9, #1
Constructor: , STA, 10, #2
Start: , STA, 10, #2
Constructor: , STA, 10, #3   <-- Notice ThreadId is 10
Terminate: , STA, 9, #1
Start: , STA, 10, #3         <-- Valid call
Constructor: , STA, 10, #4
Start: , STA, 10, #4
Read: , STA, 10, #4
Continue: , MTA, 11, #2
Terminate: , STA, 10, #4
Continue: , MTA, 12, #3      <-- EXCEPTION HERE!!! We have new thread with Id of 12 and MTA apartment
Constructor: , STA, 10, #5
Start: , STA, 10, #5
Constructor: , STA, 10, #6
Unlock: , STA, 9, #1
Start: , STA, 10, #6

第二个想法是 CLR 处理 COM 调用的方式与本机 C++ 不同。我在尝试使用 DirectShow 时遇到过这种情况。因此,在 c++ 的情况下,它只需要指向接口的指针并通过 v-table 调用函数。但在托管代码的情况下,它首先查询接口。如果没有正确实施,那么额外的查询接口调用可能会失败或返回错误的对象。解决方案将是创建本地 IUknown 包装器,该包装器将在 QueryInterface 上返回指向 IInternetProtocol 的指针。

于 2013-05-10T22:48:12.373 回答
0

经过一天的 COM 试验和互联网上的各种阅读后,我找到了解决方案。

正如Simon Mourier指出的那样,这是一个线程问题:FilteredHttpProtocol我创建的支持带有 COM 的 STA 和 MTA(= C# 默认行为),默认的 Http 协议对象不支持,并且在从不同的线程调用方法时(显然发生了) ,编组失败(也在这里指出)抛出 E_NOINTERFACE。

由于更改 COM 线程行为显然有点奇怪(它需要写入注册表)并且对我不起作用,因此解决方案是在类工厂中创建一个自定义 STA 线程(即,将相同的线程传递给每个 FilteredHttpProtocol 对象)(而不是 Protocol 类本身)使用Invoke().

一个快速而肮脏的解决方案是:

[ComVisible (true)]
public class FilteredHttpProtocolFactory : IClassFactory
{
    private readonly Control _ctrl;

    public FilteredHttpProtocolFactory ()
    {
      _ctrl = new Control();
    }

    public void CreateInstance (object pUnkOuter, Guid riid, out object ppvObject)
    {
      ppvObject = new FilteredHttpProtocol(_ctrl);
    }

    public void LockServer (bool fLock)
    {
    }
}

以及协议类本身:

[ComVisible (true)]
[AsyncProtocol (Name = "http2", Description = "blah")]
public class FilteredHttpProtocol :  IInternetProtocol, IInternetProtocolRoot
{
    private IInternetProtocol _wrapped;
    private static int s_id = 0;
    private int _id = -1;
    private int _creatingTID = -1;

    private Control _dispatcher;

    public FilteredHttpProtocol (Control ctrl)
    {
      _dispatcher = ctrl;
      _id = s_id;
      s_id++;
      _creatingTID = Thread.CurrentThread.ManagedThreadId;
      Debug.WriteLine ("#" + _id + " threadID: " + _creatingTID + " C'tor()");

      _dispatcher.Dispatcher.Invoke (
          () =>
          {
            var originalHttpHandler = new OriginalHttpHandler();
            _wrapped = (IInternetProtocol) originalHttpHandler;
          });
    }

    public void Start (string szURL, IInternetProtocolSink Sink, IInternetBindInfo pOIBindInfo, uint grfPI, uint dwReserved)
    {
      Debug.WriteLine ("#" + _id + " URL: " + "\t" + szURL);
      _dispatcher.Dispatcher.Invoke (
          () =>
          {
            Debug.WriteLine (
                "#" + _id + " original thread: " + _creatingTID + " calling thread " + Thread.CurrentThread.ManagedThreadId
                + " Start() "
                + Thread.CurrentThread.GetApartmentState());
            _wrapped.Start (szURL, Sink, pOIBindInfo, grfPI, dwReserved);
          });
    }

    public void Continue (ref _tagPROTOCOLDATA pProtocolData)
    {
      var _pProtocolData = pProtocolData;
      _dispatcher.Dispatcher.Invoke (
          () =>
          {
            Debug.WriteLine (
                "#" + _id + " original thread: " + _creatingTID + " calling thread " + Thread.CurrentThread.ManagedThreadId + " Continue() "
                + Thread.CurrentThread.GetApartmentState());
            _wrapped.Continue (ref _pProtocolData);
          });
      pProtocolData = _pProtocolData;
    }
    // ...
}

(请不要按原样使用此代码;))感谢您的帮助,我希望这也对其他人有所帮助。

干杯,

克劳斯

于 2013-05-13T16:18:16.913 回答