Results 1 to 4 of 4

Thread: SHGetSpecialFolderLocation...?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2001
    Location
    You are HERE •™
    Posts
    1,300

    SHGetSpecialFolderLocation...?

    The code below seems to work fine with Win9*, but with Win2k it doesn't. Anyone know why?

    BTW: If you test it out you will need to look up the "Cache Name" of an image file in your Temporary Internet Files folder.
    Then use that image file name instead of "image[1].gif"

    Module:
    VB Code:
    1. Option Explicit
    2. '//////////////////////////////////////////////////
    3. '// Determine Temporary Internet //////////////////
    4. '// Folder Location Declarations //////////////////
    5. '//////////////////////////////////////////////////
    6.  
    7. Private Type ShortItemId
    8.      cb As Long
    9.      abID As Byte
    10. End Type
    11.  
    12. Private Type ITEMIDLIST
    13.      mkid As ShortItemId
    14. End Type
    15.  
    16. ' Declare constants.
    17. Public Const CSIDL_INTERNET_CACHE = &H20
    18. 'Public Const CSIDL_TEMPLATES = &H15
    19. 'Public Const CSIDL_STARTMENU = &HB
    20. 'Public Const CSIDL_FAVORITES = &H6
    21. 'Public Const CSIDL_DESKTOPDIRECTORY = &H10
    22.  
    23. ' Declare API functions.
    24. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    25.    (ByVal pidl As Long, ByVal pszPath As String) As Long
    26.  
    27. Private Declare Function SHGetSpecialFolderLocation Lib _
    28.    "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder _
    29.    As Long, pidl As ITEMIDLIST) As Long
    30.    
    31. '//////////////////////////////////////////////////
    32. '///////////// Find File Declarations /////////////
    33. '//////////////////////////////////////////////////
    34.  
    35. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    36. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    37. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    38. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    39.  
    40. Const MAX_PATH = 260
    41. Const MAXDWORD = &HFFFF
    42. Const INVALID_HANDLE_VALUE = -1
    43. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    44. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    45. Const FILE_ATTRIBUTE_HIDDEN = &H2
    46. Const FILE_ATTRIBUTE_NORMAL = &H80
    47. Const FILE_ATTRIBUTE_READONLY = &H1
    48. Const FILE_ATTRIBUTE_SYSTEM = &H4
    49. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    50.  
    51. Private Type FILETIME
    52.     dwLowDateTime As Long
    53.     dwHighDateTime As Long
    54. End Type
    55.  
    56. Private Type WIN32_FIND_DATA
    57.     dwFileAttributes As Long
    58.     ftCreationTime As FILETIME
    59.     ftLastAccessTime As FILETIME
    60.     ftLastWriteTime As FILETIME
    61.     nFileSizeHigh As Long
    62.     nFileSizeLow As Long
    63.     dwReserved0 As Long
    64.     dwReserved1 As Long
    65.     cFileName As String * MAX_PATH
    66.     cAlternate As String * 14
    67. End Type
    68.  
    69.  
    70.  
    71.  
    72. Public Function GetSpecialFolder(CSIDL As Long) As String
    73.  
    74.    Dim idlstr As Long
    75.    Dim sPath As String
    76.    Dim IDL As ITEMIDLIST
    77.  
    78.    Const NOERROR = 0
    79.    Const MAX_LENGTH = 260
    80.  
    81.    On Error GoTo Err_GetFolder
    82.  
    83.    ' Fill the idl structure with the specified folder item.
    84.    idlstr = SHGetSpecialFolderLocation _
    85.       (0, CSIDL, IDL)
    86.  
    87.    If idlstr = NOERROR Then
    88.  
    89.         ' Get the path from the idl list, and return
    90.         ' the folder with a slash at the end.
    91.         sPath = Space$(MAX_LENGTH)
    92.         idlstr = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
    93.  
    94.            If idlstr Then
    95.              GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) _
    96.                - 1) '& "\"
    97.            End If
    98.  
    99.    End If
    100.  
    101. Exit_GetFolder:
    102.     Exit Function
    103. Err_GetFolder:
    104.     MsgBox Err.Description, vbCritical Or vbOKOnly
    105.     Resume Exit_GetFolder
    106.  
    107. End Function
    108.  
    109. Public Function StripNulls(OriginalStr As String) As String
    110.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    111.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    112.     End If
    113.     StripNulls = OriginalStr
    114. End Function
    115.  
    116. Public Function FindFilesAPI(path As String, SearchStr As String)
    117.     'KPD-Team 1999
    118.     'E-Mail: [email][email protected][/email]
    119.     'URL: [url]http://www.allapi.net/[/url]
    120.  
    121.     Dim FileName As String ' Walking filename variable...
    122.     Dim DirName As String ' SubDirectory Name
    123.     Dim dirNames() As String ' Buffer for directory name entries
    124.     Dim nDir As Integer ' Number of directories in this path
    125.     Dim i As Integer ' For-loop counter...
    126.     Dim hSearch As Long ' Search Handle
    127.     Dim WFD As WIN32_FIND_DATA
    128.     Dim Cont As Integer
    129.     If Right(path, 1) <> "\" Then path = path & "\"
    130.     ' Search for subdirectories.
    131.     nDir = 0
    132.     ReDim dirNames(nDir)
    133.     Cont = True
    134.     hSearch = FindFirstFile(path & "*", WFD)
    135.     If hSearch <> INVALID_HANDLE_VALUE Then
    136.         Do While Cont
    137.         DirName = StripNulls(WFD.cFileName)
    138.         ' Ignore the current and encompassing directories.
    139.         If (DirName <> ".") And (DirName <> "..") Then
    140.             ' Check for directory with bitwise comparison.
    141.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    142.                 dirNames(nDir) = DirName
    143.                 nDir = nDir + 1
    144.                 ReDim Preserve dirNames(nDir)
    145.             End If
    146.         End If
    147.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    148.         Loop
    149.         Cont = FindClose(hSearch)
    150.     End If
    151.     ' Walk through this directory and sum file sizes.
    152.     hSearch = FindFirstFile(path & SearchStr, WFD)
    153.     Cont = True
    154.     If hSearch <> INVALID_HANDLE_VALUE Then
    155.         While Cont
    156.             FileName = StripNulls(WFD.cFileName)
    157.             If (FileName <> ".") And (FileName <> "..") Then
    158.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    159.                 Form1.picYahoo.Picture = LoadPicture(path & FileName)
    160.             End If
    161.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    162.         Wend
    163.         Cont = FindClose(hSearch)
    164.     End If
    165.     ' If there are sub-directories...
    166.     If nDir > 0 Then
    167.         ' Recursively walk into them...
    168.         For i = 0 To nDir - 1
    169.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr)
    170.         Next i
    171.     End If
    172. End Function

    Form: Command Button and Picture Box
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim picName As String
    5.     picName = "image[1].gif"
    6.     FindFilesAPI GetSpecialFolder(CSIDL_INTERNET_CACHE), picName
    7. End Sub

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    If you mean that you cannot get the path of the CACHE folder, then this seems to work:
    VB Code:
    1. Public Enum eSIDL
    2.     CSIDL_DESKTOP = &H0
    3.     CSIDL_INTERNET = &H1
    4.     CSIDL_PROGRAMS = &H2
    5.     CSIDL_CONTROLS = &H3
    6.     CSIDL_PRINTERS = &H4
    7.     CSIDL_PERSONAL = &H5
    8.     CSIDL_FAVORITES = &H6
    9.     CSIDL_STARTUP = &H7
    10.     CSIDL_RECENT = &H8
    11.     CSIDL_SENDTO = &H9
    12.     CSIDL_BITBUCKET = &HA
    13.     CSIDL_STARTMENU = &HB
    14.     CSIDL_DESKTOPDIRECTORY = &H10
    15.     CSIDL_DRIVES = &H11
    16.     CSIDL_NETWORK = &H12
    17.     CSIDL_NETHOOD = &H13
    18.     CSIDL_FONTS = &H14
    19.     CSIDL_TEMPLATES = &H15
    20.     CSIDL_COMMON_STARTMENU = &H16
    21.     CSIDL_COMMON_PROGRAMS = &H17
    22.     CSIDL_COMMON_STARTUP = &H18
    23.     CSIDL_COMMON_DESKTOPDIRECTORY = &H19
    24.     CSIDL_APPDATA = &H1A
    25.     CSIDL_PRINTHOOD = &H1B
    26.     CSIDL_LOCAL_APPDATA = &H1C
    27.     CSIDL_ALTSTARTUP = &H1D
    28.     CSIDL_COMMON_ALTSTARTUP = &H1E
    29.     CSIDL_COMMON_FAVORITES = &H1F
    30.     CSIDL_INTERNET_CACHE = &H20
    31.     CSIDL_COOKIES = &H21
    32.     CSIDL_HISTORY = &H22
    33.     CSIDL_COMMON_APPDATA = &H23
    34.     CSIDL_WINDOWS = &H24
    35.     CSIDL_SYSTEM = &H25
    36.     CSIDL_PROGRAM_FILES = &H26
    37.     CSIDL_MYPICTURES = &H27
    38.     CSIDL_PROFILE = &H28
    39.     CSIDL_SYSTEMX86 = &H29
    40.     CSIDL_PROGRAM_FILESX86 = &H2A
    41.     CSIDL_PROGRAM_FILES_COMMON = &H2B
    42.     CSIDL_PROGRAM_FILES_COMMONX86 = &H2C
    43.     CSIDL_COMMON_TEMPLATES = &H2D
    44.     CSIDL_COMMON_DOCUMENTS = &H2E
    45.     CSIDL_COMMON_ADMINTOOLS = &H2F
    46.     CSIDL_ADMINTOOLS = &H30
    47. End Enum
    48.  
    49.  
    50. Private Const MAX_PATH = 255
    51. Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
    52. Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
    53.  
    54. Public Function GetSpecialFolderPath(ByVal pHwnd As Long, p_SIDL As eSIDL) As String
    55.     Dim lngRet As Long
    56.     Dim lngPidl   As Long
    57.     Dim lngPath   As Long
    58.     Dim strPath   As String
    59.  
    60.     strPath = Space$(MAX_PATH)
    61.     lngRet = SHGetSpecialFolderLocation(pHwnd, p_SIDL, lngPidl)
    62.     If lngRet = 0 Then
    63.         lngRet = SHGetPathFromIDList(lngPidl, strPath)
    64.         If lngRet = 1 Then
    65.             strPath = Left(strPath, InStr(strPath, vbNullChar) - 1)
    66.             GetSpecialFolderPath = strPath
    67.         End If
    68.     End If
    69. End Function
    70.  
    71.  
    72. Private Sub Command1_Click()
    73.    MsgBox GetSpecialFolderPath(Me.hwnd, CSIDL_INTERNET_CACHE)
    74. End Sub

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Ohh I see what you mean. You want to load the picture itself. I think in Win2K it stores the address only. Because if you try to open that picture from the Cache folder it opens browser and navigates to the actual site that has that file.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2001
    Location
    You are HERE •™
    Posts
    1,300
    Serge -

    Are you sure about that? Aren't the image files different sizes in the cache?
    I guess what I'm asking is: The images files are actually image files, and not just a reference to some internet address.
    If you copy an image file from the cache to your desktop for example, you should be able to open it with an image editor.

    Could you do me a favor...? Could you try out my code, and see if it works.
    Or better yet take your code and combine it with my FindFile API code to see if that works.
    I'm not sure if my problem is in the FindFile Code or the SHGetSpecialFolderLocation Code.

    Any help would be appreciated....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width