Exemple #1
0
        /// <summary>
        /// Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        private IntPtr LoadSymbol(string symbolName)
        {
            IntPtr pResult = IntPtr.Zero;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) /* PlatformApis.IsWindows */
            {
                int    ErrCode  = 0;
                string errorMsg = "";

                // See http://stackoverflow.com/questions/10473310 for background on this.
                if (Environment.Is64BitProcess) /* PlatformApis.Is64Bit */
                {
                    pResult = Windows.GetProcAddress(this.handle, symbolName);
                    if (pResult == IntPtr.Zero)
                    {
                        ErrCode  = Marshal.GetLastWin32Error();
                        errorMsg = Windows.GetLastErrMsg((uint)ErrCode);
                        Console.WriteLine("Error while loading function: " + symbolName + "\n\t" + errorMsg);
                    }
                    return(pResult);
                }
                else
                {
                    // Yes, we could potentially predict the size... but it's a lot simpler to just try
                    // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                    // many options - and if it takes a little bit longer to fail if we've really got the wrong
                    // library, that's not a big problem. This is only called once per function in the native library.
                    symbolName = "_" + symbolName + "@";
                    for (int stackSize = 0; stackSize < 128; stackSize += 4)
                    {
                        pResult = Windows.GetProcAddress(this.handle, symbolName + stackSize);
                        if (pResult != IntPtr.Zero)
                        {
                            return(pResult);
                        }
                    }
                    // Fail.
                    return(IntPtr.Zero);
                }
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (InteropRuntimeConfig.IsRunningOnMono) /* PlatformApis.IsMono */
                {
                    return(Mono.dlsym(this.handle, symbolName));
                }
                if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")) /* PlatformApis.IsNetCore */
                {
                    return(CoreCLR.dlsym(this.handle, symbolName));
                }
                return(Linux.dlsym(this.handle, symbolName));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))  /* PlatformApis.IsMacOSX */
            {
                return(MacOSX.dlsym(this.handle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }
 private IntPtr LoadSymbol(string symbolName)
 {
     if (PlatformApis.IsLinux)
     {
         if (PlatformApis.IsNetCore)
         {
             return(CoreCLR.dlsym(_handle, symbolName));
         }
     }
     throw new InvalidOperationException("Unsupported Platform.");
 }
Exemple #3
0
        /// <summary>
        /// Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        private IntPtr LoadSymbol(string symbolName)
        {
            if (PlatformApis.IsWindows)
            {
                // See http://stackoverflow.com/questions/10473310 for background on this.

                //if (PlatformApis.Is64Bit)
                //{
                //    return Windows.GetProcAddress(this.handle, symbolName);
                //}
                //else
                //{
                //    // Yes, we could potentially predict the size... but it's a lot simpler to just try
                //    // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                //    // many options - and if it takes a little bit longer to fail if we've really got the wrong
                //    // library, that's not a big problem. This is only called once per function in the native library.
                //    symbolName = "_" + symbolName + "@";
                //    for (int stackSize = 0; stackSize < 128; stackSize += 4)
                //    {
                //        IntPtr candidate = Windows.GetProcAddress(this.handle, symbolName + stackSize);
                //        if (candidate != IntPtr.Zero)
                //        {
                //            return candidate;
                //        }
                //    }
                //    // Fail.
                //    return IntPtr.Zero;
                //}

                // VLFD.dll 比较特殊
                // 虽然是32位的,但符号和64位相同
                return(Windows.GetProcAddress(this.handle, symbolName));
            }
            if (PlatformApis.IsLinux)
            {
                if (PlatformApis.IsMono)
                {
                    return(Mono.dlsym(this.handle, symbolName));
                }
                if (PlatformApis.IsNetCore)
                {
                    return(CoreCLR.dlsym(this.handle, symbolName));
                }
                return(Linux.dlsym(this.handle, symbolName));
            }
            if (PlatformApis.IsMacOSX)
            {
                return(MacOSX.dlsym(this.handle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }
        /// <summary>
        ///     Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        public IntPtr LoadSymbol(string symbolName)
        {
            if (IsWindows)
            {
                // See http://stackoverflow.com/questions/10473310 for background on this.
                if (Is64Bit)
                {
                    return(Windows.GetProcAddress(NativeLibraryHandle, symbolName));
                }
                // Yes, we could potentially predict the size... but it's a lot simpler to just try
                // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                // many options - and if it takes a little bit longer to fail if we've really got the wrong
                // library, that's not a big problem. This is only called once per function in the native library.
                symbolName = "_" + symbolName + "@";
                for (var stackSize = 0; stackSize < 128; stackSize += 4)
                {
                    var candidate = Windows.GetProcAddress(NativeLibraryHandle, symbolName + stackSize);
                    if (candidate != IntPtr.Zero)
                    {
                        return(candidate);
                    }
                }

                // Fail.
                return(IntPtr.Zero);
            }

            if (IsLinux)
            {
                if (IsMono)
                {
                    return(Mono.dlsym(NativeLibraryHandle, symbolName));
                }
                if (IsNetCore)
                {
                    return(CoreCLR.dlsym(NativeLibraryHandle, symbolName));
                }
                return(Linux.dlsym(NativeLibraryHandle, symbolName));
            }

            if (IsMacOSPlatform)
            {
                return(MacOSX.dlsym(NativeLibraryHandle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }