I have an app that starts another copy of itself, but after the application has started twice it should no longer start. It's a console application.
What I have tried:
I have tried to write this to check if my app is running 2 same proc.
void checkProcNumber(int cc ,const char* procfilename) {
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 p32;
p32.dwSize = sizeof(p32);
BOOL hres = Process32First(hsnap, &p32);
while (hres) {
if (strcmp(p32.szExeFile, procfilename)==0) {
cc ++;
}
hres = Process32Next(hsnap, &p32);
}
CloseHandle(hsnap);
}
And after, this to open the app (in the main)
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName(NULL, buffer, MAX_PATH);
string current_path = (buffer);
ShellExecute(NULL, "open", current_path.c_str(), NULL, NULL, SW_HIDE);
and finally this:
int x = 0;
checkProcNumber(x, "test.exe");
if (x == 2) {
cout << "finish";
Sleep(1000)
}
The problem is that the application does not start twice, but runs indefinitely