public static void Initialize() { Environment.CurrentDirectory = AppInformation.Directory; UnmanagedHandler = HandleUnmanagedException; AddGslLocationToEnvironmentalVariableForLibraries(); byte[] gsl; if (RuntimeInformation.Is64Bit) { gsl = RuntimeInformation.IsUnix ? (RuntimeInformation.IsMacOS ? Resources.libgsl_osx_amd64 : Resources.libgsl_amd64) : Resources.gsl_x64; } else if (RuntimeInformation.Is32Bit) { gsl = RuntimeInformation.IsUnix ? (RuntimeInformation.IsMacOS ? Resources.libgsl_osx_i686 : Resources.libgsl_i686) : Resources.gsl_x86; } else { throw new PlatformNotSupportedException( "Inconsistent operating system. Handles only 32 and 64 bit OS."); } ExtractEmbeddedDlls(GslConfig.GslLibraryName, gsl); switch (Settings.Default.CalculationsErrors) { case CalculationsErrors.ReturnNAN: NativeMethods.gsl_set_error_handler_off(); break; case CalculationsErrors.ShowError: NativeMethods.gsl_set_error_handler(UnmanagedHandler); break; } }
public static void Initialize() { UnmanagedHandler = HandleUnmanagedException; // var msvcr = Resources.msvcr120_x86; var gsl = Resources.gsl_x86; // var cblas = Resources.cblas_x86; if (Environment.Is64BitProcess && IntPtr.Size == 8) { //msvcr = Resources.msvcr120_x64; gsl = Resources.gsl_x64; // cblas = Resources.cblas_x64; } //else if (!Environment.Is64BitProcess && IntPtr.Size == 4) //else // throw new Exception("Inconsistent system - IntPtr.Size and Environment.Is64BitProcess dont match"); try { // EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.msvcrDllName, msvcr); EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.gslDllName, gsl); // EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.gslCblasDllName, cblas); // System.Threading.Thread.Sleep(1000); NativeMethods.gsl_set_error_handler(UnmanagedHandler); } catch (Exception exception) { logger.Log("ExtractEmbeddedDlls failed", ErrorType.General, exception); try { // string msvcrTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.msvcrDllName); var gslTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.gslDllName); // string cblasTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.gslCblasDllName); // File.WriteAllBytes(msvcrTempPath, msvcr); // File.WriteAllBytes(cblasTempPath, cblas); File.WriteAllBytes(gslTempPath, gsl); // var h0 = NativeMethods.LoadLibrary(msvcrTempPath); // var h1 = NativeMethods.LoadLibrary(cblasTempPath); var h2 = NativeMethods.LoadLibrary(gslTempPath); if (/*h0 == IntPtr.Zero || h1 == IntPtr.Zero ||*/ h2 == IntPtr.Zero) { throw new Win32Exception( $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{gslTempPath}'{Environment.NewLine}.", new Win32Exception()); // Calls GetLastError } NativeMethods.gsl_set_error_handler(UnmanagedHandler); } catch (Exception exception2) { logger.Log("LoadLibrary failed", ErrorType.General, exception2); MessageBox.Show( $"{Strings.Program_Main_Exception_during_startup}.{Environment.NewLine}ExtractEmbeddedDlls {Strings.Exception}:{Environment.NewLine}{exception}{Environment.NewLine}LoadLibrary {Strings.Exception}:{Environment.NewLine}{exception2}", Strings.Error); } } }
public static extern gsl_error_handler_t gsl_set_error_handler(gsl_error_handler_t new_handler);
public static void Initialize(IMessagingService messagingService) { UnmanagedHandler = HandleUnmanagedException; byte[] gsl; if (Environment.Is64BitProcess && IntPtr.Size == 8) { gsl = GlobalConfig.IsUnix ? (GlobalConfig.IsMacOS ? Resources.libgsl_osx_amd64 : Resources.libgsl_amd64) : Resources.gsl_x64; } else if (!Environment.Is64BitProcess && IntPtr.Size == 4) { gsl = GlobalConfig.IsUnix ? (GlobalConfig.IsMacOS ? Resources.libgsl_osx_i686 : Resources.libgsl_i686) : Resources.gsl_x86; } else { throw new PlatformNotSupportedException("Inconsistent operating system. Handles only 32 and 64 bit OS."); } var cblas = Environment.Is64BitProcess ? (GlobalConfig.IsMacOS ? Resources.libgslcblas_osx_amd64 : Resources.libgslcblas_amd64) : (GlobalConfig.IsMacOS ? Resources.libgslcblas_osx_i686 : Resources.libgslcblas_i686); try { EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.GslDllName, gsl); if (GlobalConfig.IsUnix) { EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.GslCblasDllName, cblas); } switch (Settings.Default.CalculationsErrors) { case CalculationsErrors.ReturnNAN: NativeMethods.gsl_set_error_handler_off(); break; case CalculationsErrors.ShowError: NativeMethods.gsl_set_error_handler(UnmanagedHandler); break; } } catch (Exception exception) { logger.Log("ExtractEmbeddedDlls failed", ErrorType.General, exception); try { var gslTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.GslDllName); File.WriteAllBytes(gslTempPath, gsl); if (GlobalConfig.IsUnix) { var cblasTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.GslCblasDllName); File.WriteAllBytes(cblasTempPath, cblas); var h1 = NativeMethods.dlopen(cblasTempPath, NativeMethods.RTLD.RTLD_GLOBAL); if (h1 == IntPtr.Zero) { throw new Win32Exception( $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{cblasTempPath}'{Environment.NewLine}.", new Win32Exception()); // Calls GetLastError } } var h2 = GlobalConfig.IsUnix ? NativeMethods.dlopen(gslTempPath, NativeMethods.RTLD.RTLD_GLOBAL) : NativeMethods.LoadLibrary(gslTempPath); if (h2 == IntPtr.Zero) { throw new Win32Exception( $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{gslTempPath}'{Environment.NewLine}.", new Win32Exception()); // Calls GetLastError } NativeMethods.gsl_set_error_handler(UnmanagedHandler); } catch (Exception exception2) { var funcName = GlobalConfig.IsUnix ? "dlopen" : "LoadLibrary"; logger.Log($"{funcName} failed", ErrorType.General, exception2); messagingService.Show( $"{Strings.Program_Main_Exception_during_startup}.{Environment.NewLine}ExtractEmbeddedDlls {Strings.Exception}:{Environment.NewLine}{exception}{Environment.NewLine}{funcName} {Strings.Exception}:{Environment.NewLine}{exception2}", Strings.Error); } } }