c# - 异步可插入协议(protocol)

标签 c# com interop

使用 this作为引用,我正在尝试创建一个异步可插入协议(protocol),该协议(protocol)仅对我的应用程序暂时可用(并且未在系统范围内注册)。我正在使用 CoInternetGetSession 然后调用 RegisterNameSpace 来完成它。但是,当我调用 RegisterNameSpace 时,我收到 AccessViolation 异常:Attempting to read or write protected memory

知道发生了什么吗?

我的代码是这样的:

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

/* Custom class to act as a class factory that create's an instance of the protocol */
[Guid("0b9c4422-2b6e-4c2d-91b0-9016053ab1b1")]
[ComVisible(true),ClassInterface(ClassInterfaceType.AutoDispatch)]
public class PluggableProtocolFactory : IClassFactory
{
    public Type AppType;
    public PluggableProtocolFactory(Type t)
    {
        this.AppType = t;
    }
    public void CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
    {
        riid = ProtocolSupport.GetGuid(this.AppType);
        IInternetProtocol p = Activator.CreateInstance(this.AppType) as IInternetProtocol;
        ppvObject = Marshal.GetComInterfaceForObject(p, typeof(IInternetProtocol));
    }

    public void LockServer(bool fLock)
    {
        var b = fLock;
    }

}

[ComVisible(true)]
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[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.LPArray,ArraySubType=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(false)] public interface IComRegister
{
    void Register(Type t);
    void Unregister(Type t);
}

[ComVisible(false), AttributeUsage(AttributeTargets.Class, AllowMultiple=true) ] 
public class AsyncProtocolAttribute : Attribute, IComRegister
{
    public string Name;
    public string Description;

    [DllImport("urlmon.dll",PreserveSig=false)]
    public static extern int CoInternetGetSession(UInt32 dwSessionMode /* = 0 */, ref IInternetSession ppIInternetSession, UInt32 dwReserved /* = 0 */);

    public void Register(Type t)
    {
        IInternetSession session = null;
        CoInternetGetSession(0, ref session, 0);
        Guid g = new Guid("79EAC9E4-BAF9-11CE-8C82-00AA004BA90B");
        session.RegisterNameSpace(new PluggableProtocolFactory(t), ref g, this.Name, 0, null, 0);

    }

永远不会调用 PluggableProtocolFactory 中的 CreateInstance 方法。 (那里的断点永远不会被击中)所以 RegisterNameSpace 方法内部发生了其他事情。

我尝试以管理员和普通用户身份运行。两次都出现同样的错误。

最佳答案

您在 RegisterNameSpace 方法中调用 PluggableProtocolFactory 的构造函数,而不是 CreateInstance 方法。我认为这就是您的问题所在,从来没有创建要传递给您的方法的 IInternetProtocol 实例。

关于c# - 异步可插入协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2013050/

相关文章:

c# - 为使用自定义结转准确划分两个 double 列表创建数学问题

c# - 从派生类获取特定类型

c++ - 比较 pimpl idiom 与 Microsoft COM

c++ - 未能释放Direct3D Device "or"对应上下文

C# COM 互操作库

c# - 在 c# 中使用 c 套接字时出现的问题

c# - CollectionViewSource排序算法

c# - 如何在richTextBox的索引0中的每一行附近添加数字?

events - IMFMediaSource::GetEvent 永远不会收到事件

.net - 如何以编程方式判断word文档是否已损坏?