SHGetFileInfo question

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
how do i get the My Computer icon using SHGetFileInfo?
i've tried this

VB.NET:
SHGetFileInfo("", FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON + SHGFI_SMALLICON + SHGFI_DISPLAYNAME)

but it doesn't work.
i'm not sure what the filename for My Computer should be?
 
system.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

mess around with some of those things to see if you can get to it but for some reason that just returns an empty string on my PC but maybe you will have better luck
 
thats what

system.Environment.GetFolderPath(Environment.Speci alFolder.MyComputer)

does.

i found the answer. you have to use a PIDL
 
heres how

VB.NET:
Imports System.Runtime.InteropServices

Public Class Form1

    Private Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure

    Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr

    Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As IntPtr, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr

    Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
    (ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByRef pidl As Integer) As Integer


    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_DISPLAYNAME = &H200
    Private Const SHGFI_TYPENAME = &H400
    Private Const SHGFI_PIDL = &H8
    Private Const FILE_ATTRIBUTE_NORMAL = &H80

    Private Const CSIDL_DRIVES = &H11


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim nIndex As Integer = 1
        Dim shinfo As SHFILEINFO
        Dim arDrives(), fi As String

        arDrives = IO.Directory.GetLogicalDrives()
        shinfo = New SHFILEINFO()

        lstIcons.SmallImageList = imgIcons
        lstIcons.LargeImageList = imgIcons

        Dim tmpPidl As IntPtr
        SHGetSpecialFolderLocation(0, CSIDL_DRIVES, tmpPidl)
        SHGetFileInfo(tmpPidl, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_PIDL + SHGFI_DISPLAYNAME + SHGFI_ICON + SHGFI_SMALLICON)
        Marshal.FreeCoTaskMem(tmpPidl)

        Dim myIcon As System.Drawing.Icon
        myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
        imgIcons.Images.Add(myIcon)
        lstIcons.Items.Add(shinfo.szDisplayName, 0)
        

        For Each fi In arDrives
            SHGetFileInfo(fi, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON + SHGFI_SMALLICON + SHGFI_DISPLAYNAME)
            myIcon = Nothing
            myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
            imgIcons.Images.Add(myIcon)
            lstIcons.Items.Add(shinfo.szDisplayName, nIndex)
            nIndex += 1
        Next

    End Sub
End Class
 
Back
Top