Shedding Light on windows Programming Home Training Resources Registration About the Paul Yao Company
The Paul Yao Company
P/Invoke Wizard Home

The PInvoke Wizard
PInvoke Programming
2. Can I Avoid PInvoke?
Can you avoid PInvoke? Perhaps, if you are writing very simple programs. But if your software relies on an existing, native Win32 DLL, then you must either: (1) rewrite the DLL in managed code, or (2) rely on PInvoke to bridge the gap. The problem is particularly onerous when you do not have the source code for the DLL – perhaps because it was provided by a third-party. In that case, you have to use PInvoke (or remove a feature from the managed code version of your software -- something that users might not like).

Another situation when you need to use PInvoke is when you want to access an operating system feature not supported by the framework. On the desktop .NET Framework, you are going to need to call PInvoke for the following:

  • ActiveX Controls
  • Custom, Third-Party DLLs
  • Control Panel interactions
  • Device Driver interactions
  • DirectX
  • Drawing with 3D Graphics
  • COM components
  • Mail API (MAPI)
  • Power Management States (lock computer / log off / etc)
  • Setup programs
  • Structured Storage
  • Telephony API (TAPI)
  • Windows Hooks
While PInvoke is helpful on the desktop, it is essential when building Windows Mobile or Windows CE applications written for the .NET Compact Framework. The Compact Framework brings to mobile and embedded devices some of the features of the desktop .NET Framework. But many features from the desktop framework were left out to adapt to the memory constraints of mobile and embedded devices. You get an idea about the differences between the two frameworks when you consider that the .NET Compact Framework is about 2 MB, a significant scaling back from the 25+ MB of the desktop .NET Framework. Some of the areas where PInvoke is critical for Compact Framework programming includes the following:

  • ActiveSync
  • ActiveX Controls
  • Carets
  • CE Property databases
  • Clipboard
  • COM Components
  • Common Dialog Boxes
  • Cryptography API
  • Custom controls
  • Custom, Third-Party DLLs
  • DirectX
  • File System enumeration - flash cards, directories, etc.
  • Mail API (MAPI)
  • Memory allocation of unmanaged memory
  • Microsoft Message Queue (MSMQ)
  • Point-to-Point Message Queues
  • Printing
  • Real Time Threads
  • Running programs
  • Shell functions
  • Structured Storage
  • Sounds
  • Telephony API (TAPI)
  • Stream interface drivers
  • Windows Hooks


Last updated: October 25, 2023
By: Paul Yao
The Paul Yao Company

CONTACT US LOGISTICS REQUEST MORE INFORMATION