Jump to content

How to use SHGetFileInfo from shell32.dll?


Recommended Posts

I know from these two threads:




that the idea of using SHGetFileInfo has been dismissed as it required structures that AutoIt did not support, but I am wondering whether or not recent development has solved that issue. So I thought I'd give it a try. My results seem to indicate that A) the required structs still are not available for AutoIt, or B) I don't know squat about DLL calls (true), or C) All of the above.

Maybe someone who knows more about this stuff than I do (Holger?) peek at what I've got and let me know if I am doing something insanely stupid:

; lod3n's translation attempt from: http://support.microsoft.com/kb/319350
; "How to use the SHGetFileInfo function to get the icons that are 
; associated with files in Visual C# .NET"
; this would be cool, because I can get the icon handle of any file, and show it correctly
; in a menu or listview, or just a button (if they're ownerdrawn...)

Const $SHGFI_ICON           = 0x100        ;Get icon
Const $SHGFI_SMALLICON      = 0x1          ;Get Small Icon

$pszPath = "C:\" ; this will become the sole parameter of a UDF, once this works
$dwFileAttributes = 0

;the first and second struct elements in the above URL are of a type IntPtr
;I do not know how to translates this to AutoIt correctly.
$psfi = DllStructCreate("int;int;uint;str;char[260];char[80]")

    $stIconHandle = DllStructCreate("ptr")
    $ptIconHandle = DllStructGetPtr($stIconHandle)
    DllStructSetData($psfi, 1, $ptIconHandle)

    $stIconIndex = DllStructCreate("ptr")
    $ptIconIndex = DllStructGetPtr($stIconIndex)
    DllStructSetData($psfi, 2, $ptIconIndex)

$cbSizeFileInfo = DllStructGetSize($psfi)

$hImgSmall = DllCall('shell32.dll', 'int', 'SHGetFileInfo', _
    'str', $pszPath, _
    'uint', $dwFileAttributes, _
    'ptr', DllStructGetPtr($psfi), _
    'uint', $cbSizeFileInfo, _
    'uint', $uFlags)
Switch @error
    Case 0
        Con("+ Success")
    case 1
        Con("! unable to use the DLL file")
    Case 2
        Con('! unknown "return type"')
    case 3
        Con('! "function" not found in the DLL file')
if IsArray($hImgSmall) Then
    Con("+ Returned an array!")
;~  for $i = 0 to ubound($hImgSmall)-1
;~      Con($i & ": " & $hImgSmall[$i])
;~  Next
    Con("! Did not return an array!")

$hIcon  = DllStructGetData($stIconHandle, 1)
Con("Icon Handle: " & $hIcon) ; returns 0, which is incorrect, it should be something like 1367278955

$iIcon  = DllStructGetData($stIconIndex, 1)
Con("Icon Index: " & $iIcon) ; returns 0, which is incorrect, it should be something like 1 or 2

Func Con($string)
    ConsoleWrite($string & @crlf)

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

1.) you do not need any pointer to the icon handle and index.

Direct use the 1st 2 values from the psfi-structure then you got them.

I have no idea why you think that these 1st 2 elements are of type int_ptr?

From MSDN:

typedef struct _SHFILEINFO {
    HICON hIcon;
    int iIcon;
    DWORD dwAttributes;
    TCHAR szDisplayName[MAX_PATH];
    TCHAR szTypeName[80];

So you could use the psfi like:

$psfi = DllStructCreate("uint;int;dword;char[260];char[80]")
Edited by Holger
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...