3

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

私が間違っていることと、それをどのように機能させることができるかについての考えは大歓迎です。ありがとう。

4

2 に答える 2

8

ExactSpellingプロパティを削除する必要があります。関数の実際の名前はSetDllDirectoryWです。また、CharSet.Autoを使用することをお勧めします。Ansiを使用すると、損失の多い変換であり、微妙な問題が発生する可能性があります。XP SP1より前のWindowsバージョンでは、エクスポートは使用できません。

于 2010-09-20T16:12:41.833 に答える
1

についてはよくわかりませんが、私のマシンでは属性をDllImport削除するだけです。ExactSpelling

于 2010-09-20T16:16:54.790 に答える