Skip to content

Commit

Permalink
have to remove the ExactSpelling property
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasRMiles committed Dec 7, 2014
1 parent b7c79c1 commit 2e8128e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
50 changes: 47 additions & 3 deletions c/Swicli.Library/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,51 @@ public bool UnLoad()

}

static class NativeMethods
{
public static SafeLibraryHandle LoadUnmanagedLibrary(string fileName)
{
return LoadUnmanagedLibrary(fileName, true);
}

public static SafeLibraryHandle LoadUnmanagedLibrary(string fileName, bool throwOnInvalid)
{
SafeLibraryHandle localHLibrary;
if (PrologCLR.IsLinux)
{

localHLibrary = NativeMethodsLinux.LoadLibrary(fileName);
if (!localHLibrary.IsInvalid)
{
return localHLibrary;
}
PrologCLR.ConsoleTrace("IsInvalid LoadUnmanagedLibrary " + fileName);
}
localHLibrary = NativeMethodsWindows.LoadLibrary(fileName);
if (localHLibrary.IsInvalid)
{
int hr = Marshal.GetHRForLastWin32Error();
if (throwOnInvalid) Marshal.ThrowExceptionForHR(hr);
}
return localHLibrary;
}

static public void UnLoadUnmanagedLibrary(SafeHandleZeroOrMinusOneIsInvalid _hLibrary)
{
#if USESAFELIB && false
if (_hLibrary != null && !_hLibrary.IsClosed)
{
_hLibrary.Close();
_hLibrary.UnLoad();
_hLibrary.Dispose();
_hLibrary = null;
}
#endif
}
}
static class NativeMethodsWindows
{
const string SKernel = "kernel32";
const string SKernel = "kernel32.dll";
[DllImport(SKernel, CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
public static extern SafeLibraryHandle LoadLibrary(string fileName);

Expand All @@ -94,13 +136,14 @@ static class NativeMethodsWindows
[DllImport(SKernel, CharSet = CharSet.Ansi, BestFitMapping = false, SetLastError = true)]
internal static extern IntPtr GetProcAddress(SafeLibraryHandle hModule, String procname);
}

static class NativeMethodsLinux
{
public static SafeLibraryHandle LoadLibrary(string fileName)
{
return dlopen(fileName);
}
const string SKernel = "kernel32";
const string SKernel = "libdl.so";
[DllImport(SKernel, CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
public static extern SafeLibraryHandle dlopen(string fileName);

Expand All @@ -110,8 +153,9 @@ public static SafeLibraryHandle LoadLibrary(string fileName)
public static extern bool FreeLibrary(IntPtr hModule);

// see: http://blogs.msdn.com/jmstall/archive/2007/01/06/Typesafe-GetProcAddress.aspx
[DllImport(SKernel, CharSet = CharSet.Ansi, BestFitMapping = false, SetLastError = true)]
[DllImport(SKernel, CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
internal static extern IntPtr GetProcAddress(SafeLibraryHandle hModule, String procname);

}
#endregion // Safe Handles and Native imports
#endif
Expand Down
70 changes: 17 additions & 53 deletions c/Swicli.Library/PInvokeMetaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public PInvokeMetaObject(string name)
{
if (assembName.ToLower().EndsWith(clip)) assembName = assembName.Substring(0, assembName.Length - clip.Length);
}
_hLibrary = LoadUnmanagedLibrary(_dllName, true);
//_hLibrary = NativeMethods.LoadUnmanagedLibrary(_dllName, true);
assembName = "dynlib_0" + Interlocked.Increment(ref clid_gen) + "_" + assembName;
EnsureBuilders();
}
Expand All @@ -168,44 +168,7 @@ public bool IsLibraryValid
}
}

public static SafeLibraryHandle LoadUnmanagedLibrary(string fileName)
{
return LoadUnmanagedLibrary(fileName, true);
}
public static SafeLibraryHandle LoadUnmanagedLibrary(string fileName, bool throwOnInvalid)
{
SafeLibraryHandle localHLibrary;
if (PrologCLR.IsLinux)
{
localHLibrary = NativeMethodsLinux.LoadLibrary(fileName);
if (!localHLibrary.IsInvalid)
{
return localHLibrary;
}
PrologCLR.ConsoleTrace("IsInvalid LoadUnmanagedLibrary " + fileName);
}
localHLibrary = NativeMethodsWindows.LoadLibrary(fileName);
if (localHLibrary.IsInvalid)
{
int hr = Marshal.GetHRForLastWin32Error();
if (throwOnInvalid) Marshal.ThrowExceptionForHR(hr);
}
return localHLibrary;
}

public void UnLoadUnmanagedLibrary()
{
#if USESAFELIB
if (_hLibrary != null && !_hLibrary.IsClosed)
{
_hLibrary.Close();
_hLibrary.UnLoad();
_hLibrary.Dispose();
_hLibrary = null;
}
#endif
}



private void EnsureBuilders()
{
Expand All @@ -223,8 +186,8 @@ private Delegate GetNativeDelegate(string funcName, Type delegateType)
if (_pInvokeDll != null)
return _pInvokeDll.CreateDelegate(funcName, delegateType);
IntPtr _hDLL = _hLibrary.DangerousGetHandle();
IntPtr funcPtr = ExactSpellingGetProcAddress.GetProcAddress(_hDLL, funcName);
if (funcPtr.Equals(IntPtr.Zero)) funcPtr = InExactSpellingGetProcAddress.GetProcAddress(_hDLL, funcName);
IntPtr funcPtr = NativeMethodsWindows.GetProcAddress(_hLibrary, funcName);
if (funcPtr.Equals(IntPtr.Zero)) funcPtr = NativeMethodsLinux.GetProcAddress(_hLibrary, funcName);
if (funcPtr.Equals(IntPtr.Zero)) return null;
return Marshal.GetDelegateForFunctionPointer(funcPtr, delegateType);
}
Expand Down Expand Up @@ -290,18 +253,6 @@ public MethodInfo GetInvoke(string entry_point, Type[] arg_types, Type returnTyp
return m;
}

private static class ExactSpellingGetProcAddress
{
[DllImport("kernel32", EntryPoint = "GetProcAddress", CharSet = CharSet.Ansi, ExactSpelling = true,
SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}
private static class InExactSpellingGetProcAddress
{
[DllImport("kernel32", EntryPoint = "GetProcAddress", CharSet = CharSet.Ansi, ExactSpelling = false,
SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}

#endregion

Expand Down Expand Up @@ -503,6 +454,19 @@ private static MethodBuilder DefineProxyMethod<T>(TypeBuilder tBuilder, string m

}
#endif

public void UnLoadUnmanagedLibrary()
{
#if USESAFELIB
if (_hLibrary != null && !_hLibrary.IsClosed)
{
_hLibrary.Close();
_hLibrary.UnLoad();
_hLibrary.Dispose();
_hLibrary = null;
}
#endif
}
}
// #if NET40
/*
Expand Down
Binary file modified lib/i386-win32/swicli32.dll
Binary file not shown.
Binary file modified lib/i386-win32/swicli32.lib
Binary file not shown.
Binary file modified lib/i386-win32/swicli32.pdb
Binary file not shown.
Binary file modified lib/x64-win64/swicli.dll
Binary file not shown.
Binary file modified lib/x64-win64/swicli.lib
Binary file not shown.
Binary file modified lib/x64-win64/swicli.pdb
Binary file not shown.

0 comments on commit 2e8128e

Please sign in to comment.