Created
December 10, 2013 06:46
-
-
Save masroore/7886654 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] | |
private static extern bool FreeLibrary(IntPtr hModule); | |
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)] | |
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); | |
// Load the DLL file | |
IntPtr Handle = LoadLibrary(fileName); | |
if (Handle == IntPtr.Zero) | |
{ | |
int errorCode = Marshal.GetLastWin32Error(); | |
throw new Exception(string.Format("Failed to load library (ErrorCode: {0})",errorCode)); | |
} | |
// load & invoke the desired method from DLL | |
IntPtr funcaddr = GetProcAddress(Handle,functionName); | |
YourFunctionDelegate function = Marshal.GetDelegateForFunctionPointer(funcaddr,typeof(YourFunctionDelegate )) as YourFunctionDelegate ; | |
function.Invoke(...parameters here...); | |
// When done, free the DLL | |
if(Handle != IntPtr.Zero) | |
FreeLibrary(Handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment