繁体   English   中英

C#EntryPointNotFoundException无法在DLL'kernel32.dll'中找到名为'SetDllDirectory'的入口点

[英]C# EntryPointNotFoundException Unable to find an entry point named 'SetDllDirectory' in DLL 'kernel32.dll'

我正在尝试使用kernal32.dll中的几个函数。 但是,当我的应用程序尝试调用第一个函数时,它会抛出一个EntryPointNotFoundException Unable to find an entry point named 'SetDllDirectory' in DLL 'kernel32.dll'.

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. */
    }
}

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

您必须删除ExactSpelling属性。 该函数的真实名称是SetDllDirectoryW。 我还建议你使用CharSet.Auto,使用Ansi是一个有损转换,可能会导致细微的问题。 XP SP1之前的任何Windows版本都不提供导出功能。

我对DllImport了解不多,但在我的机器上删除ExactSpelling属性就行了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM