Home > Net >  Write a Windows service, mainwindowtitle unable to get the application main window is the main windo
Write a Windows service, mainwindowtitle unable to get the application main window is the main windo

Time:04-01

Today. Write a Windows service program, the function is to obtain the system running Process, and then through the Process to obtain the main window title, found that specify the main window title will shut down the program, I use the following code
 Process [] SysProcesses=Process. GetProcesses (); 
Foreach (Process Proc in SysProcesses)
{
If (Proc. MainWindowTitle. Length> 0)
{
If (Proc. MainWindowTitle. The Contains (" Chrome "))
{
Try
{
Proc. Kill ();
}
The catch (Exception error)
{
Enclosing AddTextLine (" closing failure ");
}
}
}
, but there is a problem, after debugging, I found the problem in Proc. MainWindowTitle, the service procedure in the process of execution, Proc. MainWindowTitle access to the main window title is empty, even if the procedures are the main window, still can't get to, then I will be in the program is inserted into the normal desktop, Proc. MainWindowTitle main window title will be able to get normal program, the great god, please help to explain the reasons, and what is the solution, thank you.

CodePudding user response:

Don't sink,,,, who is a great god help,,,

CodePudding user response:

Their access to the various data, I finally understand,
Myself to answer, appear unable to access the service is the root cause of the main window title of the session, from vista, the Windows service is running in session0, and the first login user application is running in session1, a second login user runs in session2, by analogy, that is a Windows service and user application is running in a different session, so I can't get to the application's main window title, there is no effective solution, I made a flexible, will be a program split into two, will detect the application of the main window title and close the application code separately compiled into an executable file 1. Exe, and then through the unmanaged code in a Windows service programs CreateProcessAsUser function session penetration, at a fixed time interval in the current activity session start 1. Exe, basic to achieve the expected purpose,

CodePudding user response:

Thank you very much for your advice! Finally found the reason, but my side considering the Session through the performance of the problem
  •  Tags:  
  • C#
  • Related