EntryPointNotFoundException Classe

Définition

Exception levée lorsqu’une tentative de chargement d’une classe échoue en raison de l’absence d’une méthode d’entrée.

public ref class EntryPointNotFoundException : TypeLoadException
public class EntryPointNotFoundException : TypeLoadException
[System.Serializable]
public class EntryPointNotFoundException : TypeLoadException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class EntryPointNotFoundException : TypeLoadException
type EntryPointNotFoundException = class
    inherit TypeLoadException
[<System.Serializable>]
type EntryPointNotFoundException = class
    inherit TypeLoadException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type EntryPointNotFoundException = class
    inherit TypeLoadException
Public Class EntryPointNotFoundException
Inherits TypeLoadException
Héritage
EntryPointNotFoundException
Attributs

Remarques

Une EntryPointNotFoundException exception est levée lorsque le Common Language Runtime ne peut pas charger un assembly, car il ne peut pas identifier le point d’entrée de l’assembly. Cette exception peut être levée dans les conditions suivantes :

  • Le Common Language Runtime ne peut pas localiser un point d’entrée d’application (généralement une Main méthode) dans un assembly exécutable. Le point d’entrée de l’application doit être un global ou static une méthode qui n’a aucun paramètre ou tableau de chaînes comme seul paramètre. Le point d’entrée peut retourner voidou retourner un code de Int32UInt32 sortie. Un assembly d’application ne peut pas définir plusieurs points d’entrée.

  • L’appel à une fonction dans une DLL Windows ne peut pas être résolu, car la fonction est introuvable. Dans l’exemple suivant, une EntryPointNotFoundException exception est levée, car User32.dll n’inclut pas de fonction nommée GetMyNumber.

    using System;
    using System.Runtime.InteropServices;
    
    public class Example
    {
       [DllImport("user32.dll")]
       public static extern int GetMyNumber();
    
       public static void Main()
       {
          try {
             int number = GetMyNumber();
          }
          catch (EntryPointNotFoundException e) {
             Console.WriteLine("{0}:\n   {1}", e.GetType().Name,
                               e.Message);
          }
       }
    }
    // The example displays the following output:
    //    EntryPointNotFoundException:
    //       Unable to find an entry point named 'GetMyNumber' in DLL 'User32.dll'.
    
    open System
    open System.Runtime.InteropServices
    
    [<DllImport "user32.dll">]
    extern int GetMyNumber()
    
    try
        let number = GetMyNumber()
        ()
    with :? EntryPointNotFoundException as e ->
        printfn $"{e.GetType().Name}:\n   {e.Message}"
    
    // The example displays the following output:
    //    EntryPointNotFoundException:
    //       Unable to find an entry point named 'GetMyNumber' in DLL 'User32.dll'.
    
    Module Example
        Declare Auto Function GetMyNumber Lib "User32.dll" () As Integer
    
       Public Sub Main()
          Try
             Dim number As Integer = GetMyNumber()
          Catch e As EntryPointNotFoundException
             Console.WriteLine("{0}:{2}   {1}", e.GetType().Name,  
                               e.Message, vbCrLf)
          End Try   
       End Sub
    End Module
    ' The example displays the following output:
    '    EntryPointNotFoundException:
    '       Unable to find an entry point named 'GetMyNumber' in DLL 'User32.dll'.
    
  • L’appel à une fonction dans une DLL Windows ne peut pas être résolu, car le nom utilisé dans l’appel de méthode ne correspond pas à un nom trouvé dans l’assembly. Cela se produit fréquemment, car le DllImportAttribute.ExactSpelling champ est implicitement ou explicitement défini truesur , la méthode appelée inclut un ou plusieurs paramètres de chaîne et a à la fois une version ANSI et une version Unicode, et le nom utilisé dans l’appel de méthode ne correspond pas au nom de cette version ANSI ou Unicode. L’exemple suivant fournit une illustration en tentant d’appeler la fonction Windows MessageBox dans User32.dll. Étant donné que la première définition de méthode spécifie CharSet.Unicode le marshaling de chaînes, le langage commun recherche la version large de la fonction, MessageBoxWau lieu du nom utilisé dans l’appel de méthode. MessageBox La deuxième définition de méthode corrige ce problème en appelant la MessageBoxW fonction au lieu de la MessageBox fonction.

    using System;
    using System.Runtime.InteropServices;
    
    public class Example
    {
       [DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )]
       public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
    
       [DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )]
       public static extern int MessageBoxW(IntPtr hwnd, String text, String caption, uint type);
    
       public static void Main()
       {
          try {
             MessageBox(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
          }
          catch (EntryPointNotFoundException e) {
             Console.WriteLine("{0}:\n   {1}", e.GetType().Name,
                               e.Message);
          }
    
          try {
             MessageBoxW(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
          }
          catch (EntryPointNotFoundException e) {
             Console.WriteLine("{0}:\n   {1}", e.GetType().Name,
                               e.Message);
          }
       }
    }
    
    open System
    open System.Runtime.InteropServices
    
    [<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
    extern int MessageBox(IntPtr hwnd, String text, String caption, uint ``type``)
    
    [<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
    extern int MessageBoxW(IntPtr hwnd, String text, String caption, uint ``type``)
    
    try
        MessageBox(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
        |> ignore
    with :? EntryPointNotFoundException as e ->
        printfn $"{e.GetType().Name}:\n   {e.Message}"
    
    try
        MessageBoxW(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
        |> ignore
    with :? EntryPointNotFoundException as e ->
        printfn $"{e.GetType().Name}:\n   {e.Message}"
    
    Module Example
       Declare Unicode Function MessageBox Lib "User32.dll" Alias "MessageBox" (
          ByVal hWnd As IntPtr, ByVal txt As String, ByVal caption As String, 
          ByVal typ As UInteger) As Integer  
    
       Declare Unicode Function MessageBox2 Lib "User32.dll" Alias "MessageBoxW" (  
          ByVal hWnd As IntPtr, ByVal txt As String, ByVal caption As String, 
          ByVal typ As UInteger) As Integer  
          
       Public Sub Main()
          Try
             MessageBox(IntPtr.Zero, "Calling the MessageBox Function", "Example", 0 )
          Catch e As EntryPointNotFoundException
             Console.WriteLine("{0}:{2}   {1}", e.GetType().Name,  
                               e.Message, vbCrLf)
          End Try
    
          Try
             MessageBox2(IntPtr.Zero, "Calling the MessageBox Function", "Example", 0 )
          Catch e As EntryPointNotFoundException
             Console.WriteLine("{0}:{2}   {1}", e.GetType().Name,  
                               e.Message, vbCrLf)
          End Try
    
       End Sub
    End Module
    
  • Vous essayez d’appeler une fonction dans une bibliothèque de liens dynamiques par son nom simple plutôt que son nom décoré. En règle générale, le compilateur C++ génère un nom décoré pour les fonctions DLL. Par exemple, le code C++ suivant définit une fonction nommée Double dans une bibliothèque nommée TestDll.dll.

    __declspec(dllexport) int Double(int number)
    {
        return number * 2;
    }
    

    Lorsque le code de l’exemple suivant tente d’appeler la fonction, une EntryPointNotFoundException exception est levée, car la Double fonction est introuvable.

    using System;
    using System.Runtime.InteropServices;
    
    public class Example
    {
       [DllImport("TestDll.dll")]
       public static extern int Double(int number);
    
       public static void Main()
       {
          Console.WriteLine(Double(10));
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.EntryPointNotFoundException: Unable to find
    //    an entry point named 'Double' in DLL '.\TestDll.dll'.
    //       at Example.Double(Int32 number)
    //       at Example.Main()
    
    open System
    open System.Runtime.InteropServices
    
    [<DllImport "TestDll.dll">]
    extern int Double(int number)
    
    printfn $"{Double 10}"
    // The example displays the following output:
    //    Unhandled Exception: System.EntryPointNotFoundException: Unable to find
    //    an entry point named 'Double' in DLL '.\TestDll.dll'.
    //       at Example.Double(Int32 number)
    //       at Example.Main()
    
    Module Example
       Public Declare Function DoubleNum Lib ".\TestDll.dll" Alias "Double" _
                      (ByVal number As Integer) As Integer
       
       Public Sub Main()
          Console.WriteLine(DoubleNum(10))
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.EntryPointNotFoundException: Unable to find an 
    '    entry point named 'Double' in DLL '.\TestDll.dll'.
    '       at Example.Double(Int32 number)
    '       at Example.Main()
    

    Toutefois, si la fonction est appelée à l’aide de son nom décoré (dans ce cas, ?Double@@YAHH@Z), l’appel de fonction réussit, comme l’illustre l’exemple suivant.

    using System;
    using System.Runtime.InteropServices;
    
    public class Example
    {
       [DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")]
       public static extern int Double(int number);
    
       public static void Main()
       {
          Console.WriteLine(Double(10));
       }
    }
    // The example displays the following output:
    //    20
    
    open System
    open System.Runtime.InteropServices
    
    [<DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")>]
    extern int Double(int number)
    
    printfn $"{Double 10}"
    // The example displays the following output:
    //    20
    
    Module Example
       Public Declare Function DoubleNum Lib ".\TestDll.dll" Alias "?Double@@YAHH@Z" _
                      (ByVal number As Integer) As Integer
       
       Public Sub Main()
          Console.WriteLine(DoubleNum(10))
       End Sub
    End Module
    ' The example displays the following output:
    '    20
    

    Vous trouverez les noms décorés des fonctions exportées par une DLL à l’aide d’un utilitaire tel que Dumpbin.exe.

  • Vous tentez d’appeler une méthode dans un assembly managé comme s’il s’agissait d’une bibliothèque de liens dynamiques non managée. Pour voir cela en action, compilez l’exemple suivant dans un assembly nommé StringUtilities.dll.

    using System;
    
    public static class StringUtilities
    {
       public static String SayGoodMorning(String name)
       {
          return String.Format("A top of the morning to you, {0}!", name);
       }
    }
    
    module StringUtilities
    
    let SayGoodMorning name =
        $"A top of the morning to you, %s{name}!"
    
    Module StringUtilities
       Public Function SayGoodMorning(name As String) As String
          Return String.Format("A top of the morning to you, {0}!", name)
       End Function
    End Module
    

    Compilez et exécutez ensuite l’exemple suivant, qui tente d’appeler la StringUtilities.SayGoodMorning méthode dans la bibliothèque de liens dynamiques StringUtilities.dll comme s’il s’agissait d’un code non managé. Le résultat est une EntryPointNotFoundException exception.

    using System;
    using System.Runtime.InteropServices;
    
    public class Example
    {
       [DllImport("StringUtilities.dll", CharSet = CharSet.Unicode )]
       public static extern String SayGoodMorning(String name);
    
       public static void Main()
       {
          Console.WriteLine(SayGoodMorning("Dakota"));
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point
    //    named 'GoodMorning' in DLL 'StringUtilities.dll'.
    //       at Example.GoodMorning(String& name)
    //       at Example.Main()
    
    open System
    open System.Runtime.InteropServices
    
    [<DllImport("StringUtilities.dll", CharSet = CharSet.Unicode )>]
    extern String SayGoodMorning(String name)
    
    printfn $"""{SayGoodMorning "Dakota"}"""
    // The example displays the following output:
    //    Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point
    //    named 'GoodMorning' in DLL 'StringUtilities.dll'.
    //       at Example.GoodMorning(String& name)
    //       at Example.Main()
    
    Module Example
       Declare Unicode Function GoodMorning Lib "StringUtilities.dll" (
          ByVal name As String) As String  
    
       Public Sub Main()
          Console.WriteLine(SayGoodMorning("Dakota"))
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point 
    '    named 'GoodMorning' in DLL 'StringUtilities.dll'.
    '       at Example.GoodMorning(String& name)
    '       at Example.Main()
    

    Pour éliminer l’exception, ajoutez une référence à l’assembly managé et accédez à la StringUtilities.SayGoodMorning méthode comme vous le feriez pour accéder à n’importe quelle autre méthode dans le code managé, comme l’illustre l’exemple suivant.

    using System;
    
    public class Example
    {
       public static void Main()
       {
           Console.WriteLine(StringUtilities.SayGoodMorning("Dakota"));
       }
    }
    // The example displays the following output:
    //        A top of the morning to you, Dakota!
    
    printfn $"""{StringUtilities.SayGoodMorning "Dakota"}"""
    // The example displays the following output:
    //        A top of the morning to you, Dakota!
    
    Module Example
       Public Sub Main()
          Console.WriteLine(StringUtilities.SayGoodMorning("Dakota"))
       End Sub
    End Module
    ' The example displays the following output:
    '       A top of the morning to you, Dakota!
    
  • Vous essayez d’appeler une méthode dans une DLL COM comme s’il s’agissait d’une DLL Windows. Pour accéder à une DLL COM, sélectionnez l’option Add Reference dans Visual Studio pour ajouter une référence au projet, puis sélectionnez la bibliothèque de types sous l’onglet COM.

Pour obtenir la liste des valeurs de propriété initiales d’une instance de EntryPointNotFoundException, consultez les EntryPointNotFoundException constructeurs.

Constructeurs

Nom Description
EntryPointNotFoundException()

Initialise une nouvelle instance de la classe EntryPointNotFoundException.

EntryPointNotFoundException(SerializationInfo, StreamingContext)
Obsolète.

Initialise une nouvelle instance de la classe EntryPointNotFoundException avec des données sérialisées.

EntryPointNotFoundException(String, Exception)

Initialise une nouvelle instance de la EntryPointNotFoundException classe avec un message d’erreur spécifié et une référence à l’exception interne qui est la cause de cette exception.

EntryPointNotFoundException(String)

Initialise une nouvelle instance de la EntryPointNotFoundException classe avec un message d’erreur spécifié.

Propriétés

Nom Description
Data

Obtient une collection de paires clé/valeur qui fournissent des informations supplémentaires définies par l’utilisateur sur l’exception.

(Hérité de Exception)
HelpLink

Obtient ou définit un lien vers le fichier d’aide associé à cette exception.

(Hérité de Exception)
HResult

Obtient ou définit HRESULT, valeur numérique codée affectée à une exception spécifique.

(Hérité de Exception)
InnerException

Obtient l’instance Exception qui a provoqué l’exception actuelle.

(Hérité de Exception)
Message

Obtient le message d’erreur de cette exception.

(Hérité de TypeLoadException)
Source

Obtient ou définit le nom de l’application ou de l’objet qui provoque l’erreur.

(Hérité de Exception)
StackTrace

Obtient une représentation sous forme de chaîne des images immédiates sur la pile des appels.

(Hérité de Exception)
TargetSite

Obtient la méthode qui lève l’exception actuelle.

(Hérité de Exception)
TypeName

Obtient le nom complet du type qui provoque l’exception.

(Hérité de TypeLoadException)

Méthodes

Nom Description
Equals(Object)

Détermine si l’objet spécifié est égal à l’objet actuel.

(Hérité de Object)
GetBaseException()

En cas de substitution dans une classe dérivée, retourne la Exception cause racine d’une ou plusieurs exceptions ultérieures.

(Hérité de Exception)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetObjectData(SerializationInfo, StreamingContext)
Obsolète.

Définit l’objet SerializationInfo avec le nom de classe, le nom de méthode, l’ID de ressource et des informations d’exception supplémentaires.

(Hérité de TypeLoadException)
GetType()

Obtient le type d’exécution de l’instance actuelle.

(Hérité de Exception)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
ToString()

Crée et retourne une représentation sous forme de chaîne de l’exception actuelle.

(Hérité de Exception)

Événements

Nom Description
SerializeObjectState
Obsolète.

Se produit lorsqu’une exception est sérialisée pour créer un objet d’état d’exception qui contient des données sérialisées sur l’exception.

(Hérité de Exception)

S’applique à

Voir aussi