Click here to Skip to main content
Licence CPOL
First Posted 4 Jul 2007
Views 33,034
Downloads 114
Bookmarked 30 times

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

By Liam Cavanagh | 4 Jul 2007
The following article explains how to launch an executable from within your application on Windows Mobile using C#.

1

2

3
2 votes, 100.0%
4

5
4.00/5 - 2 votes
μ 4.00, σa 1.10 [?]

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



United States United States

Member
Liam Cavanagh currently works as a Sr. Program Manager for Microsoft focusing new cloud services in the SQL Azure organization. Within this group, Liam has created a number of new services including SQL Azure Data Sync, Microsoft Codename "Data Transfer", and Eye on Earth leveraging the Windows Azure infrastucture.
 
Additionally, he works with enterprise corporations implementing enterprise cloud and mobile solutions and conducts training seminars worldwide.
 
Liam holds a Bachelor of Mathematics degree in Business and Information Systems from the University of Waterloo in Waterloo, Ontario, Canada.
 
Specialties: Windows Azure, SQL Azure, SQL Server, Cloud, Mobile, Replication and Database Synchronization Technologies
 
Liam Cavanagh is the founder of Cotega, a data notificaton and scheduling service for cloud databases. You can contact Liam at his cloud data blog.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExecute a external application without displaying any messagebox PinmemberMember 6870913:19 13 Jan '10  
GeneralRe: Execute a external application without displaying any messagebox Pinmemberajaythackeracs6:27 22 Jul '10  
QuestionThank you but how about relative path? PinmemberEpon0:44 3 Apr '09  
I attempted to use relative path instead of "\windows\calc.exe" to something like "%CSIDL_PROGRAM_FILES%\MyApp" it failed. Do you have any idea?
 
thank you btw this is great job!
AnswerRe: 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    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 4 Jul 2007
Article Copyright 2007 by Liam Cavanagh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid