首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# EntryPointNotFoundException在DLL 'kernel32.dll‘中找不到名为'SetDllDirectory’的入口点

C# EntryPointNotFoundException在DLL 'kernel32.dll‘中找不到名为'SetDllDirectory’的入口点
EN

Stack Overflow用户
提问于 2010-09-21 00:08:32
回答 2查看 6.1K关注 0票数 3

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

代码语言:javascript
复制
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. */
    }
}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-21 00:12:42

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

票数 8
EN

Stack Overflow用户

发布于 2010-09-21 00:16:55

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3753306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档