Oh my you've tried so hard. Well, with a few API calls it can be easily achieved. Follow the stops below:
Add a flow layout panel to the form and dock it completely. Name it 'fl'.
Now on the main form, import the following namespace:
Imports System.Management
Now on the load event of the main form add the following code:
Dim ExtractionX As New ExtractIcon
Dim procList() As Process = Process.GetProcesses()
Dim i As Integer
For i = 0 To procList.Length - 1 Step i + 1
Try
Dim img As New picturebox
img.size = newpoint(30,28)
img.Image = ExtractionX.GetIcon(GetProcessPath(procList(i))).T oBitmap
fl.Controls.Add(img)
Catch ex As Exception
Continue For
End Try
Next
Now add the following function to the main form:
Public Function GetProcessPath( ByVal prc As Process) As String
Dim query As String = String .Format( "select * from Win32_Process where name='{0}.exe'",prc.ProcessName)
Dim searcher As New ManagementObjectSearcher( "root\CIMV2",query)
Dim result = searcher. Get ().GetEnumerator()
If Not result.MoveNext() Then Throw New ArgumentException( "Process not found")
Return CStr (result.Current( "ExecutablePath"))
End Function
Finally create a new class and name it 'ExtractIcon'. Replace all its code with the following code:
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Public Class ExtractIcon
Flags() Private Enum SHGFI
SmallIcon = & H1
LargeIcon = & H0
Icon = & H100
DisplayName = & H200
Typename = & H400
SysIconIndex = & H4000
UseFileAttributes = & H10
End Enum
Public Enum IconType
SmallIcon = True
LargeIcon = False
End Enum
StructLayout(LayoutKind.Sequential)
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
MarshalAs(UnmanagedType.LPStr,SizeConst = 260)
Public szDisplayName As String
MarshalAs(UnmanagedType.LPStr,SizeConst = 80)
Public szTypeName As String
Public Sub New ( ByVal B As Boolean )
hIcon = IntPtr.Zero
iIcon = 0
dwAttributes = 0
szDisplayName = vbNullString
szTypeName = vbNullString
End Sub
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32"(
ByVal pszPath As String , ByVal dwFileAttributes As Integer ,
ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer , ByVal uFlagsn As SHGFI) As Integer
Public Function GetIcon( ByVal Path As String , Optional ByVal Ico As IconType = True ) As Icon
Dim info As New SHFILEINFO( True )
Dim cbSizeInfo As Integer = Marshal.SizeOf(info)
Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAttributes
If Ico = True Then
flags + = SHGFI.SmallIcon
Else
flags + = SHGFI.LargeIcon
End If
SHGetFileInfo(Path,256,info,cbSizeInfo,flags)
Return Icon.FromHandle(info.hIcon)
End Function
End Class
Run your code and enjoy !!
Here's a screenshot:
Hope this helps