Extract Icons using Private Method

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
Hello.

I've tried to extract icons from exe files, though this works well with ExtractAssociatedIcon(filePath), but it extracts only the 32 pixel-sized icons (16 pixel-sized are needed).

Now, clever as I am, I found another method called ExtractAssociatedIcon(filePath, index) which I could use to go through every icon in the exe file and find a fitting one...well, I could manage to overcome the burden that this is a private method using reflection:
VB.NET:
Dim iconGetCall As Reflection.MethodInfo = GetType(Icon).GetMethod("ExtractAssociatedIcon", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)

foundIcon = DirectCast(iconGetCall.Invoke(Nothing, New Object() {fromFile, index}), Icon)

But, this does return an icon object with a (valid, at least it seems so) handle and height/width set to 0. Means, no icon. Though, I don't understand why, since when it's called from ExtractAssociatedIcon(filePath) it works:
VB.NET:
Return Icon.ExtractAssociatedIcon(filePath, 0)

If I call it like this:
VB.NET:
DirectCast(iconGetCall.Invoke(Nothing, Reflection.BindingFlags.Static Or Reflection.BindingFlags.NonPublic, Nothing, New Object() {filePath, index}, Nothing), Icon)
It does return *some* icon (most likely 32 pixel-sized) from the file, but that's always returned, no matter to what I change the index.

Maybe the problem is that I invoked it...but still I don't see why this should be a problem.

Anyone every did something like this?

Best Regards and thanks in advance,
Bobby


P.s.: No, I'd like to stick with the Framework and don't wanna include an API-Call...
 
I hesitate to make a suggestion to one with such a high reputation (compared to mine, that is), but I have always had more success using the ExtractIcon API (shell32.dll) call.
This article has an example.
 
I hesitate to make a suggestion to one with such a high reputation
What...when did that happen? I've never looked at my own reputation before...ahm...I don't think that it matters how much reputation someone has, if he asks a question, an answer/suggestion is what he wants. ;) And most of the time, people are happy if they receive _some_ answer instead of none (at least somebody tried to help them).

Though, I would have loved to stick to the framework, since API-Calls give me the creeps. Also, I wondered why the extraction (using the Framework-Emthods) did not work, though it should have.

Bobby
 
Back
Top