Click here to Skip to main content
11,721,289 members (87,307 online)
Click here to Skip to main content

A service that displays an icon in the system tray

, 17 Jan 2000 165K 4.7K 48
Rate this:
Please Sign up or sign in to vote.
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
  • <!-- Article Starts -->

    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

    Share

    About the Author

    Bruno Vais
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

     
    Generalcreateinstance Pin
    Sukanta Kumar Dash26-Jan-03 21:02
    memberSukanta Kumar Dash26-Jan-03 21:02 
    GeneralRe: createinstance Pin
    rana7421-Aug-06 21:04
    memberrana7421-Aug-06 21:04 
    GeneralPrivileges Pin
    bruno leclerc2-Dec-02 22:10
    memberbruno leclerc2-Dec-02 22:10 
    If i want do create the service with a privileged domain user account,
    it can't interact with the desktop.
    how can we solve this ?
    GeneralI have problem of Interactive Service Pin
    Anonymous1-Sep-02 15:29
    sussAnonymous1-Sep-02 15:29 
    GeneralRe: I have problem of Interactive Service Pin
    Anonymous23-Oct-02 7:05
    sussAnonymous23-Oct-02 7:05 
    GeneralRe: I have problem of Interactive Service Pin
    Peter Husemann23-Jan-04 4:19
    memberPeter Husemann23-Jan-04 4:19 
    GeneralRe: I have problem of Interactive Service Pin
    skjacob28-Sep-05 1:56
    memberskjacob28-Sep-05 1:56 
    GeneralIcon did not appear!! Pin
    Anonymous24-Jul-02 1:48
    sussAnonymous24-Jul-02 1:48 
    GeneralRe: Icon did not appear!! Pin
    Khumpty15-Jul-03 9:13
    memberKhumpty15-Jul-03 9:13 
    GeneralCreateProcess Pin
    Sarun8-Mar-02 20:13
    memberSarun8-Mar-02 20:13 
    GeneralRe: CreateProcess Pin
    Tamer Mash29-Apr-03 2:23
    sussTamer Mash29-Apr-03 2:23 
    AnswerRe: CreateProcess Pin
    ProfessorF7-Oct-07 13:31
    memberProfessorF7-Oct-07 13:31 
    GeneralDo i have to remove the icon when the user logs out Pin
    WeberMenschi20-Dec-01 9:24
    memberWeberMenschi20-Dec-01 9:24 
    GeneralRe: Do i have to remove the icon when the user logs out Pin
    Tamer Mash29-Apr-03 2:29
    sussTamer Mash29-Apr-03 2:29 
    GeneralAdd icon on "Taskbar Creation" Pin
    Derius1-Nov-01 11:33
    memberDerius1-Nov-01 11:33 
    GeneralRe: Add icon on "Taskbar Creation" Pin
    Paul Hunter22-Nov-01 23:18
    memberPaul Hunter22-Nov-01 23:18 
    GeneralShow Correct 16x16 icon in Tray Pin
    Anonymous5-Sep-01 3:58
    memberAnonymous5-Sep-01 3:58 
    GeneralPopup menu bugs... Pin
    Protopopov Gleb20-Aug-01 22:46
    memberProtopopov Gleb20-Aug-01 22:46 
    GeneralAPPLICATIONvnc Pin
    gboskin13-Jul-01 5:49
    membergboskin13-Jul-01 5:49 
    GeneralRe: APPLICATIONvnc Pin
    Carlos Antollini13-Jul-01 6:11
    memberCarlos Antollini13-Jul-01 6:11 
    QuestionWhy is my window popping up? Pin
    Alexander Boczar3-Jul-01 7:48
    memberAlexander Boczar3-Jul-01 7:48 
    AnswerRe: Why is my window popping up? Pin
    Anonymous29-Apr-03 2:08
    sussAnonymous29-Apr-03 2:08 
    AnswerRe: Why is my window popping up? Pin
    Anonymous29-Apr-03 2:09
    sussAnonymous29-Apr-03 2:09 
    GeneralPopup-Error Pin
    Bo Hansen28-Jul-00 3:06
    sussBo Hansen28-Jul-00 3:06 
    GeneralRe: Popup-Error Pin
    Greven31-Oct-01 7:52
    memberGreven31-Oct-01 7:52 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150901.1 | Last Updated 18 Jan 2000
    Article Copyright 2000 by Bruno Vais
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid