Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++

Startup Checker

Rate me:
Please Sign up or sign in to vote.
3.18/5 (7 votes)
10 Apr 2007CPOL 31.8K   452   16   4
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.
C++
#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

C++
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

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

History

  • 10th April, 2007: Initial post

License

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


Written By
Architect Kohde.io
South Africa South Africa
I escaped from the mental hospital on 25th June and was captured by a zookeeper. Escaped from the zoo on 15th July and killed the zoo guard in the attempt. So now I just eat bananas and hang out on the Code Project.

Comments and Discussions

 
GeneralSchweet Pin
Dev_258025-Nov-09 20:15
Dev_258025-Nov-09 20:15 
GeneralRe: Schweet Pin
John Spectacle26-Nov-09 1:42
John Spectacle26-Nov-09 1:42 
GeneralSome comments Pin
Hans Dietrich10-Apr-07 4:25
mentorHans Dietrich10-Apr-07 4:25 
AnswerRe: Some comments Pin
Programm3r10-Apr-07 19:52
Programm3r10-Apr-07 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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