Calling Win32 Api from C#

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Shalvin Shalvin is offline Offline Feb 14th, 2008, 11:12 pm |
0
Though .Net classes are vast and versatile at time you will have to resort to Win32 API calls for accomplishing certain tasks.

The following code shows a simple example of invoking a Win32 api.

The best way to use Win32 apis is to encapsulate it in a class module.
The namespace for COM interop is System.Runtime.InteropServices.
The method name should have DllImport attribute specifying the name of the dll. Here the dll is user32. Other dlls are Gdi and Kernel.
Quick reply to this message  
C# Syntax
  1. //Class
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace SwapMouseEg {
  5.  
  6. class SwapMouse {
  7. [DllImport("user32.dll")]
  8. public static extern Int32 SwapMouseButton(Int32 bSwap);
  9. }
  10. }
  11.  
  12. //Form
  13. private void btnSwap_Click(object sender, EventArgs e)
  14. {
  15. SwapMouse.SwapMouseButton(1);
  16. }
  17. private void btnReset_Click(object sender, EventArgs e) {
  18. SwapMouse.SwapMouseButton(0);
  19. }

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC