MsgBox Sample
This sample demonstrates how to pass string types by value as In parameters and when to use the EntryPoint, CharSet, and ExactSpelling fields. The Microsoft .NET Framework SDK includes the complete Visual Basic .NET and C# versions of this sample in Samples\Technologies\Interop\Platform-Invoke.
The MsgBox sample uses the following unmanaged function, shown with its original function declaration:
MessageBox exported from User32.dll.
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
In this sample, the LibWrap
class contains a managed prototype for each unmanaged function called by the MsgBoxSample
class. The managed prototype methods MsgBox
, MsgBox2
, and MsgBox3
have different declarations for the same unmanaged function.
The declaration for MsgBox2
produces incorrect output in the message box because the character type, specified as ANSI, is mismatched with the entry point MessageBoxW
, which is the name of the Unicode function. The declaration for MsgBox3
creates a mismatch between the EntryPoint, CharSet, and ExactSpelling fields. When called, MsgBox3
throws an exception. For detailed information on string naming and name marshaling, see Specifying a Character Set.
Declaring Prototypes
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 .NET 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 'LibWrap
[C#]
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 );
}
Calling Functions
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 'Main
End Class 'MsgBoxSample
[C#]
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!" );
}
}
}
See Also
Marshaling Strings | Platform Invoke Data Types | Default Marshaling for Strings | Creating Prototypes in Managed Code | Specifying a Character Set