EntryPointNotFoundException 类

定义

因不存在项方法而导致加载类的尝试失败时引发的异常。The exception that is thrown when an attempt to load a class fails due to the absence of an entry method.

public ref class EntryPointNotFoundException : TypeLoadException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class EntryPointNotFoundException : TypeLoadException
type EntryPointNotFoundException = class
    inherit TypeLoadException
Public Class EntryPointNotFoundException
Inherits TypeLoadException
继承
EntryPointNotFoundException
属性

注解

如果公共语言运行时无法加载程序集, 则会引发异常,因为它无法标识程序集的入口点。EntryPointNotFoundExceptionAn EntryPointNotFoundException exception is thrown when the common language runtime is unable to load an assembly because it cannot identify the assembly's entry point. 在下列情况下, 可能会引发此异常:This exception can be thrown under the following conditions:

  • 公共语言运行时找不到可执行程序集中的应用程序入口Main点 (通常为方法)。The common language runtime is unable to locate an application entry point (typically a Main method) in an executable assembly. 应用程序入口点必须是不带任何static参数的全局或方法, 或者不是字符串数组的唯一参数。The application entry point must be a global or static method that has either no parameters or a string array as its only parameter. 入口点可以返回void, 或者它可以Int32返回或UInt32退出代码。The entry point can return void, or it can return an Int32 or UInt32 exit code. 应用程序程序集不能定义多个入口点。An application assembly cannot define more than one entry point.

  • 无法解析对 Windows DLL 中函数的调用, 因为找不到该函数。The call to a function in a Windows DLL cannot be resolved because the function cannot be found. 在下面的示例中, EntryPointNotFoundException引发了异常, 因为 User32 不包含名为GetMyNumber的函数。In the following example, an EntryPointNotFoundException exception is thrown because User32.dll does not include a function named 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'.
    
    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 中函数的调用。The call to a function in a Windows DLL cannot be resolved because the name used in the method call does not match a name found in the assembly. 通常, 发生这种情况DllImportAttribute.ExactSpelling的原因是: 字段隐式或true显式设置为, 被调用的方法包含一个或多个字符串参数, 同时包含 ANSI 和 Unicode 版本, 并且在方法调用中使用的名称不是对应于此 ANSI 或 Unicode 版本的名称。Frequently, this occurs because the DllImportAttribute.ExactSpelling field is either implicitly or explicitly set to true, the called method includes one or more string parameters and has both an ANSI and a Unicode version, and the name used in the method call does not correspond to the name of this ANSI or Unicode version. 下面的示例通过尝试在 User32 中调用 Windows MessageBox函数来提供说明。The following example provides an illustration by attempting to call the Windows MessageBox function in User32.dll. 由于第一个方法定义为CharSet.Unicode字符串封送指定了, 公共语言将查找MessageBoxW函数的宽字符版本, 而不是MessageBox在方法调用中使用的名称。Because the first method definition specifies CharSet.Unicode for string marshaling, the common language looks for the wide-character version of the function, MessageBoxW, instead of the name used in the method call, MessageBox. 第二个方法定义通过调用MessageBoxW而不是MessageBox函数来更正此问题。The second method definition corrects this problem by calling the MessageBoxW instead of the MessageBox function.

    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);
          }
       }
    }
    
    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
    
  • 尝试通过简单名称 (而不是修饰名称) 在动态链接库中调用函数。You are trying to call a function in a dynamic link library by its simple name rather than its decorated name. 通常, C++编译器会为 DLL 函数生成修饰名。Typically, the C++ compiler generates a decorated name for DLL functions. 例如, 下面C++的代码定义一个名为 TestDll Double的库中名为的函数。For example, the following C++ code defines a function named Double in a library named TestDll.dll.

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

    当下面的示例中的代码尝试调用函数时, 将引发EntryPointNotFoundException异常, 因为找不Double到该函数。When the code in the following example tries to call the function, an EntryPointNotFoundException exception is thrown because the Double function cannot be found.

    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()
    
    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为) 调用函数, 则函数调用将成功, 如下面的示例所示。However, if the function is called by using its decorated name (in this case, ?Double@@YAHH@Z), the function call succeeds, as the following example shows.

    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
    
    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 等实用工具查找 DLL 导出的函数的修饰名。You can find the decorated names of functions exported by a DLL by using a utility such as Dumpbin.exe.

  • 您正尝试调用托管程序集中的方法, 就像它是非托管动态链接库一样。You are attempting to call a method in a managed assembly as if it were an unmanaged dynamic link library. 若要查看此操作, 请将下面的示例编译为名为 StringUtilities 的程序集。To see this in action, compile the following example to an assembly named 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
       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 动态StringUtilities.SayGoodMorning链接库中调用方法, 就像它是非托管代码一样。Then compile and execute the following example, which attempts to call the StringUtilities.SayGoodMorning method in the StringUtilities.dll dynamic link library as if it were unmanaged code. 结果为EntryPointNotFoundException异常。The result is an 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()
    
    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()
    

    若要消除此异常, 请添加对托管程序集的引用并StringUtilities.SayGoodMorning访问方法, 就像在托管代码中访问任何其他方法一样, 如下例所示。To eliminate the exception, add a reference to the managed assembly and access the StringUtilities.SayGoodMorning method just as you would access any other method in managed code, as the following example does.

    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!
    
    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 一样。You are trying to call a method in a COM DLL as if it were a Windows DLL. 若要访问 COM DLL, 请选择 Visual Studio 中的 "添加引用" 选项以添加对该项目的引用, 然后从 " COM " 选项卡中选择类型库。To access a COM DLL, select the Add Reference option in Visual Studio to add a reference to the project, and then select the type library from the COM tab.

有关实例的初始属性值的列表EntryPointNotFoundException,请参阅EntryPointNotFoundException构造函数。For a list of initial property values for an instance of EntryPointNotFoundException, see the EntryPointNotFoundException constructors.

构造函数

EntryPointNotFoundException()

初始化 EntryPointNotFoundException 类的新实例。Initializes a new instance of the EntryPointNotFoundException class.

EntryPointNotFoundException(SerializationInfo, StreamingContext)

用序列化数据初始化 EntryPointNotFoundException 类的新实例。Initializes a new instance of the EntryPointNotFoundException class with serialized data.

EntryPointNotFoundException(String)

用指定的错误消息初始化 EntryPointNotFoundException 类的新实例。Initializes a new instance of the EntryPointNotFoundException class with a specified error message.

EntryPointNotFoundException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 EntryPointNotFoundException 类的新实例。Initializes a new instance of the EntryPointNotFoundException class with a specified error message and a reference to the inner exception that is the cause of this exception.

属性

Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(继承自 Exception)
Message

获取此异常的错误消息。Gets the error message for this exception.

(继承自 TypeLoadException)
Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(继承自 Exception)
TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(继承自 Exception)
TypeName

获取导致异常的完全限定的类型名称。Gets the fully qualified name of the type that causes the exception.

(继承自 TypeLoadException)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(继承自 Exception)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

设置具有类名、方法名称、资源 ID 和其他异常信息的 SerializationInfo 对象。Sets the SerializationInfo object with the class name, method name, resource ID, and additional exception information.

(继承自 TypeLoadException)
GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(继承自 Exception)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