Esempio n. 1
0
        static void Main(string[] args)
        {
            var p = new Program();



            var loader = new AssemblySystem();


            var module = loader.LoadFrom(loader.GetAnyCpuPath(loader.BaseDirectory), "RegFreeComNativeConsumer.dll");

            //call dll entry point if needed -
            //var main = module.GetDelegate<DEF.DllMain>();//enty point is not declared as export
            //bool result = main(module.Handle, FDW_REASONS.DLL_PROCESS_ATTACH, IntPtr.Zero);
            //if (!result) throw new Exception("Failed to init dll");

            // push managed COM service into native method
            _service = new RegFreeSimpleObject();
            _service.FloatProperty = 42;
            _pointer = Marshal.GetIUnknownForObject(_service);
            var initialize = module.GetDelegate <Initialize>();
            int error      = initialize(_pointer);

            if (error != 0)
            {
                throw new Exception();
            }

            EnsureGC();
            _service.RaiseEnsureGCIsNotObstacle();

            // return string from C

            IntPtr strPtr = IntPtr.Zero;

            module.GetDelegate <GetStringResult>()(out strPtr);
            var str = Marshal.PtrToStringUni(strPtr);

            Marshal.FreeBSTR(strPtr);
            Console.WriteLine(str);

            // return array of COM services which return pointers
            var com            = loader.LoadFrom(loader.GetAnyCpuPath(loader.BaseDirectory), "RegFreeComNativeImplementer");
            var servicesGetter = com.GetDelegate <GetComInterface>();

            object[] services;
            servicesGetter(out services);
            var    srv = (RegFreeComNativeInterfacesLib.IMyService)services[0];
            IntPtr otherHandle;

            srv.GetSomeHanlde(out otherHandle);
            var srvRepeat = (RegFreeComNativeInterfacesLib.IMyService)Marshal.GetObjectForIUnknown(otherHandle);

            // pointer to itself was used to simplify testing
            Assert.AreEqual(services[0], srvRepeat);

            Console.ReadKey();
        }
        public void TestLoadeAndDisposeNativeDll()
        {
            var loader = new AssemblySystem();
            var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);
            var module = loader.LoadFrom(anyCpu, "RegFreeComNativeConsumer.dll");

            module.Dispose();
        }
        private static IAssembly LoadDll()
        {
            var loader = new AssemblySystem();
            var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);

            loader.AddSearchPath(anyCpu);
            var module = loader.LoadFrom(anyCpu, "NativeLibrary.dll");

            return(module);
        }
        public void TestDllDependenciesLoading()
        {
            var loader = new AssemblySystem();

            // C# loads C++ from Win32 or x64 subfolder
            var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);

            loader.AddSearchPath(anyCpu);
            var module = loader.LoadFrom(anyCpu, "NativeLibraryConsumer.dll");
            var fn     = module.GetDelegate <fnNativeLibraryConsumer>();

            object[] retval;

            Assert.IsTrue(42 == fn(out retval));
            loader.Dispose();
        }
 private static IAssembly LoadDll()
 {
     var loader = new AssemblySystem();
     var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);
     loader.AddSearchPath(anyCpu);
     var module = loader.LoadFrom(anyCpu, "NativeLibrary.dll");
     return module;
 }
 public void TestLoadeAndDisposeNativeDll()
 {
     var loader = new AssemblySystem();
     var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);
     var module = loader.LoadFrom(anyCpu, "RegFreeComNativeConsumer.dll");
     module.Dispose();
 }
        public void TestDllDependenciesLoading()
        {
            var loader = new AssemblySystem();

            // C# loads C++ from Win32 or x64 subfolder
            var anyCpu = loader.GetAnyCpuPath(loader.BaseDirectory);
            loader.AddSearchPath(anyCpu);
            var module = loader.LoadFrom(anyCpu, "NativeLibraryConsumer.dll");
            var fn = module.GetDelegate<fnNativeLibraryConsumer>();
            object[] retval;

            Assert.IsTrue(42 == fn(out retval));
            loader.Dispose();
        }