Click here to Skip to main content
Click here to Skip to main content

A service that displays an icon in the system tray

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

    About the Author

    Bruno Vais
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralRe: I have problem of Interactive ServicememberPeter Husemann23 Jan '04 - 4:19 
    See Page "Logoff Events" under "Window Stations and Desktops" in the MSDN Library.
    GeneralRe: I have problem of Interactive Servicememberskjacob28 Sep '05 - 1:56 
    Any solution for this???
    GeneralIcon did not appear!!sussAnonymous24 Jul '02 - 1:48 
    when I install iconservice Nothing happen!!!!!!!
    GeneralRe: Icon did not appear!!memberKhumpty15 Jul '03 - 9:13 
    Me either..what am i doing wrong?!Confused | :confused:
    GeneralCreateProcessmemberSarun8 Mar '02 - 20:13 
    I would like to execute a ".jar" file in Start Service, i used CreateProcess Method but it didn't executed..but if i'm giving a ".exe" file instead of ".jar" file then the command is executed properly and window is displayed..Please advice
    GeneralRe: CreateProcesssussTamer Mash29 Apr '03 - 2:23 
    You need to use ShellExecute() with "open" for the verd in order to open a non-exe file with its associated program.
    AnswerRe: CreateProcessmemberProfessorF7 Oct '07 - 13:31 
    I think, you just need to create process like this:
    program: "java"
    parameters: "your/path/and/the.jar"
    GeneralDo i have to remove the icon when the user logs outmemberWeberMenschi20 Dec '01 - 9:24 
    He folks!
     
    Thanks a lot for that article! Rose | [Rose]
     
    I developed a service, which should have a system tray icon, after the user logged in. Now i know what i have to do, in order to achieve it.
    I will register the windows message "TaskbarCreated" and then add my icon.
     
    I know that i have to call "Shell_NotifyIcon(NIM_DELETE, &tnid);", when the user stopps/deletes the service.
     
    BUT what do i have to do, to unregister my icon, when the user logs out again?? Confused | :confused:
    GeneralRe: Do i have to remove the icon when the user logs outsussTamer Mash29 Apr '03 - 2:29 
    You do not have to remove the icon when the user logs off; however, you need to re-create it when the user loggs back on. You can detect that by handling the registered window message "TaskbarCreated". This message is broadcasted when the taskbar is created.
    GeneralAdd icon on "Taskbar Creation"memberDerius1 Nov '01 - 11:33 
    A very useful addition to this example would be to add the functionality for the service icon to be added to the window on startup. You can find an example in the MSDN Library about "Taskbar Creation Notification". This would be ideal for any type of service with an icon in the taskbar area. Here is the excerpt from the library
     
    .
    .
    .
     
    Taskbar Creation Notification
    -----------------------------
    With Microsoft® Internet Explorer 4.0 and later, the Shell notifies applications that the taskbar has been created. When the taskbar is created, it registers a message with the TaskbarCreated string and then broadcasts this message to all top-level windows. When your taskbar application receives this message, it should assume that any taskbar icons it added have been removed and add them again. This feature generally applies only to services that are already running when the Shell begins execution. The following example shows a very simplified method for handling this case.
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
    {
    static UINT s_uTaskbarRestart;
     
    switch(uMessage)
    {
    case WM_CREATE:
    s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
    break;

    default:
    if(uMessage == s_uTaskbarRestart)
    AddTaskbarIcons();
    break;
    }
    return DefWindowProc(hWnd, uMessage, wParam, lParam);
    }
     
    .
    .
    .
     

    Hope this is useful. I am using it in the projects that I am working on to allow the user to interact with the service (based on their user privileges) Smile | :)
     
    Derius

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

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