MsgBox 샘플

업데이트: 2007년 11월

이 샘플에서는 문자열 형식을 In 매개 변수를 통해 값으로 전달하는 방법과 EntryPoint, CharSetExactSpelling 필드의 사용 시기를 보여 줍니다.

MsgBox 샘플에서는 다음의 관리되지 않는 함수를 사용합니다. 이 함수는 원래의 함수 선언과 함께 표시되어 있습니다.

  • User32.dll에서 내보낸 MessageBox

    int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, 
       UINT uType);
    

이 샘플에서, LibWrap 클래스에는 MsgBoxSample 클래스에서 호출하는 각각의 관리되지 않는 함수에 대한 관리되는 프로토타입이 포함되어 있습니다. 관리되는 프로토타입 메서드인 MsgBox, MsgBox2 및 MsgBox3에서는 동일한 관리되지 않는 함수에 대한 선언이 서로 다릅니다.

MsgBox2에 대한 선언에서는 ANSI로 지정된 문자 형식이 유니코드 함수의 이름인 진입점 MessageBoxW와 일치하지 않으므로 올바르지 않은 출력이 생성됩니다. MsgBox3에 대한 선언에서는 EntryPoint, CharSetExactSpelling 필드 간에 불일치가 발생됩니다. MsgBox3을 호출하면 예외가 throw됩니다. 문자열 명명 및 이름 마샬링에 대한 자세한 내용은 문자 집합 지정을 참조하십시오.

다음 코드 예제의 소스 코드는 .NET Framework Platform Invoke 기술 샘플을 통해 제공됩니다.

프로토타입 선언

Public Class LibWrap
   ' Declares managed prototypes for unmanaged functions.
   Declare Auto Function MsgBox Lib "User32.dll" Alias "MessageBox" ( _
   ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
   yVal typ As Integer ) As Integer
   
   ' Causes incorrect output in the message window.
   Declare Ansi Function MsgBox2 Lib "User32.dll" Alias "MessageBoxW" ( _
   ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
   ByVal type As Integer ) As Integer 
   
   ' Causes an exception to be thrown.
   ' ExactSpelling is True by default in Visual Basic 2005 when 
   ' Ansi or Unicode is used.
   Declare Ansi Function MsgBox3 Lib "User32.dll" Alias "MessageBox" ( _
   ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
   ByVal typ As Integer ) As Integer
End Class 
public class LibWrap 
{
   // Declares managed prototypes for unmanaged functions.
   [ DllImport( "User32.dll", EntryPoint="MessageBox", 
      CharSet=CharSet.Auto )]
   public static extern int MsgBox( int hWnd, String text, String caption, 
      uint type );
   // Causes incorrect output in the message window.
   [ DllImport( "User32.dll", EntryPoint="MessageBoxW", 
      CharSet=CharSet.Ansi )]
   public static extern int MsgBox2( int hWnd, String text, 
      String caption, uint type );
   // Causes an exception to be thrown. EntryPoint, CharSet, and 
   // ExactSpelling fields are mismatched.
   [ DllImport( "User32.dll", EntryPoint="MessageBox", 
      CharSet=CharSet.Ansi, ExactSpelling=true )]
   public static extern int MsgBox3( int hWnd, String text, 
      String caption, uint type );
}

함수 호출

Public Class MsgBoxSample
   Public Shared Sub Main()
   
      LibWrap.MsgBox( 0, "Correct text", "MsgBox Sample", 0 )
      LibWrap.MsgBox2( 0, "Incorrect text", "MsgBox Sample", 0 )
      
      Try
         LibWrap.MsgBox3( 0, "No such function", "MsgBox Sample", 0 )
      Catch e As EntryPointNotFoundException
         Console.WriteLine( "EntryPointNotFoundException thrown _
           as expected!" )
      End Try
   End Sub 
End Class 
public class MsgBoxSample
{
   public static void Main()
   {
      LibWrap.MsgBox( 0, "Correct text", "MsgBox Sample", 0 );
      LibWrap.MsgBox2( 0, "Incorrect text", "MsgBox Sample", 0 );
      try
      {
         LibWrap.MsgBox3( 0, "No such function", "MsgBox Sample", 0 );
      }
      catch( EntryPointNotFoundException )
      {
         Console.WriteLine( "EntryPointNotFoundException thrown as 
           expected!" );
      }
   }
}

참고 항목

작업

Platform Invoke 기술 샘플

개념

문자열 마샬링

플랫폼 호출 데이터 형식

문자열에 대한 기본 마샬링

관리 코드에서 프로토타입 만들기

문자 집합 지정