代码之家  ›  专栏  ›  技术社区  ›  HS.

异步可插拔协议

  •  3
  • HS.  · 技术社区  · 16 年前

    this 作为参考,我正在尝试创建一个异步可插拔协议,它只对我的应用程序暂时可用(而不是在系统范围内注册)。我正在使用 CoInternetGetSession 然后打电话 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);
    
        }
    

    这个 CreateInstance PluggableProtocolFactory中的方法从未被调用(一个断点永远不会被击中)因此在这个断点内发生了其他事情 注册中间空间

    我试着以管理员和普通用户的身份运行。两种情况下都有相同的错误。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Tony The Lion    16 年前

        2
  •  0
  •   HS.    16 年前

    好的,我发现了:IInternetSession接口的声明是错误的:

    这是一个更好的,我从 monoblog :

    [ComVisible(true), Guid("79eac9e7-baf9-11ce-8c82-00aa004ba90b"),InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInternetSession
    {
        [PreserveSig]
        int RegisterNameSpace(
            [In] IClassFactory classFactory,
            [In] ref Guid rclsid,
            [In, MarshalAs(UnmanagedType.LPWStr)] string pwzProtocol,
            [In]
                int cPatterns,
            [In, MarshalAs(UnmanagedType.LPWStr)]
                string ppwzPatterns,
            [In] int dwReserved);
    
        [PreserveSig]
        int UnregisterNameSpace(
            [In] IClassFactory classFactory,
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszProtocol);
    
        int Bogus1();
    
        int Bogus2();
    
        int Bogus3();
    
        int Bogus4();
    
        int Bogus5();
    }