Platform Invoke Examples Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Developer's Guide
Platform Invoke Examples

The following examples demonstrate how to define and call the MessageBox function in User32.dll, passing a simple string as an argument. In the examples, the DllImportAttribute.CharSet Field field is set to Auto to let the target platform determine the character width and string marshaling.

The same example appears in Visual Basic, C#, and C++. To show all examples, click the Language Filter button 42b9ea93.Filter2(en-us,VS.90).gif in the upper-left corner of the page. For additional examples, see Marshaling Data with Platform Invoke.

Imports System.Runtime.InteropServices

Public Class Win32
    Declare Auto Function MessageBox Lib "user32.dll" _
       (ByVal hWnd As Integer, ByVal txt As String, _
       ByVal caption As String, ByVal Typ As Integer) As IntPtr
End Class

Public Class HelloWorld    
    Public Shared Sub Main()
        Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)
    End Sub
End Class


See Also

Concepts

Reference

Community Content

Incorrect sample
Added by:Noelle Mallory - MSFT

The example code seems to be incorrect (in all languages) since the first parameter of the MessageBox WinAPI function is a handle and returns an int value, so in C# it should read:

[DllImport("user32", CharSet=CharSet.Auto)]
public static extern int MessageBox(
IntPtr hWnd,
string lpText,
string lpCaption,
uint uType
);
Example works
Added by:Xatru
The example works correctly - you just have to add :

using systems;

...

Otherwise the string type is not known.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View