Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Pocket PC » Invoking Unmanaged DLL Functions from Compact Framework for Pocket PC

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.

Author Rank :
Page Views : 2894
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace SDE_IE
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 16);
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.Text = "PocketPc.Com";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button3.Location = new System.Drawing.Point(40, 64);
this.button3.Size = new System.Drawing.Size(96, 32);
this.button3.Text = "IE";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button3
//
this.button2.Location = new System.Drawing.Point(40, 112);
this.button2.Size = new System.Drawing.Size(96, 32);
this.button2.Text = "Calculator";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(40, 160);
this.button4.Size = new System.Drawing.Size(96, 32);
this.button4.Text = "Calendar";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(40, 208);
this.button5.Size = new System.Drawing.Size(96, 32);
this.button5.Text = "Tasks";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(240, 270);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
static void Main()
{
Application.Run(
new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ShowPPCWebSite();
}
private void button2_Click(object sender, System.EventArgs e)
{
StartIE();
}
private void button3_Click(object sender, System.EventArgs e)
{
StartCalc();
}
private void button4_Click(object sender, System.EventArgs e)
{
StartCalendar();
}
private void button5_Click(object sender, System.EventArgs e)
{
StartTasks();
}
[DllImport("coredll.Dll")]
private static extern int CreateProcess(string strImageName, string strCmdLine, IntPtr pProcessAttributes, IntPtr pThreadAttributes , int bInheritsHandle, int
wCreationFlags, IntPtr pEnvironment, IntPtr pCurrentDir, Byte[] bArray, ProcessInfo oProc);
private void ShowPPCWebSite()
{
ProcessInfo pi =
new ProcessInfo();
CreateProcess("iexplore.exe", "www.microsoft.com/mobile/pocketpc/default.asp", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero,
new Byte[128], pi);
}
private void StartIE( )
{
ProcessInfo pi =
new ProcessInfo();
CreateProcess("iexplore.exe", "", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,
ntPtr.Zero,
new Byte[128], pi);
}
private void StartCalc()
{
ProcessInfo pi =
new ProcessInfo();
CreateProcess("calc.exe", "", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,
ntPtr.Zero,
new Byte[128], pi);
}
private void StartCalendar()
{
ProcessInfo pi =
new ProcessInfo();
CreateProcess("calendar.exe", "", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,
ntPtr.Zero,
new Byte[128], pi);
}
private void StartTasks()
{
ProcessInfo pi =
new ProcessInfo();
CreateProcess("tasks.exe", "", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero,
ntPtr.Zero,
new Byte[128], pi);
}
}
public class ProcessInfo
{
public int Process;
public int Thread;
public int ProcessID;
public int ThreadID;
}
}

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dipal Choksi
Dipal Choksi has over 10 years of industry experience in team-effort projects and also as an individual contributor. She has been working on the .Net platform since the beta releases of .Net 1.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.