Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / September 2008



Tip: Looking for answers? Try searching our database.

SHGetFileInfo bug on Win2k?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sinna - 29 Sep 2008 10:26 GMT
Hi all,

I've experienced a weird behavior of the SHGetFileInfo Win32 API on
Windows 2000 (not on XP).
For both versions (ANSI and Wide), an empty string is returned for
unregistered file types when running my application stand-alone.
When running from within the IDE, the file type is returned as expected
(e.g. 'BRC File').

I've read about this issue and someone already reported it on
http://groups.google.com/group/microsoft.public.platformsdk.shell/browse_thread/
thread/b0c7dde7724f06d9/415f85453fdd5c81?lnk=st&q=SHGetFileInfoW+SHGFI_TYPENAME+
blank#415f85453fdd5c81
,
but as it affects more than only the Wide version and as it works
correct when running from the IDE, I repost the question here.

A solution proposed in the link provided above (using AssocQueryString)
doesn't work out either. I also want to avoid loading strings with
undocumented identifiers (future-proof? I don't think so).

Someone has a solution?
Note it should work on the Wide-variant as I have to support Japanese
systems.

TIA,
Sinna
Dean Earley - 29 Sep 2008 10:56 GMT
> Hi all,
>
[quoted text clipped - 17 lines]
> Note it should work on the Wide-variant as I have to support Japanese
> systems.

You'll have to show us some of your code.
It sounds like a memory allocation problem.

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

Sinna - 29 Sep 2008 12:04 GMT
>> Hi all,
>>
[quoted text clipped - 21 lines]
> You'll have to show us some of your code.
> It sounds like a memory allocation problem.

Strange enough (?), my code works on WinXP like a charm, and it does
also work on Win2K for registered file types.

My first opinion was also a memory allocation problem or a bad calling
convention (ByVal instead of ByRef or vice versa) but that doesn't
explain why it works for registered file types. In that case I would
expect it to fail every time.

Nevertheless, here's snippet of my implementation:

<code>
'SHGetFileInfo Flags
Private Const SHGFI_USEFILEATTRIBUTES   As Long = &H10
Private Const SHGFI_TYPENAME            As Long = &H400

'FileAttribute flags
Private Const FILE_ATTRIBUTE_DIRECTORY              As Long = &H10
Private Const FILE_ATTRIBUTE_NORMAL                 As Long = &H80

Private Type SHFILEINFO _SHFILEINFO {
    hIcon                           As Long
    iIcon                           As Long
    dwAttributes                    As Long
    szDisplayName(519)              As Byte
    szTypeName(159)                 As Byte
End Type

Private Declare Function SHGetFileInfo Lib "shell32.dll" _
        Alias "SHGetFileInfoW" ( _
    ByVal pszPath As Long, _
    ByVal dwFileAttributes As Long, _
    psfi As SHFILEINFO, _
    ByVal cbSizeFileInfo As Long, _
    ByVal uFlags As Long) As Long

Private Sub GetFileInfo()
    'mvarExtension is initialized in the class

    On Error GoTo ErrHandler

    Static shInfo As SHFILEINFO, shInfoStructSize As Long

    Dim lRetval As Long

    If shInfoStructSize = 0 Then
        shInfoStructSize = Len(shInfo)

        'working variables...
        Dim sExtension As String, lFileAttributes As Long
        Dim lFlags As Long, sTypeName As String

        If mvarExtension = vbNullChar Then
            lFileAttributes = FILE_ATTRIBUTE_DIRECTORY
            sExtension = ""
        Else
            lFileAttributes = FILE_ATTRIBUTE_NORMAL
            sExtension = "." & mvarExtension
        End If

        'Get the system TypeName associated with that extension
        lFlags = SHGFI_TYPENAME Or SHGFI_USEFILEATTRIBUTES
        lRetval = SHGetFileInfo( _
            ByVal StrPtr(sExtension), lFileAttributes, shInfo, _
            shInfoStructSize, lFlags)

        If lRetval <> 0 Then
            sTypeName = shInfo.szTypeName   'Implicit conversion!
            mvarFileType = Left(sTypeName, _
                InStr(1, sTypeName, vbNullChar) - 1)
        Else
            GoSub LogAPIError_GetFileInfo
        End If
    'Else
    '   GetFileInfo() called before for the given extension
    End If

    Exit Sub

LogAPIError_GetFileInfo:
    'Some Warning Error Routine here

    Resume

ErrHandler:
    'Some Error Handling Routine here
End Sub

</code>

Note this snippet is only part of my implementation, so if some
declarations are missing, it's due to the selective copy-paste.

Sinna
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.