Click here to Skip to main content
13,345,497 members (49,807 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 17 Jan 2000

A service that displays an icon in the system tray

, 17 Jan 2000
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:


    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.


    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


    About the Author

    Bruno Vais
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    QuestionHow about terminal services? Pin
    z161669-Feb-06 16:21
    memberz161669-Feb-06 16:21 
    GeneralOffical way to add a description to a service Pin
    napalm2k10-Sep-05 11:26
    membernapalm2k10-Sep-05 11:26 
    GeneralCannot Get System Started Automatically Pin
    enjewneer3-Dec-04 3:34
    memberenjewneer3-Dec-04 3:34 
    Generalremoving args Pin
    spicy_kid200018-Mar-04 17:38
    memberspicy_kid200018-Mar-04 17:38 
    Generalrunning my routine Pin
    rapace29-Oct-03 14:24
    memberrapace29-Oct-03 14:24 
    QuestionWhy I can't modify the icon? Pin
    akingnika5-Sep-03 2:20
    memberakingnika5-Sep-03 2:20 
    AnswerRe: Why I can't modify the icon? Pin
    akingnika7-Sep-03 17:39
    memberakingnika7-Sep-03 17:39 
    GeneralNot a good practice (in general) Pin
    Sardaukar18-Aug-03 23:07
    memberSardaukar18-Aug-03 23:07 
    I don't think a service itself must expose user interface facilities.

    What I think: a service should remain what is - a console program having absolutely no user interface.
    The "subclass" of services interacting with desktop can use UI features - as system tray icons.
    However, I don't think this is a good idea.

    Arguments/my ideas against GUI in services (and other things).

    A service package must be composed from at least 3 files (assuming there is only one file per functionality):

    1. [no GUI] the service(s)
    No GUI, just exports 1 or more services, implements init/startup, terminate/shutdown, dispatcher + control codes.
    The "core" functionality.
    If the executable is large, it may be useful to split it into core executable to do the minimum required and implement the functionalities in dlls (as you can in Windows NT, there is no big csrss.exe or lsass.exe, but rather small starters csrss.exe/lsass.exe and the servers implemented as csrsrv.dll and lsasrv.dll).
    This may go on in other such "splits" - if you need MAPI services, implement them in xxmapisrv.dll, if you need Internet, use xxinetsrv.dll etc.
    Try to keep the executable itself as small as possible, and do not load all at startup unless are you sure is does not take much time. I think is a good practice to finish the service initialization (and expecially the termination...) as soon as possible.

    2. [GUI] the service controller (if more than start/stop/pause/continue is needed)
    This can be a Control Panel, a .mmc application, or a separate executable. This applet/application will be executed on demand (usually as a post-installation task), and can configure the service (grant rights, set paths, command line parameters etc).

    3. [GUI] the service notification application
    This can be a light modified version of service controller - perform the basic operations start/stop/pause/continue/restart), maybe set some simple parameters (or expose an option to launch the controller for finetune), but the central point here is that: it is executed on user logon and displays the tray icon. This won't be a service, but rather an application executed on startup.

    These 3+ components can share the same source code tree, using macros to get the binaries. It is not pleasant to have 3 or more projects, each one with Win32 Debug, Win32 Release and ANSI/MBCS/Unicode configurations (a small calculation gives me 18 configurations by now Cry | :(( ) - but I think it's ok.

    Optional (but highly recommended):
    4. documentation and help file(s) + internationalization (separate resource dlls)
    5. error reporting utility
    6. an event log application "filter" to display only the events reported from the service(s).
    7. other utilities, varying from service to service: files/databases consistence/corruption checkers (example: Exchange's eseutil.exe), user/user rights checkings (deleted/invalid users etc.) - use your imagination... Smile | :)
    7. anything else one may consider useful.

    Of course, this may vary from project to project.
    It may depend also on other factors:
    - using of device drivers (this probably open another discussion even larger that this...)
    - platform(s) supported: if you plan all from Win95 to 2003, then ...
    - additional components required for various reasons (provided by you or needed for certain features to work): system-wide hooks, custom GINAs, need to programatically install networking (I think one may qoute at least 50% of MSDN here...)

    And, finally, about language/libraries - not a flame war, folks Smile | :) :


    Of course, there are, C, C++, VB, Java, Python, Perl ... your choice.
    Personally, I didn't see any source code of a good service written in anything else than C.
    And I don't mean the quality of code, but rather the limitation of languages.
    A perfect service written in Python or VB can suffer severe impacts because language is interpreted, so another executable - usually a medium/large one - should be executed; this executable needs another references, dlls, maybe runtime components installed etc.

    (still with me? Smile | :) )

    Another thing that I must admit I'm totally against: use of MFC or VB - at least for user interface.
    To load the VB runtime or MFC library - both libraries having more than 1 Mb - just to display a dialog with 2-3 buttons and several controls seems too much for me. After all, MFC and VB are for productivity - applications with hundreds of forms and thousand of classes, with more developers working on. But for something that can be done in a single .c file, with almost no resources, calling up to 5 registry functions and SCM calls? I think to implement a callback window/dialog procedure and several small functions pays the price.
    GeneralRe: Not a good practice (in general) Pin
    milkyhonglee12-Mar-06 6:34
    membermilkyhonglee12-Mar-06 6:34 
    Generalcreateinstance Pin
    Sukanta Kumar Dash26-Jan-03 22:02
    memberSukanta Kumar Dash26-Jan-03 22:02 
    GeneralRe: createinstance Pin
    rana7421-Aug-06 22:04
    memberrana7421-Aug-06 22:04 
    GeneralPrivileges Pin
    bruno leclerc2-Dec-02 23:10
    memberbruno leclerc2-Dec-02 23:10 
    GeneralI have problem of Interactive Service Pin
    Anonymous1-Sep-02 16:29
    sussAnonymous1-Sep-02 16:29 
    GeneralRe: I have problem of Interactive Service Pin
    Anonymous23-Oct-02 8:05
    sussAnonymous23-Oct-02 8:05 
    GeneralRe: I have problem of Interactive Service Pin
    Peter Husemann23-Jan-04 5:19
    memberPeter Husemann23-Jan-04 5:19 
    GeneralRe: I have problem of Interactive Service Pin
    skjacob28-Sep-05 2:56
    memberskjacob28-Sep-05 2:56 
    GeneralIcon did not appear!! Pin
    Anonymous24-Jul-02 2:48
    sussAnonymous24-Jul-02 2:48 
    GeneralRe: Icon did not appear!! Pin
    Khumpty15-Jul-03 10:13
    memberKhumpty15-Jul-03 10:13 
    GeneralCreateProcess Pin
    Sarun8-Mar-02 21:13
    memberSarun8-Mar-02 21:13 
    GeneralRe: CreateProcess Pin
    Tamer Mash29-Apr-03 3:23
    sussTamer Mash29-Apr-03 3:23 
    AnswerRe: CreateProcess Pin
    ProfessorF7-Oct-07 14:31
    memberProfessorF7-Oct-07 14:31 
    GeneralDo i have to remove the icon when the user logs out Pin
    WeberMenschi20-Dec-01 10:24
    memberWeberMenschi20-Dec-01 10:24 
    GeneralRe: Do i have to remove the icon when the user logs out Pin
    Tamer Mash29-Apr-03 3:29
    sussTamer Mash29-Apr-03 3:29 
    GeneralAdd icon on "Taskbar Creation" Pin
    Derius1-Nov-01 12:33
    memberDerius1-Nov-01 12:33 
    GeneralRe: Add icon on "Taskbar Creation" Pin
    Paul Hunter23-Nov-01 0:18
    memberPaul Hunter23-Nov-01 0: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.

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