This is were native code meets .NET.  Its very much a black art, lots of smoke and mirrors etc. etc… But a necessary evil with Windows CE.  That is because it is the gateway from .NET applications to the hardware and to the operating system.  This is the first of a series of blogs on this matter.

I must though give credit to others’ who shoulders I am standing on for this.  Whilst I have had experience with PInvoke going back to the first version of .NET CF on Windows CE (I think it was about V4.2) I have referenced a number of sources in an attempt to provide a comprehensive coverage of this topic.  I’ll provide a reading list and some links in a later blog.


These are some of the topics that I will cover:

  • What are blitable types (ie bool, int etc .. simple types)
  • PInvoking a native function that returns a blitable type.
  • Marshalling
  • PInvoking a native function that returns a string type
  • PInvoking a native function that returns a structure or class,
  • PInvoking a native function that sets a void callback
    • The callback delagate takes no parameters
  • Pinvoking a native function that sets a blitable callback
    • The callback delegate takes a simple parameter

A simple example:

  • In a Smart Device C++ Native DLL (that builds to “native.dll” )
    • In Header
                extern "C" NATIVE_API  bool GetTest1(void);
    • In Source:

        bool val = true;
        extern "C" NATIVE_API  bool GetTest1(void){
                val = !val;
                return val;

  • In Smart Device C# Windows Form application
  • Create an internal static class  PInvoke
    • Provide native function signature:

      [DllImport( “native.dll” )]
      public static extern bool GetTest1();
  • In the the form where required
              bool result = PInvoke.GetTest1();

Need using references to System.Runtime.InteropServices in both of the C# files.

More on this in the next blog.

Cheers DJ

PS Good luck to those 3 presenting at the embedded Spark Challenge final.