* SHGetFileInfo is a pin in the ass *

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

Hi dear programmer,

I'm trying to get associate file type descriptions; for instance "music.mp3"
should return "MP3 audio file (mp3)".
I'm using SHGetFileInfo:

Imports System.Runtime.InteropServices

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 Auto 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

Const SHGFI_TYPENAME = &H400

Private Function GetAssociateType(ByVal sPath As String) As String
Dim hImgSmall As IntPtr
Dim shinfo As New SHFILEINFO
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
hImgSmall = SHGetFileInfo(sPath, 0, shinfo, Marshal.SizeOf(shinfo), _
SHGFI_TYPENAME)
Return shinfo.szTypeName
End Function

This code is similar to the one I use to get the icons associated with
files:
VB: http://support.microsoft.com/default.aspx?scid=kb;EN-US;319340
C#: http://support.microsoft.com/default.aspx?scid=kb;EN-US;319350

The icons works fine, but what I wanted most (AssociateType) doesn't seams
to work :-(
I always get an empty string.

Please, please... help if you can, cause I'm realy stuck.

Kind regards,
Mario
 
Solved. Any way, thank you very much for reading! :-)

If anyone else interested the problem was on the structure declare.

Microsoft VB.NET example has a BUG!; because doesn't declare the structure
correctly.
Yep, it works for the icons, but for the many other SHGetFileInfo uses it
doesn't.

On the page: http://support.microsoft.com/default.aspx?scid=kb;EN-US;319340
....the correct Structure declare is:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
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


Now, shinfo.szTypeName returns the string!

Regards,
Mario

PS: Microsoft documentation team, please correct that page urgently. Lots of
people are having troubles with SHGetFileInfo because of this misleading
example.
 
Back
Top