Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

A service that displays an icon in the system tray

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
17 Jan 2000 194.1K   5.2K   49   36
This article demonstrates a service that uses the system tray to interact with the user.
  • Download demo executable - 7 Kb
  • Download source files - 13 Kb
  • 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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionHow about terminal services? Pin
    z161669-Feb-06 15:21
    z161669-Feb-06 15:21 
    GeneralOffical way to add a description to a service Pin
    napalm2k10-Sep-05 10:26
    napalm2k10-Sep-05 10:26 
    GeneralCannot Get System Started Automatically Pin
    enjewneer3-Dec-04 2:34
    enjewneer3-Dec-04 2:34 
    Generalremoving args Pin
    spicy_kid200018-Mar-04 16:38
    spicy_kid200018-Mar-04 16:38 
    Generalrunning my routine Pin
    rapace29-Oct-03 13:24
    rapace29-Oct-03 13:24 
    QuestionWhy I can't modify the icon? Pin
    akingnika5-Sep-03 1:20
    akingnika5-Sep-03 1:20 
    In the callback function DialogProc, I added the handle of the click a menuItem of the popup menu, I want to change to another icon when click the menuItem, but it doesn't work! Why?

    BOOL CALLBACK DialogProc( HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    switch (uMsg)
    {
    case WM_INITDIALOG:
    .......
    break;
    case WM_ICON_NOTIFY:
    .......
    break;
    case WM_COMMAND:
    if (LOWORD(wParam) == ID_POPUP_CLOSE) {
    ServiceStop();
    } else if (LOWORD(wParam) == ID_POPUP_FUNC_START) {
    NOTIFYICONDATA ndata;
    ndata.cbSize=sizeof(NOTIFYICONDATA);
    ndata.hWnd=hwndDlg;
    ndata.uID=2000;
    ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
    ndata.uCallbackMessage=WM_ICON_NOTIFY;
    strcpy(ndata.szTip,"Another Tips"); ndata.hIcon=::LoadIcon(NULL, MAKEINTRESOURCE(IDI_WINLOGO));
    Shell_NotifyIcon(NIM_MODIFY,&ndata);
    }
    break;
    }
    return false;
    }

    AnswerRe: Why I can't modify the icon? Pin
    akingnika7-Sep-03 16:39
    akingnika7-Sep-03 16:39 
    GeneralNot a good practice (in general) Pin
    Sardaukar18-Aug-03 22:07
    Sardaukar18-Aug-03 22:07 
    GeneralRe: Not a good practice (in general) Pin
    milkyhonglee12-Mar-06 5:34
    milkyhonglee12-Mar-06 5:34 
    Generalcreateinstance Pin
    Sukanta Kumar Dash26-Jan-03 21:02
    Sukanta Kumar Dash26-Jan-03 21:02 
    GeneralRe: createinstance Pin
    rana7421-Aug-06 21:04
    rana7421-Aug-06 21:04 
    GeneralPrivileges Pin
    bruno leclerc2-Dec-02 22:10
    bruno leclerc2-Dec-02 22:10 
    GeneralI have problem of Interactive Service Pin
    hoowaycn1-Sep-02 15:29
    hoowaycn1-Sep-02 15:29 
    GeneralRe: I have problem of Interactive Service Pin
    Anonymous23-Oct-02 7:05
    Anonymous23-Oct-02 7:05 
    GeneralRe: I have problem of Interactive Service Pin
    Peter Husemann23-Jan-04 4:19
    Peter Husemann23-Jan-04 4:19 
    GeneralRe: I have problem of Interactive Service Pin
    skjacob28-Sep-05 1:56
    skjacob28-Sep-05 1:56 
    GeneralIcon did not appear!! Pin
    Anonymous24-Jul-02 1:48
    Anonymous24-Jul-02 1:48 
    GeneralRe: Icon did not appear!! Pin
    Khumpty15-Jul-03 9:13
    Khumpty15-Jul-03 9:13 
    GeneralCreateProcess Pin
    Sarun8-Mar-02 20:13
    Sarun8-Mar-02 20:13 
    GeneralRe: CreateProcess Pin
    Member 827287129-Apr-03 2:23
    Member 827287129-Apr-03 2:23 
    AnswerRe: CreateProcess Pin
    ProfessorF7-Oct-07 13:31
    ProfessorF7-Oct-07 13:31 
    GeneralDo i have to remove the icon when the user logs out Pin
    20-Dec-01 9:24
    suss20-Dec-01 9:24 
    GeneralRe: Do i have to remove the icon when the user logs out Pin
    Member 827287129-Apr-03 2:29
    Member 827287129-Apr-03 2:29 
    GeneralAdd icon on "Taskbar Creation" Pin
    1-Nov-01 11:33
    suss1-Nov-01 11:33 
    GeneralRe: Add icon on "Taskbar Creation" Pin
    22-Nov-01 23:18
    suss22-Nov-01 23:18 

    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.