Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este ejemplo se muestra cómo pasar tipos de cadena por valor como parámetros In y cuándo usar los EntryPointcampos , CharSety ExactSpelling .
El ejemplo MsgBox usa la siguiente función no administrada, que se muestra con su declaración de función original:
Cuadro de mensajes exportado desde User32.dll.
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
En este ejemplo, la NativeMethods
clase contiene un prototipo administrado para cada función no administrada a la que llama la MsgBoxSample
clase . Los métodos de prototipo administrados MsgBox
, MsgBox2
y MsgBox3
tienen declaraciones diferentes para la misma función no administrada.
La declaración para MsgBox2
genera una salida incorrecta en el cuadro de mensaje porque el tipo de carácter, especificado como ANSI, no coincide con el punto MessageBoxW
de entrada , que es el nombre de la función Unicode. La declaración para MsgBox3
crea una discrepancia entre los campos EntryPoint, CharSet y ExactSpelling . Cuando se llama a , MsgBox3
produce una excepción. Para obtener información detallada sobre la nomenclatura de cadenas y la serialización de nombres, vea Especificación de un juego de caracteres.
Declarar prototipos
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
Funciones de llamada
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