If you are using raw dll and not .NET assemblies, here are some handy P / Invokes for you:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)] private static extern IntPtr LoadLibrary(string lpFileName); [DllImport("kernel32.dll", CharSet=CharSet.Auto)] private static extern void SetDllDirectory(string lpPathName); [DllImport("kernel32.dll", CharSet=CharSet.Auto)] privatestatic extern int GetModuleFileName(IntPtr module, [Out] StringBuilder fileName, int size); [DllImport("kernel32.dll", CharSet=CharSet.Auto)] private static bool FreeLibrary(IntPtr module); [DllImport("kernel32.dll", CharSet=CharSet.Auto)] private IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
Please note that SetDllDirectory may need some protection because it is not available for all versions of Windows (Windows 2000, in particular, does not have it).
And when using:
SetDllDirectory(candidateFolder); IntPtr dllHandle = LoadLibrary(dllName); if (dllHandle != IntPtr.Zero) { _dllHandle = dllHandle; _location = candidateFolder; _fullPath = Path.Combine(candidateFolder, dllName); IntPtr p = GetProcAddress(_dllHandle, procName); if (p == IntPtr.Zero) throw new ArgumentException("procName"); SomeDelegateType d = (SomeDelegateType)Marshal.GetDelegateForFunctionPointer(p, typeof(SomeDelegateType)); d(); }
Otherwise, you will use assembly methods. Considering assembly level attributes or object level attributes is a good way to get more information, although if you want it is a plug-in system, you should use a plug-in system, such as the CodePlex Managed Add-ons Framework . See also this question and answer .
plinth Nov 17 '09 at 18:45 2009-11-17 18:45
source share