[API]
SHGetSpecialFolderLocation
SHGetSpecialFolderLocationFromSHFolder
SHGetPathFromIDListA
SHGetPathFromIDListW
CoTaskMemFree

[INCLUDE]
GenericInit

[CODE]
Public Function GetSpecialFolderPath(ByVal nFolder As Long, ByVal SetBackSlash As Boolean) As String
  Dim xLong1 As Long
  Dim xLong2 As Long

  If SHGetSpecialFolderLocation(0&, nFolder, xLong1) = 0& Then
DoWithSHFolderDLL:
    If Not xLong1 = 0& Then
      If IsUnicode Then
        xLong2 = SHGetPathFromIDListW(xLong1, ptrTemp)
        Else
        xLong2 = SHGetPathFromIDListA(xLong1, ptrTemp)
        End If
      If Not xLong2 = 0& Then
        GetSpecialFolderPath = PeekS(ptrTemp)
        If Right(GetSpecialFolderPath, 1&) = "\" Then
          If Not SetBackSlash Then GetSpecialFolderPath = Left(GetSpecialFolderPath, Len(GetSpecialFolderPath) - 1&)
          ElseIf SetBackSlash Then
          GetSpecialFolderPath = GetSpecialFolderPath + "\"
          End If
        End If
      Call CoTaskMemFree(xLong1)
      End If
    Else
    On Error GoTo Er1
    If SHGetSpecialFolderLocationFromSHFolder(0&, nFolder, xLong1) = 0& Then
      On Error GoTo 0
      GoTo DoWithSHFolderDLL
      End If
Rs1:
    On Error GoTo 0
    End If
  Exit Function
  
Er1:
  Resume Rs1
  End Function
