/// <summary> /// References the native hunspell DLL. /// </summary> /// <exception cref="System.DllNotFoundException"></exception> /// <exception cref="System.NotSupportedException"></exception> internal static void ReferenceNativeHunspellDll() { lock (nativeDllReferenceCountLock) { if (nativeDllReferenceCount == 0) { if (dllHandle != IntPtr.Zero) { throw new InvalidOperationException("Native Dll handle is not Zero"); } try { // Initialze the dynamic marshall Infrastructure to call the 32Bit (x86) or the 64Bit (x64) Dll respectively var info = new NativeMethods.SYSTEM_INFO(); NativeMethods.GetSystemInfo(ref info); // Load the correct DLL according to the processor architecture switch (info.wProcessorArchitecture) { case NativeMethods.PROCESSOR_ARCHITECTURE.Intel: string pathx86 = NativeDLLPath; if (!String.IsNullOrEmpty(pathx86) && !pathx86.EndsWith("\\")) { pathx86 += "\\"; } pathx86 += Hunspell.HunspellX86DllName; dllHandle = NativeMethods.LoadLibrary(pathx86); if (dllHandle == IntPtr.Zero) { throw new DllNotFoundException(string.Format(Hunspell.HunspellX86DllNotFoundMessage, pathx86)); } break; case NativeMethods.PROCESSOR_ARCHITECTURE.Amd64: string pathx64 = NativeDLLPath; if (!String.IsNullOrEmpty(pathx64) && !pathx64.EndsWith("\\")) { pathx64 += "\\"; } pathx64 += Hunspell.HunspellX64DllName; dllHandle = NativeMethods.LoadLibrary(pathx64); if (dllHandle == IntPtr.Zero) { throw new DllNotFoundException(string.Format(Hunspell.HunspellX64DllNotFoundMessage, pathx64)); } break; default: throw new NotSupportedException(Hunspell.HunspellNotAvailabeForProcessorArchitectureMessage + info.wProcessorArchitecture); } HunspellInit = (HunspellInitDelegate)GetDelegate("HunspellInit", typeof(HunspellInitDelegate)); HunspellFree = (HunspellFreeDelegate)GetDelegate("HunspellFree", typeof(HunspellFreeDelegate)); HunspellAdd = (HunspellAddDelegate)GetDelegate("HunspellAdd", typeof(HunspellAddDelegate)); HunspellAddWithAffix = (HunspellAddWithAffixDelegate)GetDelegate("HunspellAddWithAffix", typeof(HunspellAddWithAffixDelegate)); HunspellRemove = (HunspellRemoveDelegate)GetDelegate("HunspellRemove", typeof(HunspellRemoveDelegate)); HunspellSpell = (HunspellSpellDelegate)GetDelegate("HunspellSpell", typeof(HunspellSpellDelegate)); HunspellSuggest = (HunspellSuggestDelegate)GetDelegate("HunspellSuggest", typeof(HunspellSuggestDelegate)); HunspellAnalyze = (HunspellAnalyzeDelegate)GetDelegate("HunspellAnalyze", typeof(HunspellAnalyzeDelegate)); HunspellStem = (HunspellStemDelegate)GetDelegate("HunspellStem", typeof(HunspellStemDelegate)); HunspellGenerate = (HunspellGenerateDelegate)GetDelegate("HunspellGenerate", typeof(HunspellGenerateDelegate)); HyphenInit = (HyphenInitDelegate)GetDelegate("HyphenInit", typeof(HyphenInitDelegate)); HyphenFree = (HyphenFreeDelegate)GetDelegate("HyphenFree", typeof(HyphenFreeDelegate)); HyphenHyphenate = (HyphenHyphenateDelegate)GetDelegate("HyphenHyphenate", typeof(HyphenHyphenateDelegate)); } catch { if (dllHandle != IntPtr.Zero) { NativeMethods.FreeLibrary(dllHandle); dllHandle = IntPtr.Zero; } throw; } } ++nativeDllReferenceCount; } }
/// <summary> /// References the native hunspell DLL. /// </summary> /// <exception cref="System.DllNotFoundException"></exception> /// <exception cref="System.NotSupportedException"></exception> internal static void ReferenceNativeHunspellDll() { lock (nativeDllReferenceCountLock) { if (nativeDllReferenceCount == 0) { bool success = false; if (dllHandle != IntPtr.Zero) { throw new InvalidOperationException("Native Dll handle is not Zero"); } try { string dllName; string dllPath; if (Environment.OSVersion.Platform == PlatformID.Win32NT) { dllLoadUtils = new DllLoadUtilsWindows(); if (IntPtr.Size == 4) { dllName = Resources.HunspellX86DllName; } else { dllName = Resources.HunspellX64DllName; } } else { dllLoadUtils = new DllLoadUtilsUnix(); dllName = "libhunspell.dylib"; } dllPath = Path.Combine(NativeDLLPath, dllName); dllHandle = dllLoadUtils.LoadLibrary(Path.Combine(NativeDLLPath, dllPath)); if (dllHandle == IntPtr.Zero) { if (IntPtr.Size == 4) { throw new DllNotFoundException(string.Format(Resources.HunspellX86DllNotFoundMessage, dllPath)); } else { throw new DllNotFoundException(string.Format(Resources.HunspellX64DllNotFoundMessage, dllPath)); } } HunspellInit = (HunspellInitDelegate)GetDelegate("HunspellInit", typeof(HunspellInitDelegate)); HunspellFree = (HunspellFreeDelegate)GetDelegate("HunspellFree", typeof(HunspellFreeDelegate)); HunspellAdd = (HunspellAddDelegate)GetDelegate("HunspellAdd", typeof(HunspellAddDelegate)); HunspellAddWithAffix = (HunspellAddWithAffixDelegate)GetDelegate("HunspellAddWithAffix", typeof(HunspellAddWithAffixDelegate)); HunspellRemove = (HunspellRemoveDelegate)GetDelegate("HunspellRemove", typeof(HunspellRemoveDelegate)); HunspellSpell = (HunspellSpellDelegate)GetDelegate("HunspellSpell", typeof(HunspellSpellDelegate)); HunspellSuggest = (HunspellSuggestDelegate)GetDelegate("HunspellSuggest", typeof(HunspellSuggestDelegate)); HunspellAnalyze = (HunspellAnalyzeDelegate)GetDelegate("HunspellAnalyze", typeof(HunspellAnalyzeDelegate)); HunspellStem = (HunspellStemDelegate)GetDelegate("HunspellStem", typeof(HunspellStemDelegate)); HunspellGenerate = (HunspellGenerateDelegate)GetDelegate("HunspellGenerate", typeof(HunspellGenerateDelegate)); HyphenInit = (HyphenInitDelegate)GetDelegate("HyphenInit", typeof(HyphenInitDelegate)); HyphenFree = (HyphenFreeDelegate)GetDelegate("HyphenFree", typeof(HyphenFreeDelegate)); HyphenHyphenate = (HyphenHyphenateDelegate)GetDelegate("HyphenHyphenate", typeof(HyphenHyphenateDelegate)); success = true; } finally { if (dllHandle != IntPtr.Zero && !success) { dllLoadUtils.FreeLibrary(dllHandle); dllHandle = IntPtr.Zero; } } } ++nativeDllReferenceCount; } }
/// <summary> /// The load native hunspell dll. /// </summary> /// <exception cref="DllNotFoundException"> /// </exception> /// <exception cref="NotSupportedException"> /// </exception> internal static void LoadNativeHunspellDll() { if (dllHandle != IntPtr.Zero) { return; // Already loaded } try { // Initialze the dynamic marshall Infrastructure to call the 32Bit (x86) or the 64Bit (x64) Dll respectively var info = new SYSTEM_INFO(); GetSystemInfo(ref info); // Load the correct DLL according to the processor architecture switch (info.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE.Intel: string pathx86 = NativeDLLPath; if (pathx86 != string.Empty && !pathx86.EndsWith("\\")) { pathx86 += "\\"; } pathx86 += Resources.HunspellX86DllName; if (!File.Exists(pathx86)) { pathx86 = AssemblyPath; if (pathx86 != string.Empty && !pathx86.EndsWith("\\")) { pathx86 += "\\"; } pathx86 += Resources.HunspellX86DllName; } dllHandle = LoadLibrary(pathx86); if (dllHandle == IntPtr.Zero) { throw new DllNotFoundException( string.Format(Resources.HunspellX86DllNotFoundMessage, pathx86)); } break; case PROCESSOR_ARCHITECTURE.Amd64: string pathx64 = NativeDLLPath; if (pathx64 != string.Empty && !pathx64.EndsWith("\\")) { pathx64 += "\\"; } pathx64 += Resources.HunspellX64DllName; if (!File.Exists(pathx64)) { pathx64 = AssemblyPath; if (pathx64 != string.Empty && !pathx64.EndsWith("\\")) { pathx64 += "\\"; } pathx64 += Resources.HunspellX64DllName; } dllHandle = LoadLibrary(pathx64); if (dllHandle == IntPtr.Zero) { throw new DllNotFoundException( string.Format(Resources.HunspellX64DllNotFoundMessage, pathx64)); } break; default: throw new NotSupportedException( Resources.HunspellNotAvailabeForProcessorArchitectureMessage + info.wProcessorArchitecture); } HunspellInit = (HunspellInitDelegate)GetDelegate("HunspellInit", typeof(HunspellInitDelegate)); HunspellFree = (HunspellFreeDelegate)GetDelegate("HunspellFree", typeof(HunspellFreeDelegate)); HunspellAdd = (HunspellAddDelegate)GetDelegate("HunspellAdd", typeof(HunspellAddDelegate)); HunspellAddWithAffix = (HunspellAddWithAffixDelegate) GetDelegate("HunspellAddWithAffix", typeof(HunspellAddWithAffixDelegate)); HunspellSpell = (HunspellSpellDelegate)GetDelegate("HunspellSpell", typeof(HunspellSpellDelegate)); HunspellSuggest = (HunspellSuggestDelegate)GetDelegate("HunspellSuggest", typeof(HunspellSuggestDelegate)); HunspellAnalyze = (HunspellAnalyzeDelegate)GetDelegate("HunspellAnalyze", typeof(HunspellAnalyzeDelegate)); HunspellStem = (HunspellStemDelegate)GetDelegate("HunspellStem", typeof(HunspellStemDelegate)); HunspellGenerate = (HunspellGenerateDelegate)GetDelegate("HunspellGenerate", typeof(HunspellGenerateDelegate)); HyphenInit = (HyphenInitDelegate)GetDelegate("HyphenInit", typeof(HyphenInitDelegate)); HyphenFree = (HyphenFreeDelegate)GetDelegate("HyphenFree", typeof(HyphenFreeDelegate)); HyphenHyphenate = (HyphenHyphenateDelegate)GetDelegate("HyphenHyphenate", typeof(HyphenHyphenateDelegate)); } catch { if (dllHandle != IntPtr.Zero) { FreeLibrary(dllHandle); } throw; } }