A service that displays an icon in the system tray





4.00/5 (8 votes)
Jan 18, 2000

201071

5161
This article demonstrates a service that uses the system tray to interact with the user.
IconService is a Win32 console app that displays an icon in the system tray. The service can be installed/removed from the prompt: "IconService -install" , "IconService -remove", and started from the control panel (the "Services" icon). In order to display something from a service you must allow it to interact with the desktop. This can be done by specifying the SERVICE_INTERACTIVE_PROCESS switch when creating the service:
schService = CreateService( schSCManager, // SCManager database TEXT(SZSERVICENAME), // name of service TEXT(SZSERVICEDISPLAYNAME), // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS , // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier TEXT(SZDEPENDENCIES), // dependencies NULL, // LocalSystem account NULL); // no password
ServiceStart
creates an event used later for stopping the service, and a thread that is
responsible for the icon's parent. Here I use an old trick to prevent the dialog from
appearing in the task bar. First I create a modeless dialog with the WS_VISIBLE
not
checked:
HWND hwnd = CreateDialog(NULL, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);
and than I create the icon's parent:
DialogBox(NULL, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc);
To hide this one:
SetWindowPos(hwndDlg, NULL, -10,-10,0,0, SWP_NOZORDER|SWP_NOMOVE);
The DialogProc
is quite simple. It creates the icon, and on RBCLK it displays a menu to
stop the service. ServiceStop
sets the event created by ServiceStart
and deletes the icon.
The heart of the service is in the ServiceStart
function. So if you want your service
to actually do something after creating the icon, replace the WaitForSingleObject
with
something else.