How to prevent user open the majar execution more than twice?
-
Some times that the user often clicks the exe files more than twice,if that it would appear erros for my project. So how to do to prevent the user open more than twice.Only approved that it has a single exe all the time.
-
Some times that the user often clicks the exe files more than twice,if that it would appear erros for my project. So how to do to prevent the user open more than twice.Only approved that it has a single exe all the time.
@nicker-player
Several possible approaches are shown in Qt/C++ - Lesson 043. Qt Single Application - Start only one instance of application or Qt: Best practice for a single instance app protection or in this forum at QT single instance application. -
@nicker-player
Several possible approaches are shown in Qt/C++ - Lesson 043. Qt Single Application - Start only one instance of application or Qt: Best practice for a single instance app protection or in this forum at QT single instance application.@JonB
thanks a lot.Next time i will search the forum before post the topic.And I found that it maybe cause the errors by the way of using lock files.for example, if the target os was shutted down suddenly and the program was breaked out,but the lock files were not removed at the same time.
I think it's better of the way by using of sharememory or qprocess .Dosent it?
and in this topic https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection
some one post the codes that use the win32 api to snapshot the processes which have started.QString pName = qApp->applicationDisplayName(); pName += ".exe"; PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (Process32First(snapshot, &entry) == TRUE) { DWORD myPID = GetCurrentProcessId(); while (Process32Next(snapshot, &entry) == TRUE) { const WCHAR* wc = entry.szExeFile ; _bstr_t b(wc); const char* c = b; if (stricmp(c, pName.toStdString().c_str()) == 0) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); qDebug() <<"myPID: "<< myPID << "entry.th32ProcessID" << entry.th32ProcessID; if(myPID != entry.th32ProcessID) TerminateProcess(hProcess,0); QThread::msleep(10); CloseHandle(hProcess); } } } CloseHandle(snapshot);
It seemed complexed for me. Is it more easily by using the boost process module or qt module(I am not very good at the process of the system)?
And the code is not designed for the cross platform usage.If I moved the project to mac os or android or something else,I have to re-coding. ^.^! -
@JonB
thanks a lot.Next time i will search the forum before post the topic.And I found that it maybe cause the errors by the way of using lock files.for example, if the target os was shutted down suddenly and the program was breaked out,but the lock files were not removed at the same time.
I think it's better of the way by using of sharememory or qprocess .Dosent it?
and in this topic https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection
some one post the codes that use the win32 api to snapshot the processes which have started.QString pName = qApp->applicationDisplayName(); pName += ".exe"; PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (Process32First(snapshot, &entry) == TRUE) { DWORD myPID = GetCurrentProcessId(); while (Process32Next(snapshot, &entry) == TRUE) { const WCHAR* wc = entry.szExeFile ; _bstr_t b(wc); const char* c = b; if (stricmp(c, pName.toStdString().c_str()) == 0) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); qDebug() <<"myPID: "<< myPID << "entry.th32ProcessID" << entry.th32ProcessID; if(myPID != entry.th32ProcessID) TerminateProcess(hProcess,0); QThread::msleep(10); CloseHandle(hProcess); } } } CloseHandle(snapshot);
It seemed complexed for me. Is it more easily by using the boost process module or qt module(I am not very good at the process of the system)?
And the code is not designed for the cross platform usage.If I moved the project to mac os or android or something else,I have to re-coding. ^.^!@nicker-player
The code you show would be Windows-only. For cross-platform you will need something which only uses Qt calls.For the risk of a process "crashing" and leaving the "lock" mechanism still "active" you would have to look at each suggestion. The very first example in the first link I gave uses
QLockFile
. There is a description in the docs at https://doc.qt.io/qt-6/qlockfile.html#details ("If the process holding the lock crashes, the lock file stays on disk..."), don't know how robust/comprehensive that is.I think it's better of the way by using of sharememory or qprocess .Dosent it?
IIRC, under Linux at minimum shared memory locks remain when a process crashes, so it's no better. And whatever you have in mind about
QProcess
does not sound cross-platform. My guess is that there is no 100% robust way of doing this which works cross-platform ---QLockFile
would have implemented it if there were a rock-solid way, instead of discussing workarounds for crash scenarios.