EntryPointNotFoundException 類別

定義

當試著載入類別卻因沒有進入方法而告失敗時,所擲回的例外狀況。

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
繼承
EntryPointNotFoundException
屬性

備註

EntryPointNotFoundException當 Common Language Runtime 無法載入元件時,就會擲回例外狀況,因為它無法識別元件的進入點。 此例外狀況可以在下列情況下擲回:

  • Common Language Runtime 找不到應用程式進入點, (通常是 Main 可執行檔元件中) 的方法。 應用程式進入點必須是沒有參數或字串陣列做為其唯一參數的全域 static 或方法。 進入點可以傳回 ,也可以傳回 voidInt32UInt32 結束代碼。 應用程式元件無法定義多個進入點。

  • 無法解析 Windows DLL 中函式的呼叫,因為找不到函式。 在下列範例中,會擲回例外狀況, EntryPointNotFoundException 因為User32.dll不包含名為 的函式 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'.
    
  • 無法解析 Windows DLL 中函式的呼叫,因為方法呼叫中使用的名稱與元件中找到的名稱不符。 通常,這是因為 DllImportAttribute.ExactSpelling 欄位是隱含或明確設定 true 為 ,呼叫的方法包含一或多個字串參數,而且同時具有 ANSI 和 Unicode 版本,而且方法呼叫中使用的名稱不會對應至這個 ANSI 或 Unicode 版本的名稱。 下列範例會嘗試在 User32.dll 中呼叫 Windows MessageBox 函式,以提供圖例。 因為第一個方法定義會 CharSet.Unicode 指定字串封送處理,所以通用語言會尋找函式的寬字元版本, MessageBoxW 而不是方法呼叫中使用的名稱。 MessageBox 第二個方法定義會藉由呼叫 MessageBoxW 而不是 MessageBox 函式來更正此問題。

    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
    
  • 您正嘗試使用動態連結程式庫中的簡單名稱呼叫函式,而不是其裝飾名稱。 一般而言,C++ 編譯器會產生 DLL 函式的裝飾名稱。 例如,下列 C++ 程式碼會在名為 TestDll.dll 的程式庫中定義名為 Double 的函式。

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

    當下列範例中的程式碼嘗試呼叫函式時,會擲回例外狀況, EntryPointNotFoundException 因為 Double 找不到函式。

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

    不過,如果使用函式的裝飾名稱 (在此案例中呼叫, ?Double@@YAHH@Z) ,則函式呼叫會成功,如下列範例所示。

    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
    

    您可以使用 Dumpbin.exe 之類的公用程式,找到 DLL 所匯出之函式的裝飾名稱。

  • 您嘗試在 Managed 元件中呼叫方法,就像它是非受控動態連結程式庫一樣。 若要查看此動作,請將下列範例編譯為名為 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
    

    然後編譯並執行下列範例,它會嘗試呼叫 StringUtilities.SayGoodMorning StringUtilities.dll 動態連結程式庫中的 方法,就像非受控程式碼一樣。 結果是 EntryPointNotFoundException 例外狀況。

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

    若要消除例外狀況,請新增 Managed 元件的參考,並存取 方法,就像您在 Managed 程式碼中存取 StringUtilities.SayGoodMorning 任何其他方法一樣,如下列範例所示。

    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!
    
  • 您嘗試在 COM DLL 中呼叫方法,就像是 Windows DLL 一樣。 若要存取 COM DLL,請選取 Visual Studio 中的 [ 新增參考 ] 選項以新增專案的參考,然後從 [COM ] 索引標籤中選取型別程式庫。

如需執行個體的初始屬性值的清單EntryPointNotFoundException,請參閱EntryPointNotFoundException建構函式。

建構函式

EntryPointNotFoundException()

初始化 EntryPointNotFoundException 類別的新執行個體。

EntryPointNotFoundException(SerializationInfo, StreamingContext)
已淘汰.

使用序列化資料,初始化 EntryPointNotFoundException 類別的新執行個體。

EntryPointNotFoundException(String)

使用指定的錯誤訊息,初始化 EntryPointNotFoundException 類別的新執行個體。

EntryPointNotFoundException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 EntryPointNotFoundException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得這個例外狀況的錯誤訊息。

(繼承來源 TypeLoadException)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)
TypeName

取得造成例外狀況的類型完整名稱。

(繼承來源 TypeLoadException)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

以類別名稱、方法名稱、資源識別碼和其他例外狀況資訊設定 SerializationInfo 物件。

(繼承來源 TypeLoadException)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