public void DisposedLibraryWithoutGeneratedChecksDoesNotThrow()
        {
            var library = new NativeLibraryBuilder().ActivateInterface <IDisposeCheckLibrary>(LibraryName);

            library.Dispose();
            library.Multiply(5, 5);
        }
Esempio n. 2
0
            public void LoadingSameInterfaceAndSameFileTwiceUsesSameGeneratedType()
            {
                var secondLoad = new NativeLibraryBuilder(ImplementationOptions.GenerateDisposalChecks)
                                 .ActivateInterface <IBaseLibrary>(LibraryName);

                Assert.IsType(Library.GetType(), secondLoad);
            }
Esempio n. 3
0
 public void Setup()
 {
     ADLLibrary = new NativeLibraryBuilder(GenerateDisposalChecks).ActivateInterface <ITest>(Program.LibraryName);
     ADLLibraryWithoutDisposeChecks   = new NativeLibraryBuilder().ActivateInterface <ITest>(Program.LibraryName);
     ADLLibraryWithSuppressedSecurity = new NativeLibraryBuilder(SuppressSecurity).ActivateInterface <ITest>(Program.LibraryName);
     ADLLibraryWithCalli = new NativeLibraryBuilder(UseIndirectCalls).ActivateInterface <ITest>(Program.LibraryName);
 }
        public void RespectsStringMarshallingAttribute()
        {
            var library  = new NativeLibraryBuilder().ActivateInterface <IAttributePassthroughLibrary>(LibraryName);
            var expected = "Äta gurka i en båt på ön";

            Assert.Equal(expected, library.EchoWString(expected));
        }
        public void LoadingSameInterfaceAndSameFileButWithDifferentOptionsDoesNotUseSameGeneratedType()
        {
            var options = ImplementationOptions.UseLazyBinding;

            var secondLoad = new NativeLibraryBuilder(options).ActivateInterface <IBaseLibrary>(LibraryName);

            Assert.IsNotType(Library.GetType(), secondLoad);
        }
Esempio n. 6
0
 public static void CreateBuilder <T1>(ISymbolLoader loader = null) where T1 : NativeAPI
 {
     if (!_builders.ContainsKey(typeof(T1)) || loader != null)
     {
         _builders[typeof(T1)] = new NativeLibraryBuilder(Options)
                                 .WithSymbolLoader(x => loader ?? x);
     }
 }
        public void LoadingSameInterfaceAndSameFileButWithDifferentOptionsProducesDifferentReferences()
        {
            var options = ImplementationOptions.UseLazyBinding;

            var secondLoad = new NativeLibraryBuilder(options).ActivateInterface <IBaseLibrary>(LibraryName);

            Assert.NotSame(Library, secondLoad);
        }
Esempio n. 8
0
        static APILoader()
        {
            _options = ImplementationOptions.EnableOptimizations |
                       ImplementationOptions.SuppressSecurity |
                       ImplementationOptions.GenerateDisposalChecks |
                       ImplementationOptions.EnableDllMapSupport;

            _builder = new NativeLibraryBuilder(_options);
        }
Esempio n. 9
0
        public SolcLibAdvDLProvider()
        {
            var config = ImplementationOptions.UseLazyBinding;

            NativeLibFilePath = LibPathResolver.Resolve(LIB_FILE);
            var builder = new NativeLibraryBuilder(config);

            _native = builder.ActivateInterface <IAdvDLSolcLib>(NativeLibFilePath);
        }
        public void RespectsReturnValueMarshalAs()
        {
            var library = new NativeLibraryBuilder().ActivateInterface <IAttributePassthroughLibrary>(LibraryName);

            Assert.True(library.CheckIfGreaterThanZero(1));
            Assert.False(library.CheckIfGreaterThanZero(0));
            Assert.True(library.CheckIfStringIsNull(null));
            Assert.False(library.CheckIfStringIsNull("Hello!"));
        }
        public void CanGetNewInstanceOfInterfaceAfterDisposalOfExistingInstance()
        {
            Library.Dispose();

            var newLibrary = new NativeLibraryBuilder(Config).ActivateInterface<IDisposeCheckLibrary>(LibraryName);

            newLibrary.Multiply(5, 5);
            Assert.NotSame(Library, newLibrary);
        }
            public void CanDiscoverPrecompiledTypes()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result = Builder.Build(OutputDirectory);

                var searchPattern = $"*{result}*.dll";

                NativeLibraryBuilder.DiscoverCompiledTypes(OutputDirectory, searchPattern);
            }
