Shell_NotifyIcon fails with ERROR_FILE_NOT_FOUND on Vista Beta II

Posted on 2006-06-07
Last Modified: 2008-01-09
Hi all. We are in the process of testing our application on Windows Vista Beta II to find portability issues. The program is a Windows Service that runs with the System Account user and has access to the desktop. When the service tries to install its icon in the Notification Area, the Shell_NotifyIcon function fails with ERROR_FILE_NOT_FOUND.

I know Vista has increased security compared to XP, and I'm pretty sure it has something to do with it. The only solution I came up with is to make another program that handles the tray messages and dispatches the commands to the service thru a named pipe. I would like to avoid this solution if possible.

Here's the code I use :

// test_tray.cpp
#include "stdafx.h"
#include "shellapi.h"
#include "resource.h"
#include "ntservice.h"
#include <misc/trace.h>

#define SERVICE_NAME                "CRAService"

using namespace TELUS::Angel::Misc;

bool  RegisterWindow();
void WINAPI Run(DWORD argc, char* argv[]);
HWND                              ghWnd = NULL;

NtService               gobjService;
UINT                              gMsg = 0;

void WINAPI Run(DWORD argc, char* argv[])
      NOTIFYICONDATA            mNiData = {0};
      bool                              isSuccess = false;

      TRACE_INIT_PROCESS("C:\\tray_tester.log", Trace::FullTrace);

   gobjService.setStatus(SERVICE_START_PENDING, 0, 30);

      gMsg = RegisterWindowMessage("AlsNotifyCommand");
      if (!gMsg)
      // Create the Msg Window
      if (isSuccess = RegisterWindow())
            ghWnd = CreateWindow("AlsMsgWndClass", "", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
            isSuccess = ghWnd != NULL;
            if (!isSuccess)
      // Add Tray Icon
      if (isSuccess)
            mNiData.cbSize = sizeof(NOTIFYICONDATA);
            mNiData.uID = IDI_NOTIFY_ICON;
            mNiData.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
            mNiData.hIcon =
                  (HICON)LoadImage( GetModuleHandle(NULL),

            if (!mNiData.hIcon)
            strcpy(mNiData.szTip, "test tip");
            mNiData.hWnd = ghWnd;
            mNiData.uCallbackMessage = gMsg;

            if (!Shell_NotifyIcon(NIM_ADD, &mNiData))
                  DWORD dw = GetLastError();
                  TRACE_ERROR("Could not display ICON");
                  TRACE_SYS_CALL_CODE("Shell_NotifyIcon", dw);

      MSG msg;
      while (GetMessage(&msg, NULL, 0, 0))

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
      int wmId, wmEvent;
      HMENU gMenu, gSubMenu;
      NOTIFYICONDATA            mNiData = {0};

      if (message == gMsg)
            if (lParam == WM_RBUTTONUP)
                  POINT pos = {0};
                  gMenu = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_MENU_ICON));

                        gSubMenu = GetSubMenu(gMenu, 0);

                        TrackPopupMenu(gSubMenu, TPM_RIGHTALIGN, pos.x, pos.y, 0, ghWnd, NULL);
      else if (message == WM_COMMAND)
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Analyse les sélections de menu :
            switch (wmId)
            case ID_POPUP_2:
                  mNiData.cbSize = sizeof(NOTIFYICONDATA);
                  mNiData.uID = IDI_NOTIFY_ICON;
                  mNiData.hWnd = ghWnd;
                  Shell_NotifyIcon(NIM_DELETE, &mNiData);
                  return DefWindowProc(hWnd, message, wParam, lParam);
      else if (message == WM_DESTROY)
            return DefWindowProc(hWnd, message, wParam, lParam);

      return 0;

bool RegisterWindow()
      bool isSuccess = false;

      WNDCLASSEX wcx = {0};                  = CS_HREDRAW | CS_VREDRAW;
   wcx.cbSize = sizeof(wcx);          // size of structure
   wcx.lpfnWndProc = MainWndProc;     // points to window procedure
   wcx.hInstance = GetModuleHandle(NULL);
   wcx.hIcon = NULL;
   wcx.hCursor = NULL;
   wcx.lpszClassName = "AlsMsgWndClass";  // name of window class

   isSuccess = RegisterClassEx(&wcx) != 0;

      if (!isSuccess)

      TRACE_END_RET(isSuccess, isSuccess);
      return isSuccess;

int main(int argc, char* argv[])
      NtService::startDispatcher(SERVICE_NAME, Run);
      return 0;
Question by:Ashron
    1 Comment
    LVL 3

    Accepted Solution

    This is because in Vista, Services run in a different session to conventional applications, so they have no access to the desktop or any of it's components. Most, if not all conventional UI calls will fail.

    Have a look at:

    This contains a description of what's happening together with some possible workarounds.



    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now