Posts

Showing posts from November, 2011

Calling Win32 DLLs in C# with P/Invoke

Image
Unmanaged Code BOOL MessageBeep(   UINT uType   // beep type ); Managed Code [DllImport ("User32.dll")] static extern Boolean MessageBeep (UInt32 beepType); Note: Static 1.The MessageBeep method was declared as static.  2.This is a requirement for P/Invoke methods because there is no consistent notion of an instance in the  Window API. Extern 1. It is not a method call, but an extern method definition. 2. This is your hint to the compiler that you mean for the method to be implemented by a function exported from a DLL, and therefore there is no need for you to supply a method body. 3. P/Invoke methods are nothing more than metadata that the just-in-time (JIT) compiler uses to wire managed code to an unmanaged DLL function at run time.  4. An important piece of information required to perform this wiring to the unmanaged world is the name of the DLL from which the unmanaged method is exported .  6. The DllImport custom attribu