public static void GetLibraryMethod <T>(SupportedPlatform platform, IntPtr handle, string name, out T t)
    {
        IntPtr result;

        switch (platform)
        {
        case SupportedPlatform.Windows:
            result = NativeWindowsMethods.GetProcAddress(handle, name);
            if (result == IntPtr.Zero)
            {
                throw new EntryPointNotFoundException(name, GetLastWindowsError());
            }
            break;

        case SupportedPlatform.Linux:
        case SupportedPlatform.MacOSX:
            result = NativeUnixMehods.dlsym(handle, name);
            if (result == IntPtr.Zero)
            {
                throw new EntryPointNotFoundException(name, GetLastErrorUnix());
            }
            break;

        default: throw new NotSupportedException();
        }
        t = (T)(object)Marshal.GetDelegateForFunctionPointer(result, typeof(T));
    }
    private static bool LoadDynamicLibrary(SupportedPlatform platform, string name, out IntPtr handle, out string location)
    {
        switch (platform)
        {
        case SupportedPlatform.Windows:
            handle = NativeWindowsMethods.LoadLibrary(name);
            if (handle == IntPtr.Zero)
            {
                goto default;
            }
            location = GetLocationWindows(handle) ?? name;
            return(true);

        case SupportedPlatform.Linux:
        case SupportedPlatform.MacOSX:
            handle = NativeUnixMehods.dlopen(name, 2 /* RTLD_NOW */);
            if (handle == IntPtr.Zero)
            {
                goto default;
            }
            location = GetLocationUnix(handle) ?? name;
            return(true);

        default:
            handle   = IntPtr.Zero;
            location = null;
            return(false);
        }
    }
Example #3
0
        public static IntPtr HeapAlloc(IntPtr hHeap, uint flags, UIntPtr size)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.HeapAlloc(hHeap, flags, size));
            }

            throw new PlatformNotSupportedException();
        }
Example #4
0
        public static bool HeapDestroy(IntPtr hHeap)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.HeapDestroy(hHeap));
            }

            throw new PlatformNotSupportedException();
        }
Example #5
0
        public static uint VariantClear(IntPtr pVariant)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.VariantClear(pVariant));
            }

            throw new PlatformNotSupportedException();
        }
Example #6
0
        public static IntPtr HeapCreate(uint options, UIntPtr initialSize, UIntPtr maximumSize)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.HeapCreate(options, initialSize, maximumSize));
            }

            throw new PlatformNotSupportedException();
        }
Example #7
0
        public static uint CoCreateInstance(ref Guid clsid, IntPtr pOuter, uint clsContext, ref Guid iid, out IntPtr pInterface)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.CoCreateInstance(ref clsid, pOuter, clsContext, ref iid, out pInterface));
            }

            throw new PlatformNotSupportedException();
        }
Example #8
0
        public static uint CLSIDFromProgID(string progID, out Guid clsid)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.CLSIDFromProgID(progID, out clsid));
            }

            throw new PlatformNotSupportedException();
        }
Example #9
0
        public static uint ProgIDFromCLSID(ref Guid clsid, out string progID)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.ProgIDFromCLSID(ref clsid, out progID));
            }

            throw new PlatformNotSupportedException();
        }
Example #10
0
        public static bool VirtualProtect(IntPtr pBlock, UIntPtr size, uint newProtect, out uint oldProtect)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.VirtualProtect(pBlock, size, newProtect, out oldProtect));
            }

            throw new PlatformNotSupportedException();
        }
Example #11
0
        public static bool FreeLibrary(IntPtr hLibrary)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.FreeLibrary(hLibrary));
            }

            throw new PlatformNotSupportedException();
        }
Example #12
0
        public static bool HeapFree(IntPtr hHeap, uint flags, IntPtr pBlock)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.HeapFree(hHeap, flags, pBlock));
            }

            throw new PlatformNotSupportedException();
        }
Example #13
0
        public static IntPtr LoadLibraryW(string path)
        {
            if (MiscHelpers.PlatformIsWindows())
            {
                return(NativeWindowsMethods.LoadLibraryW(path));
            }

            throw new PlatformNotSupportedException();
        }
Example #14
0
 public static void VariantInit(IntPtr pVariant)
 {
     if (MiscHelpers.PlatformIsWindows())
     {
         NativeWindowsMethods.VariantInit(pVariant);
     }
     else
     {
         throw new PlatformNotSupportedException();
     }
 }
    private static string GetLocationWindows(IntPtr handle)
    {
        byte[] bytes  = new byte[260];
        int    length = NativeWindowsMethods.GetModuleFileName(handle, bytes, bytes.Length);

        if (length <= 0 || length == bytes.Length)
        {
            return(null);
        }
        return(Encoding.Default.GetString(bytes, 0, length));
    }
Example #16
0
        /// <summary>
        /// Sets the directory that contains the Native library. This currently only works on Windows.
        /// </summary>
        /// <param name="path">The path of the directory that contains the native library.</param>
        public static void SetNativeLibraryDirectory(string path)
        {
#if NETSTANDARD
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                NativeWindowsMethods.SetDllDirectory(FileHelper.GetFullPath(path));
            }
#else
            NativeWindowsMethods.SetDllDirectory(FileHelper.GetFullPath(path));
#endif
        }
Example #17
0
 public static void GetNativeSystemInfo(out SystemInfo info)
 {
     if (MiscHelpers.PlatformIsWindows())
     {
         NativeWindowsMethods.GetNativeSystemInfo(out info);
     }
     else
     {
         throw new PlatformNotSupportedException();
     }
 }
    public static void UnloadDynamicLibrary(SupportedPlatform platform, IntPtr handle)
    {
        switch (platform)
        {
        case SupportedPlatform.Windows:
            if (NativeWindowsMethods.FreeLibrary(handle) == false)
            {
                throw GetLastWindowsError();
            }
            break;

        case SupportedPlatform.Linux:
        case SupportedPlatform.MacOSX:
            if (NativeUnixMehods.dlclose(handle) != 0)
            {
                throw GetLastErrorUnix() ?? new InvalidOperationException();
            }
            break;

        default: throw new NotSupportedException();
        }
    }
Example #19
0
 /// <summary>
 /// Sets the directory that contains the Native library.
 /// </summary>
 /// <param name="path">The path of the directory that contains the native library.</param>
 public static void SetNativeLibraryDirectory(string path) => NativeWindowsMethods.SetDllDirectory(FileHelper.GetFullPath(path));