Random "Can't load icon" error using HICON:

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Random "Can't load icon" error using HICON:

10 Jan 2017, 15:29

Anyone know what would cause a random "Can't load icon" error when using the HICON:* method of assigning a tray icon?

Error message (.png attachments don't seem to be working):

Code: Select all

EitherMouse.exe
---------------------------
Error:  Can't load icon.
Specifically: HICON:*603392579
	Line#
--->	1203: Menu,Tray,Icon,HICON:*%hIcon%
The current thread will exit.
---------------------------
OK
Related code:

Code: Select all

 pBitmap := Gdip_CreateBitmapFromFile(A_ScriptFullPath,27,16)
 G := Gdip_GraphicsFromImage(pBitmap)
 pBrush := Gdip_BrushCreateSolid(TextColor)
 Gdip_TextToGraphics(G, Number, "x-1 y2 w20 h20 Center r4 s10 Bold c" pBrush, "Tahoma")
 Gdip_DeleteBrush(pBrush)
 hIcon := Gdip_CreateHICONFromBitmap(pBitmap)
 Gdip_DeleteGraphics(G)
 Gdip_DisposeImage(pBitmap)
 
 Menu, Tray, Icon, HICON:*%hIcon%
 
 DestroyIcon(hIcon)
I have been using this code for a while and haven't seen this error come up before, and haven't had it reported from EitherMouse users (though I usually leave #ErrStdOut in the releases so people might not ever see the error). Any thoughts? Fluke i shouldn't worry about?
Thanks
- Joel

PS: functionally what I'm doing here is taking an icon from resource, overlaying GDI+ text onto it and applying the icon to the tray (and gui controls) with HICON:*. I use the same technique in a little tray icon date tool and have never seen this error:

Code: Select all

SetTrayDate(TextColor=0xff222222)
{
 pBitmap := Gdip_CreateBitmapFromFile(A_ScriptFullPath,1,16)
 G := Gdip_GraphicsFromImage(pBitmap)
 pBrush := Gdip_BrushCreateSolid(TextColor)
 Gdip_TextToGraphics(G, A_MMM, "x-2 y-1 w20 h20 Center r4 s6       c" pBrush, "Tahoma")
 Gdip_TextToGraphics(G, A_DD+0,  "x-2 y3  w20 h20 Center r4 s10 Bold c" pBrush, "Tahoma")
 Gdip_DeleteBrush(pBrush)
 hIcon := Gdip_CreateHICONFromBitmap(pBitmap)
 Gdip_DeleteGraphics(G)
 Gdip_DisposeImage(pBitmap)
 Menu, Tray, Icon, HICON:*%hIcon%
 DestroyIcon(hIcon)
 Return 0
}
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Random "Can't load icon" error using HICON:

11 Jan 2017, 08:05

Hi Joel, I don't have a solution, unfortunately, but wanted to add that I have had a similar problem loading the tray with an image directly from file, using Menu, Tray, Icon, file.bmp so it may not be the HICON that's critical. I tried adding Sleep time between the prior saving of the file and its re-loading to the tray. At first I thought this fixed it, but I'm not sure, since it still happens from time to time, without signs that the disk I/O, CPU, memory, etc. are working hard. It's a while since it happened, but IIRC I get the same message with 'file.bmp' in place of the HICON reference.

Incidentally, I was searching for info on how to use the HICON instead of repeatedly accessing the file (and I'm working on a tray icon similar to your tray date one) so your scripts here helped a lot. :)
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Random "Can't load icon" error using HICON:

12 Jan 2017, 12:33

Thanks, that helps a little I guess knowing it can happen even without HICON:*

Glad its useful for you, in many cases you'd be better off doing the CreateBitmapFromFile(A_ScriptFullPath... only once, then creating bitmaps from that bitmap to edit, so you only read from disk once.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Random "Can't load icon" error using HICON:

30 Aug 2019, 01:55

bumping this to the top in case anyone knows, 2 and a half years later


I've had two users of EitherMouse send me screenshots of this error in the past few months. This time #ErrorStdout is on but the error message still comes up (this is probably as designed, but i was hoping this would at least hide the error from the user). I guess i can wrap the Menu, Tray, Icon command in a try catch block? still interested to know why this is happening

Error: Can't load icon.
Specifically: HICON:*603392579
Line#
---> 1203: Menu,Tray,Icon,HICON:*%hIcon%
The current thread will exit.

Code: Select all

 hIcon := Gdip_CreateHICONFromBitmap(pBitmap)
 Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap)
 Menu, Tray, Icon, hIcon:*%hIcon%
 GuiControl, 10:, TrayIconMenu, hIcon:*%hIcon%
 Menu, Icons, Icon, Number, hIcon:*%hIcon%
 Menu, Configure, Icon, Tray Icon:, hIcon:*%hIcon%
 DestroyIcon(hIcon)
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Random "Can't load icon" error using HICON:

30 Aug 2019, 02:53

because ure probably leaking gdi handles and hitting the 10k limit
lexikos
Posts: 9604
Joined: 30 Sep 2013, 04:07
Contact:

Re: Random "Can't load icon" error using HICON:

30 Aug 2019, 02:57

Menu,Tray,Icon,HICON:*%hIcon% gives the "Can't load icon" message if the internal LoadPicture call returns 0, which occurs if hIcon is 0 or the GDI CopyImage function fails. CopyImage is used to ensure the icon is a specific size (in this case LoadPicture is called once for each of the two standard icon sizes).

If you remove the asterisk, hIcon will be used directly, but will be deleted if/when the tray icon is changed. If you need to keep a copy of it, you can duplicate the icon handle and pass the duplicate without an asterisk.
This time #ErrorStdout is on but the error message still comes up (this is probably as designed, but i was hoping this would at least hide the error from the user).
#ErrorStdOut only applies to "syntax" errors. You can use try or Menu Tray, UseErrorLevel to suppress runtime errors raised by the Menu command.
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Random "Can't load icon" error using HICON:

30 Aug 2019, 08:47

thanks for the explanations guys...

I will use UseErrorLevel so that users don’t get a confusing error message, and I will investigate if i’m making too many copies of the icon and leaking memory/gdi objects (very likely)
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Random "Can't load icon" error using HICON:

30 Aug 2019, 09:11

If hIcon is a global variable, then interrupting threads could free/overwrite it before passed to the menu command.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 135 guests