Click here to Skip to main content
13,295,178 members (56,573 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 8 Sep 2008

A Windows Service Which Kills Unwanted Windows: Part 2

, 8 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Shows how to kill unwanted windows on the user desktop on Vista, using COM+ to allow a Windows service to interact with user applications.


This is Part 2 of my previous article. The main goal of this project is to create a Windows service in C# which will monitor all the appearing windows and kill the unwanted ones. After Part 1 has been published, I found (thanks to the forum attached to that article) that it does not work on Vista because of the enhanced security there. That means, Windows services are running in the hidden session #0, and they cannot interact with user sessions (#1, #2, ...). All PInvoke methods, such as SendMessage, do not work with windows/processes running on user desktops. After some search on CodeProject, I found a great article by Stefan Repas which shows how to allow a Windows service written in .NET to communicate with desktop applications using an out-of-proc COM server. To run a .NET COM-visible assembly as an out-of-proc COM server, he used COMAdmin.dll to register the DLL as an out-of-proc COM server.


Any COM visible assembly (or .dll) created in .NET using Visual Studio is hosted by dllhost.exe, which is called a "COM Surrogate". By default, it runs on Vista in session #0, which has its User Name as SYSTEM in the Task Manager. A Windows service can communicate with this COM server, but this server runs in the same session #0 as the service, and it does not see the user windows/applications. To force a COM DLL to be hosted in dllhost.exe, which is running in a user session, we have to register it under a user account using COMAdmin.dll. I created a class DesktopManager which has a rudimentary interface, to work with desktop windows/applications. Of course, this simple class could be easily extended to whatever you need. This class is implemented as a COM+ object in the DesktopManagerDll assembly registered as an out-of-proc COM server using COMAdmin.dll, and runs under a user account. All methods from the WindowFinder class (see my previous article) are moved to the DesktopManager class.

Desktop Manager

This COM object exposes the following IDesktopManager interface:

[Description("Exposed DesktopManager interface")]
public interface IDesktopManager
  int FindTopWindow(string wndClass, string wndTitle);

  int FindChildWindow(int hwndParent, string wndClass, string wndTitle);

  bool CloseWindow(int hwnd, string wndClass);

  void ExitProcess(string processToExit);

  void MessageBoxShow(string text, string caption, string icon);

  string GetMainWindowTitle(string process);

This interface is implemented in the DesktopManager class:

[Description("ServicedComponenet class to control IDesktopManager Interface")]
public class DesktopManager : ServicedComponent, IDesktopManager

Notice that this class is derived from the ServicedComponent class which is needed to expose the DesktopManager as a COM+ object. This object implements all the IDesktopManager interface methods. To implement the FindChildWindow method, for example, this class uses the same technique as was used in the WindowFinder class from my previous article.

To make this object an out-of-proc COM+ server, there is a class DesktopManagerInstaller which performs the following steps:

  1. Install the assembly using RegistrationHelper.
  2. Create an ICOMAdminCatalog object.
  3. Get all the roles for the current COM+ application.
  4. Find the role AverageUser.
  5. Assign accounts to the found AverageUser role.
  6. Get all COM+ components for the current application.
  7. Check if the DesktopManager COM+ component exists.
  8. Grant access to users in the role AverageUser

Using the DesktopManager COM+ object

First, you have to create a DesktopManager object:

m_desktopManager = new DesktopManagerDll.DesktopManager();
// this will instantiate the DesktopManager

Second, use m_desktopManager in the WindowsFinder class:

m_windowFinder.KillPopupWindow(m_titleToKill, classToKill);


public bool KillPopupWindow(string titleToKill, PopupWindowType popupType)
   m_hwndFound = FindTopWindow(classToKill, titleToKill);
   if (m_hwndFound != IntPtr.Zero)
     return m_iDesktopManager.CloseWindow(m_hwndFound.ToInt32(), classToKill);

Third, release m_desktopManager when done:


Finally, shutdown the COM+ application:

ICOMAdminCatalog cac = (ICOMAdminCatalog)Activator.CreateInstance(


To install this service, you can use the MS Installer output produced by the WindowKillerSetup project which is a part of the WindowKiller solution, or do manual registration of the two assemblies: DesktopManagerDll and WIndowKiller.exe, using InstallUtil, which is a part of the .NET Framework:

InstallUtil.exe DesktopManagerDll.dll
InstallUtil.exe WindowKiller.exe

Note: The WindowKiller EXE has implemented a self-installing feature - see my previous article. So, you can install/uninstall this service by calling it from the command line, like this...

WindowKiller -a

... where the parameter -a means "auto" install/uninstall depending on the current service status. To get help on the other command line parameters, you can call the service with the -h parameter.


This is Part 2 of my previous article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Intel Corp.
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWill This Also Work In A Win 7 Environment? Pin
William Clayton24-Jan-11 10:58
memberWilliam Clayton24-Jan-11 10:58 
GeneralAnother approach Pin
Aelthegrin16-Sep-08 3:44
memberAelthegrin16-Sep-08 3:44 
GeneralWindows XP support Pin
Decipator9-Sep-08 12:12
memberDecipator9-Sep-08 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 9 Sep 2008
Article Copyright 2008 by Victuar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid