Posted By

jimfred on 07/29/09


Tagged

Interop DllImport


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

umang_nine
khouser
Priestd09
danfinrud55


C# calling a DLL API, in this case, MessageBox in user32.dll


 / Published in: C#
 

URL: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx

[1] Add 'using System.Runtime.InteropServices;' [2] Add DllImport and function declaration [3] call function.

This example involves passing strings and passing IntPtr.

  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. class Example
  5. {
  6. // Use DllImport to import the Win32 MessageBox function.
  7. [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  8. public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
  9.  
  10. static void Main()
  11. {
  12. // Call the MessageBox function using platform invoke.
  13. MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
  14. }
  15. }

Report this snippet  

You need to login to post a comment.