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){
returnValue = 0;
}
else if (result < 0){
returnValue = 0;
}
else
{
globalVariable = pe_sen.th32ProcessID;
counter++;
}
}
} while (Process32Next(h_sna, &pe_sen));
}
}
catch(...)
{
returnValue = -1;
}
CloseHandle (h_sna);
if (counter != 0 || counter > 0)
return (globalVariable); else
return 0;
Code Snippet - Calling Function
int rc = seekProgram("winamp.exe");
History
- 10th April, 2007: Initial post
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.