Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Shell_NotifyIcon returns FALSE

Posted on 2004-11-22
25
Medium Priority
?
904 Views
Last Modified: 2008-01-09
Hi,

I have an application that runs as a Windows service and shows up in system tray. I am compiling the application in XP SP1 and everything works fine on XP. When I install this same exe on Windows 2000 server machine and the service is started, the exe runs but it never shows up in the system tray. I added some debug code and it seems that GetLastError after Shell_NotifyIcon is returning error 2, which is 'system cannot find the specified file'. Also when the same exe on Windows 2000 machine is run as a normal application (by double clicking), it appears fine in system tray. The service is set up to interact with the desktop.

Here is the code that adds the icon to system tray and it always returns false when run as service on W2K.

BOOL TaskBarAddIcon (HWND hWnd, UINT uID)
{
    BOOL bRet;
    NOTIFYICONDATA tnid;
    HANDLE hIcon = NULL;

    gCurrID = uID;
    hIcon = LoadIcon (hInst, MAKEINTRESOURCE (uID));
    tnid.cbSize = sizeof (NOTIFYICONDATA);
    tnid.hIcon = hIcon;
    tnid.hWnd = hWnd;
    lstrcpy (tnid.szTip, "My tip");
    tnid.uCallbackMessage = MY_TASK;
    tnid.uID = uID;
    tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    bRet = Shell_NotifyIcon (NIM_ADD, &tnid);
    if (hIcon) DestroyIcon (hIcon);
    return (bRet);
}

Do you have any suggestions?

fuzorblade64
0
Comment
Question by:fuzorblade64
  • 10
  • 10
21 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12648820
Are 'hIcon' and/or 'hInst' valid?
0
 

Author Comment

by:fuzorblade64
ID: 12649405
Yes, both hIcon and hWnd are non-null.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12649744
>>I have an application that runs as a Windows service and shows up in system tray.

If your service application loads up before a user logs on, there's no place to put the icon.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 12649772
To get your service to work with the trayicon, you should use a secondary application that starts up after a loggon.

You can then interface between the secondary and service application to display menu's and activate options.
0
 

Author Comment

by:fuzorblade64
ID: 12649775
Well, I am already logged on to the machine and I start the service manually. In this case it should show up.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12649816
>>Well, I am already logged on to the machine and I start the service manually. In this case it should show up.

I've tried this before myself, and it does not show up.
My application was also able to display the trayicon when I launched it directly, but when it launch via service mode, it also failed on Shell_NotifyIcon.

I didn't investigate any further, because I realize even if I some how got it to work while I was logged on, it would fail on reboot.

Do you need this to work on reboot?

If so, Shell_NotifyIcon will always fail on reboot for a service application.
0
 

Author Comment

by:fuzorblade64
ID: 12649952
The icon only matters when a user is logged in. So as long as it shows up in the system tray after a user logs in, I am fine. For this, I have a timer, which checks for the presence of the icon (by trying to modify it) and if it is not found, it adds it.
0
 

Author Comment

by:fuzorblade64
ID: 12650010
I also want to emphasize that this works perfectly on Windows XP, SP1 and SP2.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12650107
>>I also want to emphasize that this works perfectly on Windows XP, SP1 and SP2.

I have you tested it out on reboot for XP?
0
 

Author Comment

by:fuzorblade64
ID: 12650159
Yes, I have been using it on XP for a few weeks now. Every time I reboot and login, I see the icon in system tray because of that timer code mentioned earlier. Before I added that code, if I logged out and logged back in the icon used to be gone.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12651972
Check out the following link:
http://www.codeproject.com/shell/systemtray.asp?df=100

The above link claims that the CSystemTray class can work with service application and with Windows 2000.

I looked at some of the code, and I notice the code has Win2K specific logic, however you need the latest SDK loaded on your development computer in order to compile the code.

In the latest SDK the ShellAPI.h header has additional data members for NOTIFYICONDATA.

typedef struct _NOTIFYICONDATAA {
        DWORD cbSize;
        HWND hWnd;
        UINT uID;
        UINT uFlags;
        UINT uCallbackMessage;
        HICON hIcon;
#if (_WIN32_IE < 0x0500)
        CHAR   szTip[64];
#else
        CHAR   szTip[128];
#endif
#if (_WIN32_IE >= 0x0500)
        DWORD dwState;
        DWORD dwStateMask;
        CHAR   szInfo[256];
        union {
            UINT  uTimeout;
            UINT  uVersion;
        } DUMMYUNIONNAME;
        CHAR   szInfoTitle[64];
        DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0x600)
        GUID guidItem;
#endif
} NOTIFYICONDATAA, *PNOTIFYICONDATAA;

The CSystemTray class makes modifications to these additional data members.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12655618
FYI:
I just tried compileing above code at my current work site (which does NOT have the latest SDK), and it compiles, but without the W2K specific code logic.

The following macro in the above code will stop the W2K code logic from compiling if you don't have the latest SDK installed.

#ifdef NOTIFYICONDATA_V1_SIZE   // If NOTIFYICONDATA_V1_SIZE, then we can use fun stuff
#define SYSTEMTRAY_USEW2K
#else
#define NIIF_NONE 0
#endif
0
 

Author Comment

by:fuzorblade64
ID: 12658739
I just compiled TaskBardemo and installed it as service on W2K (with the help of srvany.exe and instsrv.exe) and it does not show up in the system tray.
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 12658824
>>I just compiled TaskBardemo and installed it as service on W2K (with the help of srvany.exe and instsrv.exe) and it does not show up in the system tray.

Do you have the latest SDK installed on your computer?
0
 

Author Comment

by:fuzorblade64
ID: 12659021
OK, I think an apology is in order. After I got too frustrated, I decided to log in as console user as opposed to remote desktop session and Voila! the icon shows up. This behavior is different from XP, where I have been logging thru remote desktop and icon always shows up properly.

I am extremely sorry for all the wasted cycles I have caused.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12659051
>>I am extremely sorry for all the wasted cycles I have caused.

Does it work with your old code, or only with the TaskBardemo code?
0
 

Author Comment

by:fuzorblade64
ID: 12659457
The old code works. One more point to clarify, it could be the server issue also. XP pro was working thru RDC and Windows Server 2000 was not. I haven't tried server 2003 or Windows 2000 pro. So it could be the difference between W2K and XP or server and professional version.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12659494
My target test platform is Windows 2000 Advance Server, and my original code does not work on it.

I haven't tried using TaskBardemo's code because I don't have the latest SDK on my client's development machine.
0
 

Author Comment

by:fuzorblade64
ID: 12948171
Does anybody know why the icon does not appear when connecting to W2K server thru terminal service. I see some of the icons in system tray even when I go into the server thru terminal service but not mine. What is required to be done for the icon to appear in all cases?
0
 

Author Comment

by:fuzorblade64
ID: 13195449
Okay, please close the question. My problem is not resolved though.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13195579
>>Okay, please close the question. My problem is not resolved though.
I was under the impression, that your main question was answered, and that your problem was related to logging on via terminal server.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

572 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