Tuesday, August 14, 2012

DllImport in C# vs VB.NET (unmanaged DLL Import)

The following code example shows how to use the DllImportAttribute attribute to import the Win32 MessageBox function. The code example then calls the imported method.
C#

using System;
using System.Runtime.InteropServices;

class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}
VB.NET
Imports System
Imports System.Runtime.InteropServices

Module Example

' Use DllImport to import the Win32 MessageBox function.
"user32.dll"
, CharSet:=CharSet.Unicode)> _ Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer End Function Sub Main() ' Call the MessageBox function using platform invoke. MessageBox(New IntPtr(0), "Hello World!", "Hello Dialog", 0) End Sub End Module
Source : msdn 
url :http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx

No comments:

Post a Comment