views:

830

answers:

4

I need to enumerate all running applications. In particular, all top windows. And for every window I need to add my custom item to the system menu of that window.

How can I accomplish that in C++?

Update.

I would be more than happy to have a solution for Windows, MacOS, and Ubuntu (though, I'm not sure if MacOS and Ubuntu have such thing as 'system menu').

+1  A: 

Once you have another window's top level handle, you may be able to call GetMenu() to retrieve the Window's system menu and then modify it, eg:

HMENU hMenu = GetMenu(hwndNext);
Chris
I think you meant GetSystemMenu, not GetMenu; see my reply.
efotinis
A: 

You can use EnumWindows() to enumerate top level Windows.

I don't have a specific answer for the second part of your question, but if you subclass the window, I imagine you can modify the system menu.

EDIT: or do what Chris said: call GetMenu()

Ferruccio
A: 

Re: the update - please note that not even Microsoft Windows requires windows to have a sytem menu. GetMenu( ) may return 0. You'll need to intercept window creation as well, because each new top window presumably needs it too.

Also, what you propose is rather intrusive to other applications. How are you going to ensure they don't break when you modify their menus? And how are you going to ensure you suppress the messages? In particular, how will you ensure you intercept them before anyone else sees them? To quote Raymond Chen, imagine what happens if two programs would try that.

MSalters
+2  A: 

For Windows, another way to get the top-level windows (besides EnumWindows, which uses a callback) is to get the first child of the desktop and then retrieve all its siblings:

HWND wnd = GetWindow(GetDesktopWindow(), GW_CHILD);
while (wnd) {
    // handle 'wnd' here
    // ...
    wnd = GetNextWindow(wnd, GW_HWNDNEXT);
}

As for getting the system menu, use the GetSystemMenu function, with FALSE as the second argument. The GetMenu mentioned in the other answers returns the normal window menu.

Note, however, that while adding a custom menu item to a foreign process's window is easy, responding to the selection of that item is a bit tricky. You'll either have to inject some code to the process in order to be able to subclass the window, or install a global hook (probably a WH_GETMESSAGE or WH_CBT type) to monitor WM_SYSCOMMAND messages.

efotinis