Determining the system folder locations

Option Explicit

Public Enum epPath
CSIDL_DESKTOP = &H0
CSIDL_INTERNET = &H1
CSIDL_PROGRAMS = &H2
CSIDL_CONTROLS = &H3
CSIDL_PRINTERS = &H4
CSIDL_PERSONAL = &H5
CSIDL_FAVORITES = &H6
CSIDL_STARTUP = &H7
CSIDL_RECENT = &H8
CSIDL_SENDTO = &H9
CSIDL_BITBUCKET = &HA
CSIDL_STARTMENU = &HB
CSIDL_DESKTOPDIRECTORY = &H10
CSIDL_DRIVES = &H11
CSIDL_NETWORK = &H12
CSIDL_NETHOOD = &H13
CSIDL_FONTS = &H14
CSIDL_TEMPLATES = &H15
CSIDL_COMMON_STARTMENU = &H16
CSIDL_COMMON_PROGRAMS = &H17
CSIDL_COMMON_STARTUP = &H18
CSIDL_COMMON_DESKTOPDIRECTORY = &H19
CSIDL_APPDATA = &H1A
CSIDL_PRINTHOOD = &H1B
CSIDL_LOCAL_APPDATA = &H1C
CSIDL_ALTSTARTUP = &H1D
CSIDL_COMMON_ALTSTARTUP = &H1E
CSIDL_COMMON_FAVORITES = &H1F
CSIDL_INTERNET_CACHE = &H20
CSIDL_COOKIES = &H21
CSIDL_HISTORY = &H22
CSIDL_COMMON_APPDATA = &H23
CSIDL_WINDOWS = &H24
CSIDL_SYSTEM = &H25
CSIDL_PROGRAM_FILES = &H26
CSIDL_MYPICTURES = &H27
CSIDL_PROFILE = &H28
CSIDL_SYSTEMX86 = &H29
CSIDL_PROGRAM_FILESX86 = &H2A
CSIDL_PROGRAM_FILES_COMMON = &H2B
CSIDL_PROGRAM_FILES_COMMONX86 = &H2C
CSIDL_COMMON_TEMPLATES = &H2D
CSIDL_COMMON_DOCUMENTS = &H2E
CSIDL_COMMON_ADMINTOOLS = &H2F
CSIDL_ADMINTOOLS = &H30
CSIDL_FLAG_CREATE = &H8000&
CSIDL_FLAG_DONT_VERIFY = &H4000
CSIDL_FLAG_MASK = &HFF00
End Enum


Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long

Private Type SHITEMID
CB As Long
AbID As Byte
End Type

Private Type ITEMIDLIST
Mkid As SHITEMID
End Type

Public Type SpecialFolder
FolderPath As String
FolderType As epPath
End Type

'Purpose : Returns an array of special folders
'Inputs :
'Outputs : Returns an array of the special folders.


Function GetAllSpecialFolders() As SpecialFolder()
Dim lThisFolder As Long, lNumFolders As Long
Dim sFolderPath As String
Dim resultArray() As SpecialFolder

Const clMaxFolders As Long = 255

On Error GoTo ErrFailed
Erase resultArray
'Return All Folders
ReDim resultArray(1 To clMaxFolders)
For lThisFolder = 0 To clMaxFolders
sFolderPath = UCase$(GetSpecialFolder(lThisFolder))
If Len(sFolderPath) Then
lNumFolders = lNumFolders + 1
resultArray(lNumFolders).FolderPath = sFolderPath
resultArray(lNumFolders).FolderType = lThisFolder
End If
Next

If lNumFolders Then
ReDim Preserve resultArray(1 To lNumFolders)
GetAllSpecialFolders = resultArray
Else
GetAllSpecialFolders = Empty
End If

Exit Function
ErrFailed:
GetAllSpecialFolders = Empty
End Function

Function GetSpecialFolder(eFolderID As epPath) As String
Dim tRetVal As Long, sBuffer As String
Dim tIDL As ITEMIDLIST
Const NO_ERROR = 0

