Invoking Unmanaged DLL Functions from Compact Framework for Pocket PC in VB.NET

In this example we will use the Compact Framework to create a program containing a launch pad for the Pocket PC. Invoking Unmanaged DLL Functions from Compact Framework for Pocket PC in VB.NET.

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.

sde_launch_1-in-vb.net.jpg

Figure: Launchpad Application in Action

Internationaliziing1-in-vb.net.gif


Figure: PocketPc.com site opened from the LaunchPad program through a single button click

Complete Code Listing

  1. Imports System  
  2. Imports System.Drawing  
  3. Imports System.Collections  
  4. Imports System.Windows.Forms  
  5. Imports System.Data  
  6. Imports System.Runtime.InteropServices  
  7. Public Class Form1  
  8. Inherits System.Windows.Forms.Form  
  9. Private button1 As System.Windows.Forms.Button  
  10. Private button3 As System.Windows.Forms.Button  
  11. Private button2 As System.Windows.Forms.Button  
  12. Private button4 As System.Windows.Forms.Button  
  13. Private button5 As System.Windows.Forms.Button  
  14. Private mainMenu1 As System.Windows.Forms.MainMenu  
  15. #Region " Windows Form Designer generated code "  
  16. Public Sub New()  
  17. MyBase.New()  
  18. 'This call is required by the Windows Form Designer.  
  19. InitializeComponent()  
  20. 'Add any initialization after the InitializeComponent() call  
  21. End Sub  
  22. 'Form overrides dispose to clean up the component list.  
  23. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)  
  24. MyBase.Dispose(disposing)  
  25. End Sub  
  26. 'NOTE: The following procedure is required by the Windows Form Designer  
  27. 'It can be modified using the Windows Form Designer.  
  28. 'Do not modify it using the code editor.  
  29. Private Sub InitializeComponent()  
  30. Me.mainMenu1 = New System.Windows.Forms.MainMenu  
  31. Me.button1 = New System.Windows.Forms.Button  
  32. Me.button3 = New System.Windows.Forms.Button  
  33. Me.button2 = New System.Windows.Forms.Button  
  34. Me.button4 = New System.Windows.Forms.Button  
  35. Me.button5 = New System.Windows.Forms.Button  
  36. 'button1  
  37. Me.button1.Location = New System.Drawing.Point(40, 16)  
  38. Me.button1.Size = New System.Drawing.Size(96, 32)  
  39. Me.button1.Text = "PocketPc.Com"  
  40. AddHandler button1.Click, AddressOf button1_Click  
  41. 'button3  
  42. Me.button3.Location = New System.Drawing.Point(40, 64)  
  43. Me.button3.Size = New System.Drawing.Size(96, 32)  
  44. Me.button3.Text = "IE"  
  45. AddHandler button3.Click, AddressOf button3_Click  
  46. 'button2  
  47. Me.button2.Location = New System.Drawing.Point(40, 112)  
  48. Me.button2.Size = New System.Drawing.Size(96, 32)  
  49. Me.button2.Text = "Calculator"  
  50. AddHandler button2.Click, AddressOf button2_Click  
  51. 'button4  
  52. Me.button4.Location = New System.Drawing.Point(40, 160)  
  53. Me.button4.Size = New System.Drawing.Size(96, 32)  
  54. Me.button4.Text = "Calendar"  
  55. AddHandler button4.Click, AddressOf button4_Click  
  56. 'button5  
  57. Me.button5.Location = New System.Drawing.Point(40, 208)  
  58. Me.button5.Size = New System.Drawing.Size(96, 32)  
  59. Me.button5.Text = "Tasks"  
  60. AddHandler button5.Click, AddressOf button5_Click  
  61. 'Form1  
  62. Me.Controls.Add(Me.button5)  
  63. Me.Controls.Add(Me.button4)  
  64. Me.Controls.Add(Me.button2)  
  65. Me.Controls.Add(Me.button3)  
  66. Me.Controls.Add(Me.button1)  
  67. Me.Menu = Me.mainMenu1  
  68. Me.Text = "Form1"  
  69. End Sub  
  70. Shared Sub Main()  
  71. Application.Run(New Form1)  
  72. End Sub 'Main  
  73. #End Region  
  74. Private Sub button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)  
  75. ShowPPCWebSite()  
  76. End Sub  
  77. Private Sub button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)  
  78. StartCalc()  
  79. End Sub  
  80. Private Sub button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)  
  81. StartIE()  
  82. End Sub  
  83. Private Sub button4_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)  
  84. StartCalendar()  
  85. End Sub  
  86. Private Sub button5_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)  
  87. StartTasks()  
  88. End Sub  
  89. Private Declare Function CreateProcess Lib "coredll.Dll" (ByVal strImageName As StringByVal strCmdLine As StringByVal pProcessAttributes As IntPtr, ByVal pThreadAttributes As IntPtr, ByVal bInheritsHandle As IntegerByVal dwCreationFlags As IntegerByVal pEnvironment As IntPtr, ByVal pCurrentDir As IntPtr, ByVal bArray() As [Byte], ByVal oProc As ProcessInfo) As Integer  
  90. Private Sub ShowPPCWebSite()  
  91. Dim pi As New ProcessInfo  
  92. CreateProcess("iexplore.exe""www.microsoft.com/mobile/pocketpc/default.asp", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, New [Byte](128) {}, pi)  
  93. End Sub 'ShowPPCWebSite  
  94. Private Sub StartIE()  
  95. Dim pi As New ProcessInfo  
  96. CreateProcess("iexplore.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, New [Byte](128) {}, pi)  
  97. End Sub 'StartIE  
  98. Private Sub StartCalc()  
  99. Dim pi As New ProcessInfo  
  100. CreateProcess("calc.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, New [Byte](128) {}, pi)  
  101. End Sub 'StartCalc  
  102. Private Sub StartCalendar()  
  103. Dim pi As New ProcessInfo  
  104. CreateProcess("calendar.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, New [Byte](128) {}, pi)  
  105. End Sub 'StartCalendar  
  106. Private Sub StartTasks()  
  107. Dim pi As New ProcessInfo  
  108. CreateProcess("tasks.exe""", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, New [Byte](128) {}, pi)  
  109. End Sub 'StartTasks  
  110. End Class  
  111. Public Class ProcessInfo  
  112. Public Process As Integer  
  113. Public Thread As Integer  
  114. Public ProcessID As Integer  
  115. Public ThreadID As Integer  
  116. End Class 'ProcessInfo 
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.