Question

I made a GUI application that must run in my DELL server to send queries for 24 hours.

In case where the application is stopped by random users, or whatever it is, I created a service program that detects when it stops running, and executes it again.

The problem is, in service, FindWindow() doesn't properly work (always returns nullptr) because Microsoft changed its OS service policies since XP. And my service program has no way to find if the program is on the process list or not.

I found some solutions on the internet, which is to "allow service to interact with desktop on service panel" but since it was a long time ago so doesn't quite fit into the current OS version.

Should I use IPC instead? or any other ways to fix?

I believe that there has got to be a way to do this, because executing a process from service is also possible by using CreateProcessAsUser().

Any advice will be truly appreciated.

Thanks in advance.

Était-ce utile?

La solution

So I did what Remy Lebeau suggessted for me, and it properly works in Windows 7, and 2008.

Here's how I went step by step.

  1. Create a named mutex in the global namespace in the GUI application.

    ::CreateMutex(nullptr, false, L"Global\\MyMutex");
    
  2. Check periodically if the mutex has disappeared or not by using CreateMutex(), and do not forget to take care of the reference count to the handle.

    HANDLE hDetector = ::CreateMutex(nullptr, false, L"Global\\MyMutex");
    
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
            // The GUI application is still running.
            // ...  
    
            ::CloseHandle(hDetector); 
    }
    else
    {
            // The GUI application is not running.
            // ...
    
            ::CloseHandle(hDetector);
    }
    
  3. See it work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top