'Get the special folder
tRetVal = SHGetSpecialFolderLocation(100&, eFolderID, tIDL)
If tRetVal = NO_ERROR Then
'Create a buffer
sBuffer = Space$(512)
'Get the path from the IDList
tRetVal = SHGetPathFromIDList(ByVal tIDL.Mkid.CB, ByVal sBuffer)
'Remove the unnecesarry chr$(0)'s
GetSpecialFolder = Left$(sBuffer, InStr(1, sBuffer, vbNullChar) - 1)
If Right$(GetSpecialFolder, 1) <> "\" And Len(GetSpecialFolder) > 0 Then
GetSpecialFolder = GetSpecialFolder & "\"
End If
End If
End Function

'Demonstration routine
Sub Test()
Dim atFolders() As SpecialFolder, tFolder As SpecialFolder, folder As Integer
'Get all special folders
atFolders = GetAllSpecialFolders()
For folder = 1 To UBound(atFolders)
tFolder = atFolders(folder)
Debug.Print "Type: " & tFolder.FolderType & ". Path: " & tFolder.FolderPath
Next

'Get the start menu folder location
Debug.Print GetSpecialFolder(CSIDL_STARTMENU)
End Sub

No comments:

Visual Basic-6 has emerged as one of the standard Windows Programming Language and it has become a must for all Software people for developing Applications in Visual Environment. So it is, one must learn Visual Basic-6.

What is our Objective in this Courseware?


The Overall Objective in this Courseware is to give a Hands-on Approach to develop different projects in Visual Basic-6.0 using intrinsic, professional and user–created ActiveX controls and also develop projects using databases, DAO’s, ADO’s, DLL’s, Documents, Crystal Reports etc. covering almost all the essential features of VB-6 Professional Edition. After reading one lesson any interested reader will be able to get complete hands-on experience with the VB project and get a sense of fulfilment and achievement. Learning by doing is the motto with which this courseware is written. After giving a short introduction about VB-6 we will explain how to create and execute a project in VB using some intrinsic ActiveX controls. Creating and executing projects will be the central theme of all the lessons which we will be giving in this courseware.

What is Visual Basic-6?


Visual Basic-6 has its origin in Basic which was developed round about the year 1960, when high level languages were just being introduced to the computer community. Microsoft has made it extremely powerful by gearing all its good features to the Windows environment. Starting with the version 3 and then with 4, and then with 6, Visual Basic is now at version 6. Basic is a Procedure Oriented Language intended to implement single tasks in text based environment whereas Visual Basic is an Event Driven Language intended to implement Projects or Applications containing multiple tasks in Windows Environment.

What can Visual Basic do for you?

Visual Basic can serve as an ideal front end tool for the clients to interact. It has got connectivity mechanisms for all types of databases situated far and wide in a network and so it can cater to the needs of a large body of clients. Using the latest ActiveX technologies, it can integrate the functionalities provided by other applications like Word Excel and other Windows. Its internet capabilities provide easy access to documents and applications across the internet. Above all it embodies the Object Oriented Technology, which is the cutting edge technology for all the present day developments in the Software World. The final application is a true EXE file and so can be freely distributed.


Structure of VB-6 Projects:


We said earlier that VB-6 implements projects or applications. A project is developed using one or more Forms. A Form is simply a window containing one or more Controls. Controls in VB consist of labels, text boxes, list boxes, combo boxes, scroll bars etc. which are the constituents of windows environment. It is only the controls that give VB, its immense power and so there is a lot of interest in creating more and more powerful controls. ActiveX controls mark a significant development in controls technology. In fact all controls in VB-6 are ActiveX controls, which have the extension .ocx. These controls have properties whose values can be initialized at design time and also varied during run time. The properties are something like variables. The controls are activated by codes written in a high level language. By associating our problem variables with the properties of the controls, our problem variables can be manipulated to give the problem solution. In summary we can say that a VB project is made of forms, controls and their properties and codes.

Integrated Development Environment:

The working environment in VB is often referred to as the Integrated Development Environment or IDE, because it integrates many different functions such as design, editing, compiling and debugging within a common environment. Since all our projects are developed only in the IDE, let us now have a brief look at its features. You will be able to understand their uses at the time of building projects. The VB IDE looks as shown in the figure.