Пример #1
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);
		}
Пример #2
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);
        }
Пример #3
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (!_dllHandle.Equals(IntPtr.Zero))
         {
             var dllCanUnloadNowPtr = Win32NativeMethods.GetProcAddress(_dllHandle, "DllCanUnloadNow");
             if (!dllCanUnloadNowPtr.Equals(IntPtr.Zero))
             {
                 var dllCanUnloadNow = (DllCanUnloadNow)Marshal.GetDelegateForFunctionPointer(dllCanUnloadNowPtr, typeof(DllCanUnloadNow));
                 if (dllCanUnloadNow() != 0)
                 {
                     return;                         //there are still live objects returned by the dll, so we should not unload the dll
                 }
             }
             Win32NativeMethods.FreeLibrary(_dllHandle);
             _dllHandle = IntPtr.Zero;
         }
     }
 }
Пример #4
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);
        }