Esempio n. 13
0
        public static T1 Load <T1, T2>(T2 baseApi, SearchPathContainer paths, ISymbolLoader loader)
            where T1 : NativeExtension <T2> where T2 : NativeAPI
        {
            var builder = new NativeLibraryBuilder(Options).WithSymbolLoader(x => loader ?? x);
            var extAttr = GetExtensionAttribute(typeof(T1));

            return(baseApi.IsExtensionPresent(extAttr.Name)
                ? builder.ActivateClass <T1>((paths ?? baseApi.SearchPaths).GetLibraryName())
                : null);
        }
            public void CanDiscoverPrecompiledTypesFromStream()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result = Builder.Build(OutputDirectory);

                foreach (var asm in Directory.GetFiles(OutputDirectory, $"*{result}*.dll"))
                {
                    NativeLibraryBuilder.DiscoverCompiledTypes(File.OpenRead(asm));
                }
            }
Esempio n. 15
0
        protected override bool ReleaseHandle()
        {
            if (!this.IsInvalid)
            {
                var        NativeLibraryBuilder = new NativeLibraryBuilder();
                IX3DNative library = NativeLibraryBuilder.Default.ActivateInterface <IX3DNative>("EngineRenderer");

                library.x3d_drop_shader(handle);
            }

            return(true);
        }
            public void CanDiscoverPrecompiledTypes()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result = Builder.Build(OutputDirectory);

                var searchPattern = $"*{Path.GetFileNameWithoutExtension(result)}*.dll";

                searchPattern = Path.Combine(Path.GetDirectoryName(result), searchPattern);

                NativeLibraryBuilder.DiscoverCompiledTypes(OutputDirectory, searchPattern);
            }
Esempio n. 17
0
        public SolcLibAdvDLProvider()
        {
            var config   = ImplementationOptions.UseLazyBinding;
            var resolver = new LibFilePathResolver();

            try
            {
                var builder = new NativeLibraryBuilder(config, resolver);
                _native = builder.ActivateInterface <IAdvDLSolcLib>(LIB_FILE);
            }
            catch (FileNotFoundException)
            {
                var result = resolver.Resolve(LIB_FILE);
                throw result.Exception ?? new Exception(result.ErrorReason);
            }
        }
            public void UsesPrecompiledTypesIfDiscovered()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result = Builder.Build(OutputDirectory);

                var searchPattern = $"*{result}*.dll";

                NativeLibraryBuilder.DiscoverCompiledTypes(OutputDirectory, searchPattern);

                var library = LibraryBuilder.ActivateInterface <IAOTLibrary>("AOTTests");

                var libraryAssembly = library.GetType().Assembly;

                Assert.False(libraryAssembly.GetCustomAttribute <AOTAssemblyAttribute>() is null);
            }
            public void UsesPrecompiledTypesIfDiscoveredFromStream()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result = Builder.Build(OutputDirectory);

                foreach (var asm in Directory.GetFiles(OutputDirectory, $"*{result}*.dll"))
                {
                    NativeLibraryBuilder.DiscoverCompiledTypes(File.OpenRead(asm));
                }

                var library = LibraryBuilder.ActivateInterface <IAOTLibrary>("AOTTests");

                var libraryAssembly = library.GetType().Assembly;

                Assert.False(libraryAssembly.GetCustomAttribute <AOTAssemblyAttribute>() is null);
            }
Esempio n. 20
0
        public static INativeSolcLib Create()
        {
            var config   = ImplementationOptions.UseLazyBinding;
            var resolver = new LibFilePathResolver();

            try
            {
                var builder = new NativeLibraryBuilder(config, resolver);
                var library = builder.ActivateClass <AdvDLSolcLibBase, IAdvDLSolcLib>(LIB_FILE);
                return(library);
            }
            catch (FileNotFoundException)
            {
                var result = resolver.Resolve(LIB_FILE);
                throw result.Exception ?? new Exception(result.ErrorReason);
            }
        }
            public void UsesPrecompiledTypesIfDiscoveredFromStream()
            {
                // Pregenerate the types
                Builder.WithSourceAssembly(GetType().Assembly);
                var result        = Builder.Build(OutputDirectory);
                var searchPattern = $"*{Path.GetFileNameWithoutExtension(result)}*.dll";

                searchPattern = Path.Combine(Path.GetDirectoryName(result), searchPattern);
                foreach (var asm in Directory.GetFiles(OutputDirectory, searchPattern))
                {
                    NativeLibraryBuilder.DiscoverCompiledTypes(File.OpenRead(asm));
                }

                var library = LibraryBuilder.ActivateInterface <IAOTLibrary>("AOTTests");

                var libraryAssembly = library.GetType().Assembly;

                Assert.False(libraryAssembly.GetCustomAttribute <AOTAssemblyAttribute>() is null);
            }
