Beispiel #1
0
 public override DirectShowLib.IBaseFilter Create()
 {
     DirectShowLib.IBaseFilter result = null;
     try
     {
         IntPtr handle = FromFile.LoadLibrary(this.path);
         IntPtr dllGetClassObjectPointer = FromFile.GetProcAddress(handle, "DllGetClassObject");
         //Convert the function pointer to a .net delegate
         DllGetClassObject dllGetClassObject = (DllGetClassObject)System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPointer, typeof(DllGetClassObject));
         System.Guid       IClassFactoryGUID = new System.Guid("00000001-0000-0000-C000-000000000046");           //IClassFactory class id
         object            classFactory;
         System.Guid       identifier = this.Identifier;
         if (dllGetClassObject(ref identifier, ref IClassFactoryGUID, out classFactory) == 0)
         {
             System.Guid iBaseFilter = new System.Guid("56a86895-0ad4-11ce-b03a-0020af0ba770");
             object      filter;
             (classFactory as IClassFactory).CreateInstance(null, ref iBaseFilter, out filter);
             result = filter as DirectShowLib.IBaseFilter;
         }
     }
     catch (System.Exception e)
     {
         string message = "Filter \"" + this.Description + "\" could not find the file \"" + this.path.ToString() + "\".";
         new DirectShow.Binding.Exception.FilterNotFound(Error.Level.Debug, message, e.Message).Throw();
     }
     if (result.IsNull())
     {
         result = base.Create();
     }
     return(result);
 }
Beispiel #2
0
		private static IClassFactory GetClassFactoryFromDll(string dllName, string filterPersistClass)
		{
			//Load the dll
			IntPtr dllHandle=Win32NativeMethods.LoadLibrary(dllName);
			if (dllHandle==IntPtr.Zero)
				return null;

			//Keep a reference to the dll until the process\AppDomain dies
			_dllList.AddDllHandle(dllHandle);

			//Get a pointer to the DllGetClassObject function
			IntPtr dllGetClassObjectPtr=Win32NativeMethods.GetProcAddress(dllHandle, "DllGetClassObject");
			if (dllGetClassObjectPtr==IntPtr.Zero)
				return null;

			//Convert the function pointer to a .net delegate
			DllGetClassObject dllGetClassObject=(DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));

			//Call the DllGetClassObject to retreive a class factory for out Filter class
			Guid filterPersistGUID=new Guid(filterPersistClass);
			Guid IClassFactoryGUID=new Guid("00000001-0000-0000-C000-000000000046"); //IClassFactory class id
			Object unk;
			if (dllGetClassObject(ref filterPersistGUID, ref IClassFactoryGUID, out unk)!=0)
				return null;

			//Yippie! cast the returned object to IClassFactory
			return (unk as IClassFactory);
		}
        private static IntPtr FindProxyDllInfo(SafeLibraryHandle lib, Guid clsid)
        {
            try
            {
                GetProxyDllInfo get_proxy_dllinfo = lib.GetFunctionPointer <GetProxyDllInfo>();
                IntPtr          pInfo;
                IntPtr          pId;
                get_proxy_dllinfo(out pInfo, out pId);
                return(pInfo);
            }
            catch (Win32Exception)
            {
            }

            IntPtr psfactory = IntPtr.Zero;

            try
            {
                DllGetClassObject dll_get_class_object = lib.GetFunctionPointer <DllGetClassObject>();
                Guid IID_IPSFactoryBuffer = COMInterfaceEntry.IID_IPSFactoryBuffer;

                int hr = dll_get_class_object(ref clsid, ref IID_IPSFactoryBuffer, out psfactory);
                if (hr != 0)
                {
                    throw new Win32Exception(hr);
                }

                // The PSFactoryBuffer object seems to be structured like on Win10 at least.
                // VTABLE*
                // Reference Count
                // ProxyFileInfo*

                IntPtr pInfo = Marshal.ReadIntPtr(psfactory, 2 * IntPtr.Size);
                // TODO: Should add better checks here,
                // for example VTable should be in COMBASE and the pointer should be in the
                // server DLL's rdata section. But this is probably good enough for now.
                using (SafeLibraryHandle module = COMUtilities.SafeGetModuleHandle(pInfo))
                {
                    if (module == null || lib.DangerousGetHandle() != module.DangerousGetHandle())
                    {
                        return(IntPtr.Zero);
                    }
                }

                return(pInfo);
            }
            catch (Win32Exception)
            {
                return(IntPtr.Zero);
            }
            finally
            {
                if (psfactory != IntPtr.Zero)
                {
                    Marshal.Release(psfactory);
                }
            }
        }
Beispiel #4
0
        private static IUnknown NewRedemptionObject(Guid guid)
        {
            //try to set the thread COM apartment
            //Thread.CurrentThread.ApartmentState = ApartmentState.STA;

            object res = null;

            lock (_criticalSection)
            {
                IClassFactory ClassFactory;
                if (_redemptionDllHandle.Equals(IntPtr.Zero))
                {
                    string dllPath;
                    if (IntPtr.Size == 8)
                    {
                        dllPath = DllLocation64Bit;
                    }
                    else
                    {
                        dllPath = DllLocation32Bit;
                    }
                    _redemptionDllHandle = Win32NativeMethods.LoadLibraryW(dllPath);
                    if (_redemptionDllHandle.Equals(IntPtr.Zero))
                    {
                        //throw new Exception(string.Format("Could not load '{0}'\nMake sure the dll exists.", dllPath));
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    _dllGetClassObjectPtr = Win32NativeMethods.GetProcAddress(_redemptionDllHandle, "DllGetClassObject");
                    if (_dllGetClassObjectPtr.Equals(IntPtr.Zero))
                    {
                        //throw new Exception("Could not retrieve a pointer to the 'DllGetClassObject' function exported by the dll");
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    _dllGetClassObject =
                        (DllGetClassObject)
                        Marshal.GetDelegateForFunctionPointer(_dllGetClassObjectPtr, typeof(DllGetClassObject));
                }

                Object unk;
                int    hr = _dllGetClassObject(ref guid, ref IID_IClassFactory, out unk);
                if (hr != 0)
                {
                    throw new Exception("DllGetClassObject failed with error code 0x" + hr.ToString("x8"));
                }
                ClassFactory = unk as IClassFactory;
                ClassFactory.CreateInstance(null, ref IID_IUnknown, out res);

                //If the same class factory is returned as the one still
                //referenced by .Net, the call will be marshalled to the original thread
                //where that class factory was retrieved first.
                //Make .Net forget these objects
                Marshal.ReleaseComObject(unk);
                Marshal.ReleaseComObject(ClassFactory);
            } //lock

            return(res as IUnknown);
        }
Beispiel #5
0
        /// <summary>
        /// Gets a class factory for a specific COM Class ID.
        /// </summary>
        /// <param name="dllName">The dll where the COM class is implemented.</param>
        /// <param name="filterPersistClass">The requested Class ID.</param>
        /// <returns>IClassFactory instance used to create instances of that class.</returns>
        internal static IClassFactory GetClassFactory(string dllName, Guid filterPersistClass)
        {
            // Load the class factory from the dll.
            // By specifying the flags we allow to search for dependencies in the same folder as the file to be loaded
            // as well as default dirs like System32 and the Application folder.
            IntPtr dllHandle = Utilities.SystemAPI.NativeMethods.LoadLibraryEx(dllName, IntPtr.Zero,
                                                                               Utilities.SystemAPI.NativeMethods.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | Utilities.SystemAPI.NativeMethods.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);

            if (dllHandle == IntPtr.Zero)
            {
                return(null);
            }

            // Keep a reference to the dll until the process\AppDomain dies.
            DLL_LIST.AddDllHandle(dllHandle);

            //Get a pointer to the DllGetClassObject function
            IntPtr dllGetClassObjectPtr = Utilities.SystemAPI.NativeMethods.GetProcAddress(dllHandle, "DllGetClassObject");

            if (dllGetClassObjectPtr == IntPtr.Zero)
            {
                return(null);
            }

            // Convert the function pointer to a .net delegate.
            DllGetClassObject dllGetClassObject = (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));

            // Call the DllGetClassObject to retreive a class factory for out Filter class.
            Guid   baseFilterGuid   = filterPersistClass;
            Guid   classFactoryGuid = typeof(IClassFactory).GUID;
            Object unk;

            if (dllGetClassObject(ref baseFilterGuid, ref classFactoryGuid, out unk) != 0)
            {
                return(null);
            }

            return(unk as IClassFactory);
        }
Beispiel #6
0
            /// <summary>
            /// Gets a class factory for a specific COM Class ID.
            /// </summary>
            /// <param name="dllName">The dll where the COM class is implemented</param>
            /// <param name="filterPersistClass">The requested Class ID</param>
            /// <returns>IClassFactory instance used to create instances of that class</returns>
            /// <exception cref="System.Runtime.InteropServices.COMException">Thrown if the method can't creat COM-object</exception>
            /// <exception cref="System.Runtime.DllNotFoundException">Thrown if the dll not found</exception>
            public static IClassFactory GetClassFactory(string dllName, Guid filtersGuiid)
            {
                IntPtr dllHandle = loader.GetDLLHandle(dllName);
                Object unk;

                //Get a pointer to the DllGetClassObject function
                IntPtr dllGetClassObjectPtr = GetProcAddress(dllHandle, "DllGetClassObject");

                if (dllGetClassObjectPtr == IntPtr.Zero)
                {
                    return(null);
                }


                //Call the DllGetClassObject to retreive a class factory for out Filter class
                Guid IClassFactory_GUID = typeof(IClassFactory).GUID; //IClassFactory class id

                //Convert the function pointer to a .net delegate
                DllGetClassObject dllGetClassObject = (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));

                return((dllGetClassObject(ref filtersGuiid, ref IClassFactory_GUID, out unk) != 0) ? null : (unk as IClassFactory));
            }
