Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Startup Checker

0.00/5 (No votes)
10 Apr 2007 3  
Find another process running of a specific process

Introduction

The following dynamic link library searches for a certain process that was specified by the user / calling application, that is currently running. Thus if you want to prevent another process of the same application from opening / starting more than once, this function will do just that. I hope this will be useful.

Using the Code

To use the dynamic link library, follow these steps:

  • Link your program with the .lib file.
  • Project -> Settings -> Link -> Object/library modules.
  • In the text area, add the startupchecker.lib file and click ok.
  • After adding the library file, add the "startupchecker" header file.
#include "startupchecker.h"

After adding the .lib and header file, you can call the function.

  • Function Name: seekProgram
  • Function Parameters: char* Name (Exact Name of process to search for)
  • Return Type: Integer
  • Return: pid if found, else zero

Code Snippet from startupchecker.dll

HANDLE h_pro;
HANDLE h_sna;
PROCESSENTRY32 pe_sen = {0};

int result;
int returnValue;
int counter = 0;

char* ProcessNames = Name;

h_sna = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (h_sna == INVALID_HANDLE_VALUE){
    returnValue = -2;
    return (returnValue);
}

pe_sen.dwSize = sizeof(PROCESSENTRY32);

try{
    if (Process32First(h_sna, &pe_sen))
    {
        do
        {
            h_pro = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pe_sen.th32ProcessID);
            CloseHandle (h_pro);
            
            if (pe_sen.th32ProcessID != 0)
            {
                result = strcmp (pe_sen.szExeFile,ProcessNames);
                
                if(result > 0){
                    // Not Found
                    returnValue = 0;
                }
                else if (result < 0){
                    // Not Found
                    returnValue = 0;
                }
                else
                {
                    // Found Process
                    globalVariable = pe_sen.th32ProcessID;
                    counter++;
                }
            }
        } while (Process32Next(h_sna, &pe_sen));
    }
}
catch(...)
{
    returnValue = -1;
}
CloseHandle (h_sna);

if (counter != 0 || counter > 0)
    return (globalVariable);     // Found the process and returning pid
else
    return 0;                    // Nothing was found

Code Snippet - Calling Function

int rc = seekProgram("winamp.exe");

History

  • 10th April, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here