Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralExecute a external application without displaying any messagebox
Member 687091
3: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?
Epon
0: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!
GeneralRe: Thank you but how about relative path?
Epon
1:34 3 Apr '09  
I already solved it by using the following command

System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

this returns full path that wherever the current exe is running. just then put the callee exe in the same folder.

thank you.
GeneralTo run CreateProcess ....
vijaywithu
18:52 24 Feb '09  
Hi all,
I want to run a createprocess snippet so that it should execute more than one parameter for the .exe
which means in this code
CreateProcess(_T("\\windows\\ceplayer.exe"),_T("c\\folder\\new.wmv"), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, NULL, &processInfo);

i want to execute new.wmv and also once new.wmv is done i want to run next.wmv and so on in a row.

Can anyone help me in this regard.
(But the thing is we should not create a playlist)
GeneralThank you very much!
Member 4420599
1:58 19 Jun '08  
That was for me very helpful!
Thanks again
GeneralAbout FTP applicaqtion for Mobile
ndas.net
0:28 30 Apr '08  
I have a Mobile application written by c#.net . I want to incorporate a FTP application in this.My FTP application works in that mannar such as I give IP address, User Id, Password and it will be upload to that server and vise versa also.Is there any solution???
GeneralHave you tried System.Diagnostics.Process...Not sure if this works in Embedded Env.
AnandChavali
21:28 4 Jul '07  
// I dont know if this works in Embedded .NET(Compact Framework)..I have not tested..
// But this works great in normal .NET
// No need to use CreateProcess directly...

using System.Diagnostics;

Process proc = new Process();
proc.StartInfo.FileName = #your_executable_with_full_path#;
proc.StartInfo.Arguments = #any_cmdline_arguments_to_your_exe#;
proc.StartInfo.WorkingDirectory = #your_working_dir#;
proc.StartInfo.CreateNoWindow = #whether_to_show_command_window#;
proc.StartInfo.RedirectStandardOutput = #redirect?#;
proc.StartInfo.RedirectStandardError = #redirect?#;
proc.StartInfo.UseShellExecute = #if_shellExceute_has_to_be_used__you cannot standard
#error and output if you use this#;

// Now start the process
proc.Start();

// Wait for its exit
proc.WaitForExit();

// Get the exit code using proc.ExitCode



Thanks and Regards,
Anand.

GeneralRe: Have you tried System.Diagnostics.Process...Not sure if this works in Embedded Env.
Liam Cavanagh
10:57 5 Jul '07  
I don't think this is available on the older devices and 1.0 compact framework, but I do agree that this is likely a better way to do it on the 2.0 compact framework. Thanks!



Last Updated 4 Jul 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010