Esempio n. 22
0
        public static T1 Load <T1>(SearchPathContainer nameContainer, ISymbolLoader loader) where T1 : NativeAPI
        {
            var builder = new NativeLibraryBuilder(Options).WithSymbolLoader(x => loader ?? x);

            return(builder.ActivateClass <T1>(nameContainer.GetLibraryName()));
        }
Esempio n. 23
0
        static Interop()
        {
            string pluginsDirectory = null;
            string qmlDirectory     = null;

            ILibraryPathResolver pathResolver = null;
            var internalType = Type.GetType("AdvancedDLSupport.DynamicLinkLibraryPathResolver, AdvancedDLSupport");

            if (internalType != null)
            {
                pathResolver = (ILibraryPathResolver)Activator.CreateInstance(internalType, new object[] { true });

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // This custom path resolver attempts to do a DllImport to get the path that .NET decides.
                    // It may load a special dll from a NuGet package.
                    pathResolver = new WindowsDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    pathResolver = new MacDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    pathResolver = new LinuxDllImportLibraryPathResolver(pathResolver);
                }

                var resolveResult = pathResolver.Resolve("QmlNet");

                if (resolveResult.IsSuccess)
                {
                    var directory = Path.GetDirectoryName(resolveResult.Path);
                    if (!string.IsNullOrEmpty(directory))
                    {
                        // If this library has a plugins/qml directory below it, set it.
                        var potentialPlugisDirectory = Path.Combine(directory, "plugins");
                        if (Directory.Exists(potentialPlugisDirectory))
                        {
                            pluginsDirectory = potentialPlugisDirectory;
                        }

                        var potentialQmlDirectory = Path.Combine(directory, "qml");
                        if (Directory.Exists(potentialQmlDirectory))
                        {
                            qmlDirectory = potentialQmlDirectory;
                        }
                    }
                }
            }


            var builder = new NativeLibraryBuilder(pathResolver: pathResolver);

            var interop = builder.ActivateInterface <ICombined>("QmlNet");

            Callbacks             = interop;
            NetTypeInfo           = interop;
            NetMethodInfo         = interop;
            NetPropertyInfo       = interop;
            NetTypeManager        = interop;
            QGuiApplication       = interop;
            QQmlApplicationEngine = interop;
            NetVariant            = interop;
            NetReference          = interop;
            NetVariantList        = interop;
            NetTestHelper         = interop;
            NetSignalInfo         = interop;
            QResource             = interop;
            NetDelegate           = interop;
            NetJsValue            = interop;
            QQuickStyle           = interop;
            QtInterop             = interop;
            Utilities             = interop;

            if (!string.IsNullOrEmpty(pluginsDirectory))
            {
                Qt.PutEnv("QT_PLUGIN_PATH", pluginsDirectory);
            }
            if (!string.IsNullOrEmpty(qmlDirectory))
            {
                Qt.PutEnv("QML2_IMPORT_PATH", qmlDirectory);
            }

            var cb = DefaultCallbacks.Callbacks();

            Callbacks.RegisterCallbacks(ref cb);
        }
        public void LoadingSameInterfaceAndSameFileTwiceProducesDifferentReferences()
        {
            var secondLoad = new NativeLibraryBuilder().ActivateInterface <IBaseLibrary>(LibraryName);

            Assert.NotSame(Library, secondLoad);
        }
        public void LoadingSameInterfaceAndSameFileTwiceUsesSameGeneratedType()
        {
            var secondLoad = new NativeLibraryBuilder().ActivateInterface <IBaseLibrary>(LibraryName);

            Assert.IsType(Library.GetType(), secondLoad);
        }
Esempio n. 26
0
        public MixedModeTests()
        {
            _builder = new NativeLibraryBuilder();

            _mixedModeClass = _builder.ActivateClass <MixedModeClass, IMixedModeLibrary>(LibraryName);
        }
        public MixedModeTests()
        {
            _builder = new NativeLibraryBuilder(ImplementationOptions.GenerateDisposalChecks);

            _mixedModeClass = _builder.ActivateClass <MixedModeClass, IMixedModeLibrary>(LibraryName);
        }
