B4J Question How do I retrieve an external app icon from its exe?

Cableguy

Expert
Licensed User
Longtime User
Hi guys

I' m listing the files existing in a folder... How do I retrieve their icon?
If one of these files is an exe, how to retrieve the embed icon?
 
Try unzipping the .exe file. One of the resulting sub-folders will probably be entitled "ICON" or something like that.
 
Upvote 0
It will not work. An exe file is not a zip file. The icon is stored as an embedded resource.

You can use this code:
Code: Copy
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private ImageView1 As ImageView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.RootPane.LoadLayout("1") 'Load the layout file.
   MainForm.Show
   ImageView1.SetImage(GetFileIcon("C:\Program Files (x86)\Anywhere Software\B4J", "B4J.exe"))
   
End Sub

Sub GetFileIcon(Dir As String, FileName As String) As Image
   Dim jo As JavaObject
   Dim JavaFile As JavaObject
   JavaFile.InitializeNewInstance("java.io.File", Array(File.Combine(Dir, FileName)))
   Dim icon As JavaObject = jo.InitializeStatic("sun.awt.shell.ShellFolder").RunMethodJO( _
     "getShellFolder", Array(JavaFile)).RunMethod("getIcon", Array(True))
   Return AwtImageToImage(icon)
End Sub

Private Sub AwtImageToImage(img As JavaObject) As Image
   Dim jo As JavaObject
   Return jo.InitializeStatic("javafx.embed.swing.SwingFXUtils").RunMethod("toFXImage", Array(img, Null))
End Sub
 
Upvote 0
Upvote 0
Extra question:

will this snippet get the icon of any file extension? (.txt .doc .jar .jpeg .awg etc)
 
Upvote 0
Upvote 0
Back
Top