Beispiel #7
0
        private static ClassFactoryWrapper GetClassFactoryFromDll(string dllName, string filterPersistClass)
        {
            //Load the dll
            IntPtr dllHandle = LoadLibrary(dllName);

            if (dllHandle == IntPtr.Zero)
            {
                return(null);
            }

            //Get a pointer to the DllGetClassObject function
            IntPtr dllGetClassObjectPtr = GetProcAddress(dllHandle, "DllGetClassObject");

            if (dllGetClassObjectPtr == IntPtr.Zero)
            {
                return(null);
            }

            //Convert the function pointer to a .net delegate
            DllGetClassObject dllGetClassObject =
                (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));

            //Call the DllGetClassObject to retreive a class factory for out Filter class
            Guid   filterPersistGuid = new Guid(filterPersistClass);
            Guid   classFactoryGuid  = new Guid("00000001-0000-0000-C000-000000000046");          //IClassFactory class id
            Object unk;

            if (dllGetClassObject(ref filterPersistGuid, ref classFactoryGuid, out unk) != 0)
            {
                return(null);
            }

            IClassFactory result = unk as IClassFactory;

            return(result != null ? new ClassFactoryWrapper(dllHandle, result) : null);
        }
Beispiel #8
0
        private static IClassFactory GetClassFactoryFromDll(string dllName, string filterPersistClass)
        {
            IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName);

            if (dllHandle == IntPtr.Zero)
            {
                return(null);
            }


            _dllList.AddDllHandle(dllHandle);


            IntPtr dllGetClassObjectPtr = Win32NativeMethods.GetProcAddress(dllHandle, "DllGetClassObject");

            if (dllGetClassObjectPtr == IntPtr.Zero)
            {
                return(null);
            }


            DllGetClassObject dllGetClassObject = (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));


            Guid   filterPersistGUID = new Guid(filterPersistClass);
            Guid   IClassFactoryGUID = new Guid("00000001-0000-0000-C000-000000000046");
            Object unk;

            if (dllGetClassObject(ref filterPersistGUID, ref IClassFactoryGUID, out unk) != 0)
            {
                return(null);
            }


            return(unk as IClassFactory);
        }
        /// <summary>
        /// Gets a class factory for a specific COM Class ID.
        /// </summary>
        /// <returns>IClassFactory instance used to create instances of that class.</returns>
        public IBaseFilter GetFilter()
        {
            if (_filter != null)
            {
                return(_filter);
            }

            // Load the class factory from the dll.
            // By specifying the flags we allow to search for dependencies in the same folder as the file to be loaded
            // as well as default dirs like System32 and the Application folder.
            List <uint> loadFlagsToTry = new List <uint>
            {
                Utilities.SystemAPI.NativeMethods.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | Utilities.SystemAPI.NativeMethods.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
                0x00000010 /*LOAD_IGNORE_CODE_AUTHZ_LEVEL*/,
                0 /*NONE*/
            };

            _dllHandle = IntPtr.Zero;
            foreach (var flags in loadFlagsToTry)
            {
                _dllHandle = Utilities.SystemAPI.NativeMethods.LoadLibraryEx(_dllName, IntPtr.Zero, flags);
                if (_dllHandle != IntPtr.Zero)
                {
                    break;
                }
#if DEBUG
                int error = Marshal.GetLastWin32Error();
                ServiceRegistration.Get <ILogger>().Warn("Failed to load library {0}, Flags: {1:X}, HR: {2:X}", _dllName, flags, error);
#endif
            }

            if (_dllHandle == IntPtr.Zero)
            {
                return(null);
            }

            //Get a pointer to the DllGetClassObject function
            IntPtr dllGetClassObjectPtr = Utilities.SystemAPI.NativeMethods.GetProcAddress(_dllHandle, "DllGetClassObject");
            if (dllGetClassObjectPtr == IntPtr.Zero)
            {
#if DEBUG
                int error = Marshal.GetLastWin32Error();
                ServiceRegistration.Get <ILogger>().Warn("Failed to GetProcAddress of DllGetClassObject in library {0} HR: {1:X}", _dllName, error);
                return(null);
#endif
            }

            // Convert the function pointer to a .net delegate.
            DllGetClassObject dllGetClassObject = (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));

            // Call the DllGetClassObject to retreive a class factory for out Filter class.
            Guid   classFactoryGuid = typeof(IClassFactory).GUID;
            object unk;
            if (dllGetClassObject(ref _classId, ref classFactoryGuid, out unk) != 0)
            {
#if DEBUG
                int error = Marshal.GetLastWin32Error();
                ServiceRegistration.Get <ILogger>().Warn("Failed to get class factory in library {0} HR: {1:X}", _dllName, error);
#endif
                return(null);
            }

            var classFactory = (unk as IClassFactory);
            if (classFactory == null)
            {
                return(null);
            }

            // And create an IFilter instance using that class factory
            Guid   baseFilterGuid = typeof(IBaseFilter).GUID;
            object obj;
            classFactory.CreateInstance(null, ref baseFilterGuid, out obj);

            Marshal.ReleaseComObject(classFactory);

            _filter = obj as IBaseFilter;
            return(_filter);
        }
