이 샘플에서는 문자열 형식을 In 매개 변수로 값으로 전달하는 방법과 EntryPoint, CharSet, ExactSpelling 필드를 언제 사용하는지를 보여줍니다.
MsgBox 샘플은 원래 함수 선언과 함께 표시된 다음과 같은 관리되지 않는 함수를 사용합니다.
MessageBox가 User32.dll에서 내보내졌습니다.
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
이 샘플에서 NativeMethods
클래스는 MsgBoxSample
클래스에서 호출되는 관리되지 않는 각 함수에 대해 관리되는 프로토타입을 포함합니다. 관리되는 프로토타입 메서드MsgBox
MsgBox2
는 MsgBox3
관리되지 않는 동일한 함수에 대해 서로 다른 선언을 갖습니다.
ANSI로 지정된 문자 형식이 유니코드 함수의 이름인 진입점MsgBox2
과 일치하지 않으므로 메시지 상자에서 잘못된 출력을 생성하는 선언 MessageBoxW
입니다. 선언 MsgBox3
은 EntryPoint, CharSet 및 ExactSpelling 필드 간에 불일치를 만듭니다. 호출되면 MsgBox3
예외를 throw합니다. 문자열 이름 지정 및 이름 마샬링에 대한 자세한 내용은 문자 집합 지정을 참조하세요.
프로토타입 선언
private ref class NativeMethods
{
public:
// Declares managed prototypes for unmanaged functions.
[DllImport("User32.dll", EntryPoint = "MessageBox",
CharSet = CharSet::Auto)]
static int MsgBox(int hWnd, String^ text, String^ caption,
unsigned int type);
// Causes incorrect output in the message window.
[DllImport("User32.dll", EntryPoint = "MessageBoxW",
CharSet = CharSet::Ansi)]
static int MsgBox2(int hWnd, String^ text,
String^ caption, unsigned int type);
// Causes an exception to be thrown. EntryPoint, CharSet, and
// ExactSpelling fields are mismatched.
[DllImport("User32.dll", EntryPoint = "MessageBox",
CharSet = CharSet::Ansi, ExactSpelling = true)]
static int MsgBox3(int hWnd, String^ text,
String^ caption, unsigned int type);
};
internal static class NativeMethods
{
// Declares managed prototypes for unmanaged functions.
[DllImport("User32.dll", EntryPoint = "MessageBox",
CharSet = CharSet.Auto)]
internal static extern int MsgBox(
IntPtr hWnd, string lpText, string lpCaption, uint uType);
// Causes incorrect output in the message window.
[DllImport("User32.dll", EntryPoint = "MessageBoxW",
CharSet = CharSet.Ansi)]
internal static extern int MsgBox2(
IntPtr hWnd, string lpText, string lpCaption, uint uType);
// Causes an exception to be thrown. EntryPoint, CharSet, and
// ExactSpelling fields are mismatched.
[DllImport("User32.dll", EntryPoint = "MessageBox",
CharSet = CharSet.Ansi, ExactSpelling = true)]
internal static extern int MsgBox3(
IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
Friend Class NativeMethods
' Declares managed prototypes for unmanaged functions.
Friend Declare Auto Function MsgBox Lib "User32.dll" Alias "MessageBox" (
ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
ByVal uType As UInteger) As Integer
' Causes incorrect output in the message window.
Friend Declare Ansi Function MsgBox2 Lib "User32.dll" Alias "MessageBoxW" (
ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
ByVal uType As UInteger) As Integer
' Causes an exception to be thrown.
' ExactSpelling is True by default when Ansi or Unicode is used.
Friend Declare Ansi Function MsgBox3 Lib "User32.dll" Alias "MessageBox" (
ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
ByVal uType As UInteger) As Integer
End Class
함수 호출
public class MsgBoxSample
{
public:
static void Main()
{
NativeMethods::MsgBox(0, "Correct text", "MsgBox Sample", 0);
NativeMethods::MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);
try
{
NativeMethods::MsgBox3(0, "No such function", "MsgBox Sample", 0);
}
catch (EntryPointNotFoundException^)
{
Console::WriteLine("EntryPointNotFoundException thrown as expected!");
}
}
};
public class MsgBoxSample
{
public static void Main()
{
NativeMethods.MsgBox(0, "Correct text", "MsgBox Sample", 0);
NativeMethods.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);
try
{
NativeMethods.MsgBox3(0, "No such function", "MsgBox Sample", 0);
}
catch (EntryPointNotFoundException)
{
Console.WriteLine($"{nameof(EntryPointNotFoundException)} thrown as expected!");
}
}
}
Public Class MsgBoxSample
Public Shared Sub Main()
NativeMethods.MsgBox(0, "Correct text", "MsgBox Sample", 0)
NativeMethods.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0)
Try
NativeMethods.MsgBox3(0, "No such function", "MsgBox Sample", 0)
Catch e As EntryPointNotFoundException
Console.WriteLine($"{NameOf(EntryPointNotFoundException)} thrown as expected!")
End Try
End Sub
End Class
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET