I am trying to add an icon to taskbar status area using VC++ 4.2. I don't
get any erros but the function return 0 which indicates a error.
Here's the code I use to try this.
void CIconDlg::OnButton1()
{
NOTIFYICONDATA *notifyicondata;
char* msg = "msg";
HICON hIcon;
CIconDlg cIcondlg; //the current window
hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
notifyicondata->cbSize = sizeof( notifyicondata );
notifyicondata->hWnd = cIcondlg.m_hWnd;
notifyicondata->uID = NULL;
notifyicondata->uFlags = NIF_ICON || NIF_MESSAGE || NIF_TIP;
notifyicondata->uCallbackMessage = NULL;
notifyicondata->hIcon = hIcon;
lstrcpyn( notifyicondata->szTip, msg, sizeof( msg ) + 1 );
if( Shell_NotifyIcon( NIM_ADD, notifyicondata ) )
AfxMessageBox("YESSS");
else
AfxMessageBox("Snif, Snif");
}
any help or suggestions would be great !
Felipe Barbosa
> NOTIFYICONDATA *notifyicondata;
This is not pointing at anything, i.e. no memory is reserved other
than for the pointer itself. Better to declare the structure as an
automatic variable and manipulate it using dot notation:
NOTIFYICONDATA notifyicondata;
notifyicondata.cbSize = sizeof (notifyicondata);
..... etc .....
if (Shell_NotifyIcon (NIM_ADD, ¬ifyicondata))
or declare a separate pointer assigned to the object you have created
and use dereference notation:
NOTIFYICONDATA notifyicondata;
NOTIFYICONDATA * pNid = ¬ifyicondata;
pNid -> cbSize = sizeof (notifyicondata);
..... etc .....
if (Shell_NotifyIcon (NIM_ADD, pNid))
Given that you used an uninitialised pointer, I'm suprised this didn't
crash as soon as you used the -> operator.
> notifyicondata->uFlags = NIF_ICON || NIF_MESSAGE || NIF_TIP;
This is definitely wrong. You have used the logical or || not the
bitwise or |. Instead of producing the desired result (three bits set,
0x7), this will produce TRUE : 1 bit set, 0x1. This needs to be :
notifyicondata->uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
-------------
Two fairly minor additional points :
> notifyicondata->uID = NULL;
I don't know if zero is a legal ID. This ID is only intended for your
use, so I strongly suspect the value is immaterial, but don't know for
sure.
> notifyicondata->uCallbackMessage = NULL;
I'm presuming that this code is for test purposes and you don't intend
receiving messages as yet ?
Bob Moore [MVP]
> CIconDlg cIcondlg; //the current window
> notifyicondata->hWnd = cIcondlg.m_hWnd;
This window will not exist after your function exits, so how did you
intend to receive any messages ? Is this simply intended as a valid
HWND for testing purposes ?
Bob Moore [MVP]