2010-09-20 27 views
2

J'essaie d'utiliser plusieurs fonctions de kernal32.dll. Cependant, lorsque ma demande essaie d'appeler la première fonction, il jette un EntryPointNotFoundException Unable to find an entry point named 'SetDllDirectory' in DLL 'kernel32.dll'.C# EntryPointNotFoundException Impossible de trouver un point d'entrée nommé 'SetDllDirectory' dans la 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. */ 
    } 
} 

Toute réflexion sur ce que je fais mal et comment je peux le faire fonctionner serait grandement apprécié. Merci.

Répondre

7

Vous devrez supprimer la propriété ExactSpelling. Le vrai nom de la fonction est SetDllDirectoryW. Je vous recommande également d'utiliser CharSet.Auto, en utilisant Ansi est une conversion avec perte qui peut causer des problèmes subtils. L'exportation n'est disponible dans aucune version de Windows antérieure à XP SP1.

0

Je ne sais pas beaucoup sur DllImport, mais sur ma machine en supprimant l'attribut ExactSpelling il suffit de le faire.