Icon display messed up

Just starting out? Need help? Post your questions and find answers here.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Icon display messed up

Post by tatanas »

Hi,

I'm adding icons to Menu items through "SHDefExtractIconW" function.
But sometimes the icons aren't what they should be.
I can't put the full code (too big), so here is the function and the call :

Code: Select all

Prototype.i ExtractArbitrarySizeIcon(pszIconFile.s, iIndex.i, uFlags.i, phiconLarge.i, phiconSmall.i, nIconSize.i)

Procedure MyCustomExtractIcon(file$, index, size)
	If OpenLibrary(0, "Shell32.dll")
		Protected hIcon
		Protected CustomIcon.ExtractArbitrarySizeIcon
	
		CustomIcon = GetFunction(0, "SHDefExtractIconW")
		CustomIcon(file$, index, 0, @hIcon, #Null, size)
		CloseLibrary(0)
		
		ProcedureReturn hIcon
	Else
		ProcedureReturn 0
	EndIf
EndProcedure

...
MenuItem(#contextMenu1, "example", MyCustomExtractIcon("shell32.dll", 55, 16))
...
Where do I make a mistake ?

Thanks for your time.
Last edited by tatanas on Thu Mar 21, 2024 9:10 am, edited 2 times in total.
Windows 10 Pro x64
PureBasic 6.20 x64
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Icon display messed up

Post by STARGÅTE »

tatanas wrote: Thu Mar 21, 2024 8:24 am I can't put the full code (too big), so here is the function and the call :
Where do I make a mistake ?
The prototype ExtractArbitrarySizeIcon is not defined, #contextMenu1 is not defined and the menu with number #contextMenu1 is not created.
At least you need a window to create a menu.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Icon display messed up

Post by tatanas »

Sorry I didn't put all the code but of course I wrote everything you've noticed.
(I added the prototype def in the first post)
Windows 10 Pro x64
PureBasic 6.20 x64
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Icon display messed up

Post by Fred »

Why not taking the time to write a small working snippet showing the issue ? It's hard guessing what's wrong when you can't test it.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Icon display messed up

Post by tatanas »

Because I can't reproduce the bug. It happens one time out of 50 maybe.
I was just wondering if the function and its use was right.
Windows 10 Pro x64
PureBasic 6.20 x64
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Icon display messed up

Post by BarryG »

Write a small snippet that does what you want to achieve, and when that works, compare it to your full other code and you'll spot your bug. I've done this so many times that it's not funny.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Icon display messed up

Post by tatanas »

Sorry but I'm going to repeat myself :
I was just wondering if the function and its use was right.
If there is no error with the use of this function, I will investigate other part of my code.
Windows 10 Pro x64
PureBasic 6.20 x64
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Icon display messed up

Post by fryquez »

The function is more or less right.

I would not use OpenLibrary() with hard coded number unless written for a short forum example code.
You do not keep track of the hIcon's you create, ideally you would free them if no longer needed.

However the problem is most likely with the Windows Icon cache:
I seeing messed up icons on some systems every couple of months, others never show these problems.
Axolotl
Addict
Addict
Posts: 873
Joined: Wed Dec 31, 2008 3:36 pm

Re: Icon display messed up

Post by Axolotl »

I have a few spontaneous questions about the function used:
What if the icons are not included as a 16x16Px version?
Why don't you check the return value for success?
--> you can use the available constants (#S_OK, #S_FALSE, #E_FAIL) for the possible return values here.
Perhaps the answers will help you to get to the bottom of the problem.

You can also use a different function to test the file (example shows dll, exe or ico are ok as well)

Code: Select all

NbIcons = 2
Dim hIcon(NbIcons)

Debug ExtractIconEx_(GetSystemDirectory()+"Shell32.dll", 130, 0, @hIcon(0), NbIcons)

For n = 0 To NbIcons
  Debug Str(n) + " = 0x" + Hex(hIcon(n)) 
Next 
; remark: You must destroy all icons extracted by ExtractIconEx by calling the DestroyIcon function. 

Another hint from MSDN:
To retrieve the dimensions of the large and small icons, use the GetSystemMetrics function with the SM_CXICON, SM_CYICON, SM_CXSMICON, and SM_CYSMICON flags.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Icon display messed up

Post by RASHAD »

Most of my problems comes from Time and Timing specially these days with SSD devices :)
Check that and give the CPU and other components time to breeze
Egypt my love
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Icon display messed up

Post by tatanas »

Thank you everyone for your tips.
I will add some error check inside the function.

RASHAD : I'm intrigued by your comment. Do you mean I should add some delay between each call of this function ?
Windows 10 Pro x64
PureBasic 6.20 x64
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Icon display messed up

Post by RASHAD »

Hi tatanas
It depends
Delay is not always the good solution
But you can use [While WindowEvent():Wend ] when needed
Be sure that every repeated process start on clean ground
Try and error is your answer :D
Egypt my love
Post Reply