2010-09-20 384 views
2

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

任何思考什麼我做錯了,我怎麼能得到它的工作將不勝感激。謝謝。

回答

7

您必須刪除ExactSpelling屬性。該函數的真實名稱是SetDllDirectoryW。我也建議你使用CharSet.Auto,使用Ansi是一個有損耗的轉換,可能會導致微妙的問題。在XP SP1之前的任何Windows版本中都不提供導出功能。

0

我對DllImport瞭解不多,但是在我的機器上刪除ExactSpelling屬性就行了。

相關問題