我想從kernal32.dll中使用幾個函數。然而,當我的應用程序試圖調用的第一個函數,它拋出一個EntryPointNotFoundException Unable to find an entry point named 'SetDllDirectory' in DLL 'kernel32.dll'.
C#EntryPointNotFoundException無法在DLL'kernel32.dll'中找到名爲'SetDllDirectory'的入口點
public class MyClass
{
/// <summary>
/// Use to interface to kernel32.dll for dynamic loading of similar DLLs.
/// </summary>
internal static class UnsafeNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
internal static extern bool SetDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}
private void MyFunc()
{
if (UnsafeNativeMethods.SetDllDirectory(_location) == false) // <-- Exception thrown here.
{
throw new FileNotFoundException(_location);
}
/* Some code. */
_dllHandle = UnsafeNativeMethods.LoadLibrary(_fullPath);
/* Some more code. */
_fptr = UnsafeNativeMethods.GetProcAddress(_dllHandle, _procName);
/* Yet even more code. */
}
}
任何思考什麼我做錯了,我怎麼能得到它的工作將不勝感激。謝謝。