Invoking Unmanaged DLL Functions from Compact Framework for Pocket PC

In this example we will use the Compact Framework to create a program containing a launch pad for the Pocket PC. Our program will provide an integrated interface to launch some of the common programs on the Pocket PC. This sample serves to demonstrate the use of the Pinvoke mechanism on the Pocket PC to launch external programs such as Internet Explorer and Calculator. This example can be extended to create a convenient toolbox for launching frequently used Pocket PC programs.
 
The Compact Framework does not have complete support for COM Interop and interoperating with ActiveX controls. It provides the Platform Invoke mechanism for calling unmanaged DLLs. We will explore this functionality more in detail in this example.
 
The complete code listing along with sample screenshots follow.
 
The core of our example consists of using C# 's DLLImport attribute to access the "CreateProcess" function from the unmanaged DLL coredll.dll. This function is invoked in the program to launch the program as specified in the parameters.
 
 
 
Figure: Launchpad Application in Action.
 
 
 
Figure: PocketPc.com site opened from the LaunchPad program through a single button click
 
Complete Code Listing
  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.Windows.Forms;  
  5. using System.Data;  
  6. using System.Runtime.InteropServices;  
  7. namespace SDE_IE  
  8. {  
  9. public class Form1 : System.Windows.Forms.Form  
  10. {  
  11. private System.Windows.Forms.Button button1;  
  12. private System.Windows.Forms.Button button3;  
  13. private System.Windows.Forms.Button button2;  
  14. private System.Windows.Forms.Button button4;  
  15. private System.Windows.Forms.Button button5;  
  16. private System.Windows.Forms.MainMenu mainMenu1;  
  17. public Form1()  
  18. {  
  19. InitializeComponent();  
  20. }  
  21. protected override void Dispose( bool disposing )  
  22. {  
  23. base.Dispose( disposing );  
  24. }  
  25. #region Windows Form Designer generated code  
  26. ///  
  27. /// Required method for Designer support - do not modify  
  28. /// the contents of this method with the code editor.  
  29. ///  
  30. private void InitializeComponent()  
  31. {  
  32. this.mainMenu1 = new System.Windows.Forms.MainMenu();  
  33. this.button1 = new System.Windows.Forms.Button();  
  34. this.button3 = new System.Windows.Forms.Button();  
  35. this.button2 = new System.Windows.Forms.Button();  
  36. this.button4 = new System.Windows.Forms.Button();  
  37. this.button5 = new System.Windows.Forms.Button();  
  38. //  
  39. // button1  
  40. //  
  41. this.button1.Location = new System.Drawing.Point(40, 16);  
  42. this.button1.Size = new System.Drawing.Size(96, 32);  
  43. this.button1.Text = "PocketPc.Com";  
  44. this.button1.Click += new System.EventHandler(this.button1_Click);  
  45. //  
  46. // button2  
  47. //  
  48. this.button3.Location = new System.Drawing.Point(40, 64);  
  49. this.button3.Size = new System.Drawing.Size(96, 32);  
  50. this.button3.Text = "IE";  
  51. this.button3.Click += new System.EventHandler(this.button3_Click);  
  52. //  
  53. // button3  
  54. //  
  55. this.button2.Location = new System.Drawing.Point(40, 112);  
  56. this.button2.Size = new System.Drawing.Size(96, 32);  
  57. this.button2.Text = "Calculator";  
  58. this.button2.Click += new System.EventHandler(this.button2_Click);  
  59. //  
  60. // button4  
  61. //  
  62. this.button4.Location = new System.Drawing.Point(40, 160);  
  63. this.button4.Size = new System.Drawing.Size(96, 32);  
  64. this.button4.Text = "Calendar";  
  65. this.button4.Click += new System.EventHandler(this.button4_Click);  
  66. //  
  67. // button5  
  68. //  
  69. this.button5.Location = new System.Drawing.Point(40, 208);  
  70. this.button5.Size = new System.Drawing.Size(96, 32);  
  71. this.button5.Text = "Tasks";  
  72. this.button5.Click += new System.EventHandler(this.button5_Click);  
  73. //  
  74. // Form1  
  75. //  
  76. this.ClientSize = new System.Drawing.Size(240, 270);  
  77. this.Controls.Add(this.button5);  
  78. this.Controls.Add(this.button4);  
  79. this.Controls.Add(this.button2);  
  80. this.Controls.Add(this.button3);  
  81. this.Controls.Add(this.button1);  
  82. this.Menu = this.mainMenu1;  
  83. this.Text = "Form1";  
  84. }  
  85. #endregion  
  86. static void Main()  
  87. {  
  88. Application.Run(new Form1());  
  89. }  
  90. private void button1_Click(object sender, System.EventArgs e)  
  91. {  
  92. ShowPPCWebSite();  
  93. }  
  94. private void button2_Click(object sender, System.EventArgs e)  
  95. {  
  96. StartIE();  
  97. }  
  98. private void button3_Click(object sender, System.EventArgs e)  
  99. {  
  100. StartCalc();  
  101. }  
  102. private void button4_Click(object sender, System.EventArgs e)  
  103. {  
  104. StartCalendar();  
  105. }  
  106. private void button5_Click(object sender, System.EventArgs e)  
  107. {  
  108. StartTasks();  
  109. }  
  110. [DllImport("coredll.Dll")]  
  111. private static extern int CreateProcess(string strImageName, string strCmdLine, IntPtr pProcessAttributes, IntPtr pThreadAttributes , int bInheritsHandle, int  
  112. wCreationFlags, IntPtr pEnvironment, IntPtr pCurrentDir, Byte[] bArray, ProcessInfo oProc);  
  113. private void ShowPPCWebSite()  
  114. {  
  115. ProcessInfo pi = new ProcessInfo();  
  116. CreateProcess("iexplore.exe""www.microsoft.com/mobile/pocketpc/default.asp", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, new Byte[128], pi);  
  117. }  
  118. private void StartIE( )  
  119. {  
  120. ProcessInfo pi = new ProcessInfo();  
  121. CreateProcess("iexplore.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,  
  122. ntPtr.Zero, new Byte[128], pi);  
  123. }  
  124. private void StartCalc()  
  125. {  
  126. ProcessInfo pi = new ProcessInfo();  
  127. CreateProcess("calc.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,  
  128. ntPtr.Zero, new Byte[128], pi);  
  129. }  
  130. private void StartCalendar()  
  131. {  
  132. ProcessInfo pi = new ProcessInfo();  
  133. CreateProcess("calendar.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,  
  134. ntPtr.Zero, new Byte[128], pi);  
  135. }  
  136. private void StartTasks()  
  137. {  
  138. ProcessInfo pi = new ProcessInfo();  
  139. CreateProcess("tasks.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,  
  140. ntPtr.Zero, new Byte[128], pi);  
  141. }  
  142. }  
  143. public class ProcessInfo  
  144. {  
  145. public int Process;  
  146. public int Thread;  
  147. public int ProcessID;  
  148. public int ThreadID;  
  149. }  
  150. }
NOTE: This article is purely for demonstration. This article should not be construed as a best practices white paper. This article is entirely original, unless specified. Any resemblance to other material is an un-intentional coincidence and should not be misconstrued as malicious, slanderous, or any anything else hereof.
 
Conclusion
 
In this article, we explored the code to launch external applications on Pocket PC using Pinvoke and unmanaged DLLs. I would like to give credit to Scott Swigart for his valuable inputs on support newsgroups.


Similar Articles