C# EntryPointNotFoundException: 找不到名为 'SetDllDirectory' 的 DLL 'kernel32.dll' 中的入口点。

4

我正在尝试使用kernal32.dll中的一些函数。然而,当我的应用程序尝试调用第一个函数时,它会抛出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. */
    }
}

任何关于我做错了什么以及如何让它正常工作的想法都将不胜感激。谢谢。
2个回答

8

您需要删除ExactSpelling属性。函数的真实名称是SetDllDirectoryW。我还建议您使用CharSet.Auto,使用Ansi会导致细微的问题且有损转换。该导出在XP SP1之前的任何Windows版本中都不可用。


1

我对DllImport不是很了解,但是在我的机器上移除ExactSpelling属性就可以了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接