Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent user open the majar execution more than twice?
Forum Updated to NodeBB v4.3 + New Features

How to prevent user open the majar execution more than twice?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 278 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • nicker playerN nicker player

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 playerN 1 Reply Last reply
      2
      • JonBJ JonB

        @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 playerN Offline
        nicker playerN Offline
        nicker player
        wrote on last edited by nicker player
        #3

        @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. ^.^!

        JonBJ 1 Reply Last reply
        0
        • nicker playerN nicker player

          @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. ^.^!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved