Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired

    [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.
    Quote Originally Posted by Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  2. #2
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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

  3. The Following 4 Users Say Thank You to Hassan For This Useful Post:

    /b/oss (05-24-2010),Blubb1337 (05-24-2010),Samueldo (05-24-2010),Zoom (05-24-2010)

  4. #3
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired
    Great work, thanks for the help!

    Quote Originally Posted by Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  5. #4
    mnpeepno2's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    In a barren wasteland
    Posts
    905
    Reputation
    10
    Thanks
    81
    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


    -----------------------------------------------

    My site: here
    My Blog: here
    My member's area: here
    Minecraft Sever Forum: here


    Minecraft Servers:

    Public:



    Private:



  6. #5
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired
    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
    Last edited by Samueldo; 05-24-2010 at 01:44 AM.
    Quote Originally Posted by Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  7. #6
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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 ???

  8. #7
    /b/oss's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    13,651
    Reputation
    795
    Thanks
    3,547
    Quote Originally Posted by FLAMESABER 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!

  9. #8
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired
    Download link please for the Management DLL please
    Quote Originally Posted by Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  10. #9
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Quote Originally Posted by Samueldo View Post
    Download link please for the Management DLL please
    Flame Saber Edit: Link Removed xD
    Last edited by Hassan; 05-25-2010 at 02:08 AM.

  11. #10
    Zoom's Avatar
    Join Date
    May 2009
    Gender
    male
    Location
    Your going on my 24/7 DDoS hit list.
    Posts
    8,552
    Reputation
    127
    Thanks
    5,970
    My Mood
    Happy
    Quote Originally Posted by FLAMESABER View Post
    https://www.m e d i a f i r e . com/download.php?4ymzy2oozny

    Remove spaces.

    Download it. Open the project. And add reference to this new dll. Problem would be solved.
    Who approved that outside link?
    -Rest in peace leechers-

    Your PM box is 100% full.

  12. #11
    Invidus's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    2,167
    Reputation
    23
    Thanks
    650
    My Mood
    Bored
    Nobody o.o
    Edit: Hey! Points + Activity are back

  13. #12
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired
    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 Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  14. #13
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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.

  15. #14
    Samueldo's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Meh
    Posts
    1,023
    Reputation
    29
    Thanks
    348
    My Mood
    Inspired
    Quote Originally Posted by FLAMESABER 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 Grim View Post
    glad to be an inspiration
    Minions rule. /endof

    InjectPlz Refresh - download v1.0 now!

  16. #15
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. [Solved] Help Getting Rs to run on google Chrome
    By Steele Duke in forum Runescape Help
    Replies: 4
    Last Post: 10-26-2011, 07:56 AM
  2. [Help]Getting Location of a running process.
    By nathanael890 in forum Visual Basic Programming
    Replies: 3
    Last Post: 09-20-2010, 09:07 AM
  3. [HELP] Get Process By ID
    By GameTrainerMaker in forum Visual Basic Programming
    Replies: 5
    Last Post: 06-23-2010, 01:29 AM
  4. [TUT] Get running processes and kill them ~~
    By Zoom in forum Visual Basic Programming
    Replies: 2
    Last Post: 11-09-2009, 08:34 AM
  5. Getting the path of a running process?
    By *Marneus901* in forum C++/C Programming
    Replies: 0
    Last Post: 12-08-2008, 01:51 PM