I have an application that the user can start in two different ways, as a normal application or as a Windows service. When the service is already running (there’s a icon of it in the system tray) and the user tries to start the application (not just open by clicking on the tray icon, but start again clicking in Start>Applications>Blah blah), it says: “The application is already running as a Service, do you want to close the service and start the App?” When the user clicks ‘Yes’ the application closes the service and starts itself as a normal application.
When that occurs I put a flag indicating that the user had once stopped the service and when they close the application the software sees the flag and the service has to start running again, because it was closed before to start as a normal app.
I tried to put:
WinExec(PChar('NET START MyApplicationName'),SW_shownormal);
in the OnClose event, before this line:
ExitProcess(0);
but it says that the service is already running and closes the application without starting the service, and if I put it after that line it doesn’t do anything at all.
Is there any way to do this?
When I put the line before the ExitProcess(0); it opens a cmd window with the message:
“The service is not responding to the control function”
then it closes the application, closes the cmd window and it doesn’t start the service.
Write a simple Batch file like that
Then run execute this batch file before close your program.