Beispiel #10
0
        private static IUnknown NewRedemptionObject(Guid guid)
        {
            //try to set the thread COM apartment
            //Thread.CurrentThread.ApartmentState = ApartmentState.STA;

            object res = null;

            lock (_criticalSection)
            {
                IClassFactory ClassFactory;
                if (_redemptionDllHandle.Equals(IntPtr.Zero))
                {
                    string dllPath;
                    if (IntPtr.Size == 8) dllPath = DllLocation64Bit;
                    else dllPath = DllLocation32Bit;
                    _redemptionDllHandle = Win32NativeMethods.LoadLibraryW(dllPath);
                    if (_redemptionDllHandle.Equals(IntPtr.Zero))
                        //throw new Exception(string.Format("Could not load '{0}'\nMake sure the dll exists.", dllPath));
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    _dllGetClassObjectPtr = Win32NativeMethods.GetProcAddress(_redemptionDllHandle, "DllGetClassObject");
                    if (_dllGetClassObjectPtr.Equals(IntPtr.Zero))
                        //throw new Exception("Could not retrieve a pointer to the 'DllGetClassObject' function exported by the dll");
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    _dllGetClassObject =
                        (DllGetClassObject)
                        Marshal.GetDelegateForFunctionPointer(_dllGetClassObjectPtr, typeof (DllGetClassObject));
                }

                Object unk;
                int hr = _dllGetClassObject(ref guid, ref IID_IClassFactory, out unk);
                if (hr != 0) throw new Exception("DllGetClassObject failed with error code 0x"+hr.ToString("x8"));
                ClassFactory = unk as IClassFactory;
                ClassFactory.CreateInstance(null, ref IID_IUnknown, out res);

                //If the same class factory is returned as the one still
                //referenced by .Net, the call will be marshalled to the original thread
                //where that class factory was retrieved first.
                //Make .Net forget these objects
                Marshal.ReleaseComObject(unk);
                Marshal.ReleaseComObject(ClassFactory);
            } //lock

            return (res as IUnknown);
        }