Click here to Skip to main content
Licence CPOL
First Posted 10 Apr 2007
Views 16,308
Downloads 240
Bookmarked 16 times

Startup Checker

By | 10 Apr 2007 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Programm3r

Software Developer (Senior)
Qmuzik Technologies Pty
South Africa South Africa

Member

Follow on Twitter Follow on Twitter
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.
 
So you want to surf, and the waves where you live aren't half bad.
And you start to improve ...
And then you hear about this wave in a far-away country, whose name you can't even pronounce. What then?
How far are you willing to go?
What sacrifice are you willing to make? How good do you really want to be?

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
GeneralSchweet PingroupDev_258020:15 25 Nov '09  
GeneralRe: Schweet PinmemberJohn Spectacle1:42 26 Nov '09  
GeneralSome comments PinmvpHans Dietrich4:25 10 Apr '07  
AnswerRe: Some comments PinmemberProgramm3r19:52 10 Apr '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
Web02 | 2.5.120517.1 | Last Updated 10 Apr 2007
Article Copyright 2007 by Programm3r
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid