Click here to Skip to main content
Licence CPOL
First Posted 21 Jan 2006
Views 32,373
Bookmarked 29 times

Writing a Win32 method to find if an application is already running

By | 21 Jan 2006 | Article
This article demonstrates how to write a method to find out if an application is already running in the background.

Introduction

I never was a Win32 developer. But sometimes, the .NET classes just don't make the cut. During my work, I needed a way to check if a certain application is running in the background. Obviously, I turned to the Process class that can be found in the System.Diagnostics namespace. Not much long after, I had this perfect looking piece of code:

if (Process.GetProcessesByName(appName).Length > 0)
{
    // Warn the user that the application
    // is running in the background
}

It took our QA team less than a minute to tear these one-and-a-half lines of code to pieces. It turns out, that the Process class in .NET relies heavily on the PerfMon process/application. If the machine you're running has it disabled for some reason, you'll get an exception from the GetProcessesByName method. And it will take the exception a couple of seconds to be thrown. And that's the best case scenario. A couple of minutes spent with the other developers taught me that the usage of Process might just freeze or process. This was of course unacceptable.

The solution

I had no choice but to find another way. After some research, I found a couple of Win32 methods that I thought might help me: Process32First and Process32Next.

Now, I was ready to write my own IsProcessRunning method. Let's start with the different DllImport declarations:

[DllImport("kernel32.dll")]
private static extern int Process32First(IntPtr hSnapshot, 
                                 ref PROCESSENTRY32 lppe);

[DllImport("kernel32.dll")]
private static extern int Process32Next(IntPtr hSnapshot, 
                                ref PROCESSENTRY32 lppe);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, 
                                               uint th32ProcessID);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool CloseHandle(IntPtr hSnapshot);

private const uint TH32CS_SNAPPROCESS = 0x00000002;

[StructLayout(LayoutKind.Sequential)]
private struct PROCESSENTRY32
{
    public uint dwSize;
    public uint cntUsage;
    public uint th32ProcessID;
    public IntPtr th32DefaultHeapID;
    public uint th32ModuleID;
    public uint cntThreads;
    public uint th32ParentProcessID;
    public int pcPriClassBase;
    public uint dwFlags;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
}

And here are some basic explanations of the methods:

  • PROCESSENTRY32 – This struct will obviously be used to keep the information of the different processes in the system.
  • Process32First/Process32Next – These methods will serve to enumerate all the processes.
  • CreateToolhelp32Snapshot – We'll use this method to create a snapshot of the system and the processes.

Now let's look at the code:

public static bool IsProcessRunning(string applicationName)
{
    IntPtr handle = IntPtr.Zero;
    try
    {
        // Create snapshot of the processes
        handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        PROCESSENTRY32 info = new PROCESSENTRY32();
        info.dwSize = (uint)System.Runtime.InteropServices.
                      Marshal.SizeOf(typeof(PROCESSENTRY32));

        // Get the first process
        int first = Process32First(handle, ref info);
        // If we failed to get the first process, throw an exception
        if (first == 0)
            throw new Tzunami.Common.TzunamiException("Cannot" + 
                                        " find first process.");

        // While there's another process, retrieve it
        do
        {
            if (string.Compare(info.szExeFile, 
                  applicationName, true) == 0)
            {
                return true;
            }
        }
        while (Process32Next(handle, ref info) != 0);
    }
    catch
    {
        throw;
    }
    finally
    {
        // Release handle of the snapshot
        CloseHandle(handle);
        handle = IntPtr.Zero;
    }
    return false;
}

License

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

About the Author

Itay Sagui

Team Leader

Israel Israel

Member

I currently work as the development manager in a company called Tzunami Inc. that develops a content migration solution for Microsoft SharePoint . Our product, called Tzunami Deployer is developed using C#.

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
Questionver urgent - GetProcessesByName() method if I am a power user Pinmemberpopaserban3:23 21 Nov '06  
QuestionWhat about this method? Pinmemberevgeniyn21:00 19 Nov '06  
Questionhow can i detect apllication is already dieed Pinmembersimboychan17:25 31 May '06  
GeneralAlternative implementation Pinmembergrinbergnir5:06 22 Jan '06  
Last week I've come across the exact same problem.
After reading thoroughly both Enumerating Windows Processes (by Alex Fedotov)[^] and How To Enumerate Applications Using Win32 APIs (Microsoft KB)[^] I've decided to implement a slightly different solution (the following code snippet).
 
I prefered not to use the ToolHelp32 API (Itay's implementation) because of the follwing argument from Alex's article:
" Rumors are the Windows NT development team was refusing to include ToolHelp32 API into the system for long time, but anyway, starting with Windows 2000, you can see this API in the Windows NT family of operating systems. "
 
The attached code is not complete (the complete sample may be added on demand).
 
		/// <summary>
		///		Gets Process id for the specified process name.
		/// </summary>
		/// <param name="ProcessName">Non case-sensitive process name.</param>
		/// <returns>Process's unique identifier. Zero is returned if process name could not be found.</returns>
		public static int GetProcessIdByName(string ProcessName)
		{
			int result = 0;
			int[] pids = new int[1024];
			int bytesNeeded;
			StringBuilder sbProcessName = new StringBuilder(1024);
			string sProcessName = String.Empty; 
			string sRequiredProcessName = ProcessName.ToLower();
 
			// Enumerates system process to pids - int array that will contain all process id's.
			if (Win32Helper.EnumProcesses(pids, 1024, out bytesNeeded) == false) 
			{
				return result;
			}
 
			IntPtr hProcess = IntPtr.Zero;
			IntPtr hModule = IntPtr.Zero;
			// Iterates through the pids
			for(int i=0; i<(bytesNeeded/4); i++) 
			{
				try
				{
					hProcess = IntPtr.Zero;
 
					// retrive a process handle, later-on CloseHandle method will release this handle
					hProcess = Win32Helper.OpenProcess(
						(int)(	Win32Helper.ProcessAccessRights.PROCESS_QUERY_INFORMATION | 
						Win32Helper.ProcessAccessRights.PROCESS_VM_READ), 
						false, 
						pids[i]);
 
					if (hProcess == IntPtr.Zero)
					{
						continue;
					}
 
					hModule = IntPtr.Zero;
					int cbNeeded = 0;
 
					int iProcessNameLength = Win32Helper.GetModuleBaseName(hProcess, hModule, 
						sbProcessName, 1024);
					if (iProcessNameLength < 1)
					{
						continue;
					}
 
					#region Process name comparison and return value.
					sProcessName = sbProcessName.ToString(0, iProcessNameLength).ToLower();
					if (sProcessName.IndexOf(sRequiredProcessName) > -1)
					{
						return pids[i];
					}
					#endregion
 
				}
				finally
				{
					Win32Helper.CloseHandle(hProcess);
				}
			}
 
			return result;
		}

GeneralRe: Alternative implementation Pinmemberentraped.isoLated19:12 22 Jul '07  
AnswerRe: Alternative implementation Pinmembergrinbergnir0:57 23 Jul '07  
QuestionComparing the first process? PinprotectorMarc Clifton12:59 21 Jan '06  
AnswerRe: Comparing the first process? PinmemberItay Sagui8:35 22 Jan '06  

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
Web01 | 2.5.120529.1 | Last Updated 21 Jan 2006
Article Copyright 2006 by Itay Sagui
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid