Exemplo n.º 1
0
        public PlatformFuncs()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                OperatingSystem = OperatingSystem.Windows;

                // TryLoad ?
                _libHandle = NativeLibrary.Load("kernel32.dll");


                // TryGetExport
                var loadlibp = NativeLibrary.GetExport(_libHandle, "LoadLibraryA");
                _loadLibraryFunc = Marshal.GetDelegateForFunctionPointer <LoadLibraryDelegate>(loadlibp);

                // TryGetExport
                var getsymbolp = NativeLibrary.GetExport(_libHandle, "GetProcAddress");
                _getSymbolFunc = Marshal.GetDelegateForFunctionPointer <GetSymbolDelegate>(getsymbolp);

                // TryGetExport
                var freelibraryp = NativeLibrary.GetExport(_libHandle, "FreeLibrary");
                _freeLibraryFunc = Marshal.GetDelegateForFunctionPointer <FreeLibraryDelegate>(freelibraryp);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                OperatingSystem = OperatingSystem.Linux;

                // TryLoad ?
                _libHandle = NativeLibrary.Load("libdl.so");


                // TryGetExport
                var loadlibp  = NativeLibrary.GetExport(_libHandle, "dlopen");
                var loadlibpf = Marshal.GetDelegateForFunctionPointer <DlOpenDelegate>(loadlibp);
                _loadLibraryFunc = (path) => loadlibpf(path, RTLD_NOW);

                // TryGetExport
                var getsymbolp = NativeLibrary.GetExport(_libHandle, "dlsym");
                _getSymbolFunc = Marshal.GetDelegateForFunctionPointer <GetSymbolDelegate>(getsymbolp);

                // TryGetExport
                var freelibraryp = NativeLibrary.GetExport(_libHandle, "dlclose");
                _freeLibraryFunc = Marshal.GetDelegateForFunctionPointer <FreeLibraryDelegate>(freelibraryp);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                OperatingSystem = OperatingSystem.OSX;
                throw new NotImplementedException("Dynamic library loading on OSX is not implemented");
            }
            else
            {
                throw new NotImplementedException("Application is running on unknown operation system.");
            }
        }
Exemplo n.º 2
0
        private bool disposedValue = false; // To detect redundant calls


        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                NativeLibrary.Free(_libHandle);
                _libHandle = IntPtr.Zero;

                _loadLibraryFunc = null;
                _getSymbolFunc   = null;
                _freeLibraryFunc = null;

                disposedValue = true;
            }
        }