[Help] Getting icon of a running process

Posts 1–15 of 30 · Page 1 of 2
[Help] Getting icon of a running process
I've tried dozens of pages from Google, tons of code snippets and I couldn't get it to work. I want to get process icons like Faith does.

Don't put Google or LMGTFY as an answer, I've tried for days now.

You'll be put on the program credits, be thanked and repped and even get a free added in!

Thanks for your help, or just reading with no intent to do anything.
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
Great work, thanks for the help!

Quote Originally Posted by Hassan View Post
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
AWESOME GJ!
thats umm, thats a lot of VB 0.o

I think my head will explode of the amount of stuff in my mind now. x.x
Quote Originally Posted by mnpeepno2 View Post
thats umm, thats a lot of VB 0.o

I think my head will explode of the amount of stuff in my mind now. x.x
Mine already has! 5 months ago, when I joined.

EDIT: Now for some reason I'm getting errors.

Error 1 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 19 1 IconProcess
Error 2 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 24 9 IconProcess
Error 3 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 26 9 IconProcess
Error 4 Type 'ManagementObjectSearcher' is not defined. C:\Users\Raymondo\AppData\Local\Temporary Projects\IconProcess\Form1.vb 5 29 IconProcess
Quote Originally Posted by Samueldo View Post
Mine already has! 5 months ago, when I joined.

EDIT: Now for some reason I'm getting errors.

Error 1 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 19 1 IconProcess
Error 2 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 24 9 IconProcess
Error 3 Declaration expected. C:\Users\Raymundo\AppData\Local\Temporary Projects\IconProcess\ExtractIcon.vb 26 9 IconProcess
Error 4 Type 'ManagementObjectSearcher' is not defined. C:\Users\Raymondo\AppData\Local\Temporary Projects\IconProcess\Form1.vb 5 29 IconProcess

I recognize error 4. It occurred for me. It occurs when you are using Express Edition. You need to download a dll of System.Management. But first we need to solve the first 3.

But first I need to know that did it worked fine for at least once ???
Download link please for the Management DLL please
Quote Originally Posted by Samueldo View Post
Download link please for the Management DLL please
Flame Saber Edit: Link Removed xD
Nobody o.o
Edit: Hey! Points + Activity are back
Okay I've got rid of the 4th error, now the last three.

It underlines the StructLayout and MarshalAs lines:

Code:
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
Quote Originally Posted by Samueldo View Post
Okay I've got rid of the 4th error, now the last three.

It underlines the StructLayout and MarshalAs lines:

Code:
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
I need the complete code of the ExtractIcon Class.
Quote Originally Posted by Hassan View Post
I need the complete code of the ExtractIcon Class.
Erm... don't you have it already? I only edited the lines slightly because there were a lot of breaks in it.
Quote Originally Posted by Samueldo View Post
Erm... don't you have it already? I only edited the lines slightly because there were a lot of breaks in it.
Why you edited the code ?? I think the problem is occurring coz of editing. Send me the edited code, or use the code I sent in its original form.
Posts 1–15 of 30 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?