Click here to Skip to main content
6,930,524 members and growing! (22,953 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Howto     Beginner License: The Code Project Open License (CPOL)

Launch an Executable Programatically on Windows Mobile using CreateProcess and C#

By Liam Cavanagh

The following article explains how to launch an executable from within your application on Windows Mobile using C#.
C#2.0, Windows, .NETCF, .NET, PocketPC-2002, WinMobile2003, WinMobile5VS2005, Dev
Posted:4 Jul 2007
Views:25,164
Bookmarked:28 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.20 Rating: 4.00 out of 5

1

2

3
2 votes, 100.0%
4

5

Introduction

Perhaps it is just me, but there are a number of times when I have found that I needed to launch (or shell) an executable from within my application. For the most part, I have had to do this to launch a data synchronization client but I am sure there are a number of other reasons why you would need to do this on Windows Mobile. I was somewhat surprised that there were very few examples (that I could find) on how to do this on Windows Mobile.

Although there seem to be many ways to do this, my personal preference has been to use CreateProcess. I like this because it gives me the ability to either launch the executable and return immediately or I can sit and wait for the application to complete.

This article is meant to explain how I have used the CreateProcess function to launch an executable. If you know of a better way to do this or know how I can improve the code, I would love to hear from you. If not, I hope you find the code helpful.

Requirements

The following tools are required to get started with developing the application:

Using the Code

The application itself is fairly straightforward. The first thing you will need to do in your application is to add a reference to the following namespace:

using System.Runtime.InteropServices;

Once that is done, you will also need the following declarations in order to execute a program:

public class ProcessInfo
{
	public IntPtr hProcess;
	public IntPtr hThread;
	public IntPtr ProcessID;
	public IntPtr ThreadID;
}

[DllImport("CoreDll.DLL", SetLastError = true)]
private static extern int CreateProcess(String imageName, String cmdLine, 
	IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, 
	Int32 boolInheritHandles, Int32 dwCreationFlags, IntPtr lpEnvironment, 
	IntPtr lpszCurrentDir, byte[] si, ProcessInfo pi);

[DllImport("coredll")]
private static extern bool CloseHandle(IntPtr hObject);

[DllImport("coredll")]
private static extern uint WaitForSingleObject
			(IntPtr hHandle, uint dwMilliseconds);

[DllImport("coredll.dll", SetLastError = true)]
private static extern int GetExitCodeProcess
			(IntPtr hProcess, ref int lpExitCode);

Additionally, we need the following function to make it easier to launch the executable. Notice the line which uses WaitForSingleObject. This line can be commented out if you do not want to wait for the application to complete.

private void LaunchApp(string strPath, string strParms)
{
	ProcessInfo pi = new ProcessInfo();
	byte[] si = new byte[128];
	CreateProcess(strPath, strParms, IntPtr.Zero, IntPtr.Zero, 
		0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);
	// This line can be commented out if you do not want 
	// to wait for the process to exit
	WaitForSingleObject(pi.hProcess, 0xFFFFFFFF);
	int exitCode = 0;
	GetExitCodeProcess(pi.hProcess, ref exitCode);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return;
}

Now launching the application is simple. The following line will launch the executable with the appropriate parameters:

LaunchApp(textEXE.Text, textParms.Text);

History

  • 4th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Liam Cavanagh


Member
Liam Cavanagh currently works as a Program Manager for Microsoft in the Data Replication and Synchronization group.

Liam works in the development of “always available” mobile enterprise solutions – enabling mobile workers to remain productive regardless of network availability. Additionally, he works with enterprise corporations implementing enterprise mobile solutions, conducts training seminars worldwide on mobile and wireless technology design, development, and deployment. Liam holds a Bachelor of Mathematics degree in Business and Information Systems from the University of Waterloo in Waterloo, Ontario, Canada.

Specialties:
Mobile, Wireless, Replication and Database Technologies

Liam Cavanagh is also the author of the website Adventure City Tours, a set of free walking tours for mobile devices.

He can be reached at liam.cavanagh@microsoft.com.
Location: Canada Canada

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Windows Mobile, iPhone, Android - Marketplace Comparison
    Detailed comparison between Windows Mobile Marketplace, Apple's iPhone AppStore and Android Market from developer point of view.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralExecute a external application without displaying any messagebox PinmemberMember 6870913:19 13 Jan '10  
Hi,

Thanks for your code. Do you have any idea as to how to execute the external
application without displaying any messageboxes from that program?

Regards,
Dileep.
GeneralThank you but how about relative path? PinmemberEpon0:44 3 Apr '09  
GeneralRe: Thank you but how about relative path? PinmemberEpon1:34 3 Apr '09  
GeneralTo run CreateProcess .... Pinmembervijaywithu18:52 24 Feb '09  
GeneralThank you very much! PinmemberMember 44205991:58 19 Jun '08  
GeneralAbout FTP applicaqtion for Mobile Pinmemberndas.net0:28 30 Apr '08  
GeneralHave you tried System.Diagnostics.Process...Not sure if this works in Embedded Env. PinmemberAnandChavali21:28 4 Jul '07  
GeneralRe: Have you tried System.Diagnostics.Process...Not sure if this works in Embedded Env. PinmemberLiam Cavanagh10:57 5 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jul 2007
Editor: Deeksha Shenoy
Copyright 2007 by Liam Cavanagh
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project