Esempio n. 28
0
        static NativeLibrary()
        {
            var activator = new NativeLibraryBuilder();

            IPHelper = activator.ActivateInterface <IIPHelper>("Iphlpapi.dll");
        }
Esempio n. 29
0
        static Interop()
        {
            string pluginsDirectory = null;
            string qmlDirectory     = null;
            string libDirectory     = null;

            ILibraryPathResolver pathResolver = null;

            if (Host.GetExportedSymbol != null)
            {
                // We are loading exported functions from the currently running executable.
                var member = (FieldInfo)typeof(NativeLibraryBase).GetMember("PlatformLoader", BindingFlags.Static | BindingFlags.NonPublic).First();
                member.SetValue(null, new Host.Loader());
                pathResolver = new Host.Loader();
            }
            else
            {
                var internalType = Type.GetType("AdvancedDLSupport.DynamicLinkLibraryPathResolver, AdvancedDLSupport");
                if (internalType != null)
                {
                    pathResolver = (ILibraryPathResolver)Activator.CreateInstance(internalType, new object[] { true });

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        // This custom path resolver attempts to do a DllImport to get the path that .NET decides.
                        // It may load a special dll from a NuGet package.
                        pathResolver = new WindowsDllImportLibraryPathResolver(pathResolver);
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                    {
                        pathResolver = new MacDllImportLibraryPathResolver(pathResolver);
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        pathResolver = new LinuxDllImportLibraryPathResolver(pathResolver);
                    }

                    var resolveResult = pathResolver.Resolve("QmlNet");

                    if (resolveResult.IsSuccess)
                    {
                        libDirectory = Path.GetDirectoryName(resolveResult.Path);
                        if (!string.IsNullOrEmpty(libDirectory))
                        {
                            // If this library has a plugins/qml directory below it, set it.
                            var potentialPlugisDirectory = Path.Combine(libDirectory, "plugins");
                            if (Directory.Exists(potentialPlugisDirectory))
                            {
                                pluginsDirectory = potentialPlugisDirectory;
                            }

                            var potentialQmlDirectory = Path.Combine(libDirectory, "qml");
                            if (Directory.Exists(potentialQmlDirectory))
                            {
                                qmlDirectory = potentialQmlDirectory;
                            }
                        }
                    }
                }
            }


            var builder = new NativeLibraryBuilder(pathResolver: pathResolver);

            var interop = builder.ActivateInterface <ICombined>("QmlNet");

            Callbacks             = interop;
            NetTypeInfo           = interop;
            NetMethodInfo         = interop;
            NetPropertyInfo       = interop;
            NetTypeManager        = interop;
            QGuiApplication       = interop;
            QQmlApplicationEngine = interop;
            NetVariant            = interop;
            NetReference          = interop;
            NetVariantList        = interop;
            NetTestHelper         = interop;
            NetSignalInfo         = interop;
            QResource             = interop;
            NetDelegate           = interop;
            NetJsValue            = interop;
            QQuickStyle           = interop;
            QtInterop             = interop;
            Utilities             = interop;
            QtWebEngine           = interop;

            if (!string.IsNullOrEmpty(pluginsDirectory))
            {
                Qt.PutEnv("QT_PLUGIN_PATH", pluginsDirectory);
            }
            if (!string.IsNullOrEmpty(qmlDirectory))
            {
                Qt.PutEnv("QML2_IMPORT_PATH", qmlDirectory);
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (!string.IsNullOrEmpty(libDirectory) && Directory.Exists(libDirectory))
                {
                    // Even though we opened up the native dll correctly, we need to add
                    // the folder to the path. The reason is because QML plugins aren't
                    // in the same directory and have trouble finding dependencies
                    // that are within our lib folder.
                    Environment.SetEnvironmentVariable("PATH",
                                                       Environment.GetEnvironmentVariable("PATH") + $";{libDirectory}");
                }
            }

            var cb = DefaultCallbacks.Callbacks();

            Callbacks.RegisterCallbacks(ref cb);
        }
Esempio n. 30
0
 protected NativeLibraryBuilderTestBase()
 {
     LibraryBuilder = new NativeLibraryBuilder(GetImplementationOptions());
 }