Platform Invoke

Although the focus of this book is COM Interop, I would be remiss if I didn't discuss Platform Invocation Service (PInvoke), which allows you to call non-COM DLLs, Windows API and System functions. To use PInvoke perform the following steps:

  • Declare a function in your managed code that has an equivalent parameter list and return value to the unmanaged function. You will need to use the static and extern keyword in your function declaration.
  • Use the System.Runtime.InteropServices.DllImport attribute on your function declaration to (among other things) indicate which unmanaged DLL the implementation of the method resides in.
  • Optionally specify custom marshaling attributes for the parameters and return values if necessary.
  • Call the ...

Get .NET and COM Interoperability Handbook, The now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.