Page 1 of 1

Tired of your tray icon vanishing when Explorer.exe dies?

Posted: Fri Feb 08, 2008 11:33 pm
by Rescator
( this is actually from my feature request post http://www.purebasic.fr/english/viewtopic.php?t=20712 )

It annoys the crap out of me when applications do not recreate their tray icons if explorer.exe should "die" and be restarted,
so to make your PureBasic program that much better than te others out there, use this:

Code: Select all

;Put this somewhere early in the app, along with the window creation
Global taskbarrestart.l
taskbarrestart=RegisterWindowMessage_("TaskbarCreated")
Now in your window callback add this:

Code: Select all

Procedure.l WindowCallback(hwnd,message,wparam,lparam)
Protected result.l=#PB_ProcessPureBasicEvents
 Select message
  Case taskbarrestart
   ;Tray icon recreate code here or maybe call a procedure that does this.
 EndSelect
ProcedureReturn result
Endprocedure
Obviously it makes most sense to only use this if you have tray icons in your program.
Also, older Windows does not support this message. (should do no harm though obviously since it's just a message).
I forgot which versions, but I'm guessing Window 2000 or later, maybe Win98 not sure. Vista however seems to need some extra privileges added to your program before you get that message, not sure about a solution there.

Posted: Sat Feb 09, 2008 10:59 am
by maw
Interestingly enough the code works on Vista if your program is not running as administrator, but if your program is running as administrator it doesn't work. So for all of us always running as administrators has a problem here. The reason for this is simple, if not very logical. Explorer is not running as administrator, and in Vista system messages like these are not allowed to be sent to higher privileged running applications. Yet another security measure gone wrong if you ask me...

So the trick is for your program to first detect if you're running Vista and then make a call to ChangeWindowMessageFilter, http://msdn2.microsoft.com/en-us/library/ms632675.aspx, and allow the message to be sent to your program. Be ware that ChangeWindowMessageFilter is a Vista only command of user32.dll..

Posted: Sat Feb 09, 2008 4:51 pm
by SFSxOI
I'm not so sure its a security measure gone wrong. In previous versions of windows when this happened it was possible for some malware, viruses, trojans, etc...to crash explorer on purpose then run something from the taskbar and gain administrative rights to the system if the user was running as either admin or just a regular user.

The solution to this was to completly divorce explorer from this possibility, this is the reason Vista does this. It keeps the previous security flaw from happening and closes yet another attack vector. People complained about security flaws in Windows, so they fix'd them, maybe not all, but a lot of them.

Posted: Sat Feb 09, 2008 8:07 pm
by Trond
It shouldn't be possible to crash explorer on purpose in the first place.

Posted: Sun Feb 10, 2008 12:30 am
by rsts
Not 'easy' maybe, but anything is possible.

cheers

Posted: Sun Feb 10, 2008 1:21 am
by PB
> It shouldn't be possible to crash explorer on purpose in the first place

It's easy to do it. Just terminate the explorer.exe process and you're done.

Posted: Sun Feb 10, 2008 2:07 am
by rsts
I believe Trond means it shouldn't be that easy. explorer should be fairly well protected.

cheers

Posted: Sun Feb 10, 2008 3:56 am
by PB
> explorer should be fairly well protected

Explorer is nothing special. It's just the visual part of Windows. In fact, you
can easily replace it totally with your own exe if you want. So, I very much
doubt Microsoft sees a need to protect it.

Re:

Posted: Sun Nov 06, 2011 3:10 am
by MachineCode
Got a problem... if I'm sending a large email (with PureSMTP) and explorer.exe has died or crashed during this time, then the callback event is NOT fired; meaning my exe's icon no longer appears in the system tray due to missing the "taskbarrestart" message. How would we work around that situation?

Re: Tired of your tray icon vanishing when Explorer.exe dies

Posted: Wed Nov 09, 2011 6:21 pm
by Zach
spawn a listening thread?

Re: Tired of your tray icon vanishing when Explorer.exe dies

Posted: Sat Nov 12, 2011 4:27 pm
by MachineCode
Thanks Zach, I'll have to try that. I don't see why it wouldn't work. :)

Re: Tired of your tray icon vanishing when Explorer.exe dies

Posted: Sun Dec 09, 2012 1:42 am
by Phantomas
So sorry for up this old thread, but, I have some question. This is correct code?

Code: Select all

EnableExplicit

Enumeration
  #window
  #icon
  #icon_image
EndEnumeration

#window_w = 320
#window_h = 60
#window_title = "My Window"

#icon_file = "path_to_icon.ico"

Define taskbarrestart.i

Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
  Shared taskbarrestart
  Select Message
    Case taskbarrestart
      AddSysTrayIcon(#icon, WindowID(#window), ImageID(#icon_image))
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(#window, #PB_Ignore, #PB_Ignore, #window_w, #window_h, #window_title, #PB_Window_ScreenCentered | #PB_Window_SystemMenu) And LoadImage(#icon_image, #icon_file)
  
  taskbarrestart = RegisterWindowMessage_("TaskbarCreated")
  AddSysTrayIcon(#icon, WindowID(#window), ImageID(#icon_image))
  SetWindowCallback(@MyWindowCallback())
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  ForEver
EndIf
This is work, but I use Shared for "transfer" taskbarrestart INT in MyWindowCallback() procedure, this is correct? Or is there another way?

Re: Tired of your tray icon vanishing when Explorer.exe dies

Posted: Sat Sep 20, 2014 6:37 pm
by ts-soft