EntryPointNotFoundException Klass

Definition

Undantaget som utlöses när ett försök att läsa in en klass misslyckas på grund av att det inte finns någon inmatningsmetod.

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
Arv
EntryPointNotFoundException
Attribut

Kommentarer

Ett EntryPointNotFoundException undantag utlöses när den vanliga språkkörningen inte kan läsa in en sammansättning eftersom den inte kan identifiera sammansättningens startpunkt. Det här undantaget kan utlöses under följande villkor:

  • Den vanliga språkkörningen kan inte hitta en startpunkt för programmet (vanligtvis en Main metod) i en körbar sammansättning. Programmets startpunkt måste vara en global static eller metod som antingen inte har några parametrar eller en strängmatris som enda parameter. Startpunkten kan returnera void, eller returnera en Int32 eller UInt32 avsluta kod. En programsammansättning kan inte definiera mer än en startpunkt.

  • Det går inte att matcha anropet till en funktion i en Windows DLL eftersom det inte går att hitta funktionen. I följande exempel genereras ett EntryPointNotFoundException undantag eftersom User32.dll inte innehåller en funktion med namnet 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'.
    
  • Det går inte att matcha anropet till en funktion i en Windows DLL eftersom namnet som används i metodanropet inte matchar ett namn som finns i sammansättningen. Detta beror ofta på att DllImportAttribute.ExactSpelling fältet antingen är implicit eller explicit inställt på true, den anropade metoden innehåller en eller flera strängparametrar och har både en ANSI- och Unicode-version, och namnet som används i metodanropet motsvarar inte namnet på denna ANSI- eller Unicode-version. I följande exempel visas en bild genom att försöka anropa funktionen Windows MessageBox i User32.dll. Eftersom den första metoddefinitionen CharSet.Unicode anger för strängmarsering letar det gemensamma språket efter funktionens wide-character-version, MessageBoxW, i stället för namnet som används i metodanropet, MessageBox. Den andra metoddefinitionen korrigerar det här problemet genom att anropa MessageBoxW i stället för MessageBox funktionen.

    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
    
  • Du försöker anropa en funktion i ett dynamiskt länkbibliotek med dess enkla namn i stället för dess dekorerade namn. Vanligtvis genererar C++-kompilatorn ett dekorerat namn för DLL-funktioner. Följande C++-kod definierar till exempel en funktion med namnet Double i ett bibliotek med namnet TestDll.dll.

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

    När koden i följande exempel försöker anropa funktionen utlöses ett EntryPointNotFoundException undantag eftersom funktionen Double inte kan hittas.

    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()
    

    Men om funktionen anropas med hjälp av dess dekorerade namn (i det här fallet ?Double@@YAHH@Z), lyckas funktionsanropet, vilket visas i följande exempel.

    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
    

    Du hittar de dekorerade namnen på funktioner som exporteras av en DLL med hjälp av ett verktyg som Dumpbin.exe.

  • Du försöker anropa en metod i en hanterad sammansättning som om det vore ett ohanterat dynamiskt länkbibliotek. Om du vill se detta i praktiken kompilerar du följande exempel till en sammansättning med namnet 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
    

    Kompilera och kör sedan följande exempel, som försöker anropa StringUtilities.SayGoodMorning metoden i StringUtilities.dll dynamiska länkbiblioteket som om det vore ohanterad kod. Resultatet är ett EntryPointNotFoundException undantag.

    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()
    

    Om du vill eliminera undantaget lägger du till en referens till den hanterade sammansättningen och kommer åt StringUtilities.SayGoodMorning metoden precis som du skulle komma åt någon annan metod i hanterad kod, som i följande exempel.

    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!
    
  • Du försöker anropa en metod i en COM DLL som om det vore en Windows DLL. Om du vill komma åt en COM-DLL väljer du alternativet Lägg till referens i Visual Studio för att lägga till en referens till projektet och väljer sedan typbiblioteket från fliken COM.

För en lista över inledande egenskapsvärden för en instans av EntryPointNotFoundException, se i EntryPointNotFoundException-konstruktorn.

Konstruktorer

Name Description
EntryPointNotFoundException()

Initierar en ny instans av EntryPointNotFoundException klassen.

EntryPointNotFoundException(SerializationInfo, StreamingContext)
Föråldrad.

Initierar en ny instans av EntryPointNotFoundException klassen med serialiserade data.

EntryPointNotFoundException(String, Exception)

Initierar en ny instans av EntryPointNotFoundException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget.

EntryPointNotFoundException(String)

Initierar en ny instans av EntryPointNotFoundException klassen med ett angivet felmeddelande.

Egenskaper

Name Description
Data

Hämtar en samling nyckel/värde-par som ger ytterligare användardefinierad information om undantaget.

(Ärvd från Exception)
HelpLink

Hämtar eller anger en länk till hjälpfilen som är associerad med det här undantaget.

(Ärvd från Exception)
HResult

Hämtar eller anger HRESULT, ett kodat numeriskt värde som har tilldelats ett specifikt undantag.

(Ärvd från Exception)
InnerException

Hämtar den Exception instans som orsakade det aktuella undantaget.

(Ärvd från Exception)
Message

Hämtar felmeddelandet för det här undantaget.

(Ärvd från TypeLoadException)
Source

Hämtar eller anger namnet på programmet eller objektet som orsakar felet.

(Ärvd från Exception)
StackTrace

Hämtar en strängrepresentation av de omedelbara ramarna i anropsstacken.

(Ärvd från Exception)
TargetSite

Hämtar den metod som utlöser det aktuella undantaget.

(Ärvd från Exception)
TypeName

Hämtar det fullständigt kvalificerade namnet på den typ som orsakar undantaget.

(Ärvd från TypeLoadException)

Metoder

Name Description
Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetBaseException()

När den åsidosätts i en härledd klass returnerar den Exception som är rotorsaken till ett eller flera efterföljande undantag.

(Ärvd från Exception)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetObjectData(SerializationInfo, StreamingContext)
Föråldrad.

SerializationInfo Anger objektet med klassnamn, metodnamn, resurs-ID och ytterligare undantagsinformation.

(Ärvd från TypeLoadException)
GetType()

Hämtar körningstypen för den aktuella instansen.

(Ärvd från Exception)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
ToString()

Skapar och returnerar en strängrepresentation av det aktuella undantaget.

(Ärvd från Exception)

Händelser

Name Description
SerializeObjectState
Föråldrad.

Inträffar när ett undantag serialiseras för att skapa ett undantagstillståndsobjekt som innehåller serialiserade data om undantaget.

(Ärvd från Exception)

Gäller för

Se även