(50 pts) API to find Common directory

There's a Windows API to find the Windows, Windows\System, and Windows\Temp directory... but I don't know of any API to find the Program Files\Common directory.  

I didn't need to know this until I found out that Microsoft went and changed how Windows works!!  STUPID!  Anyways... I need sample code on how to do it along with declarations/constants/types used in sample code.

Thanks,

HATCHET
LVL 3
HATCHETAsked:
Who is Participating?
 
HATCHETConnect With a Mentor Author Commented:
Well, I got the code to use the  SHGetSpecialFolderLocation and SHGetPathFromIDList API's to get the Program Files directory, but you need Windows 2000 to do it.  That doesn't work for me.  I need it for Win 9x / NT / 2000.

What I did find that is a reliable way to get this information from any system is getting it directly from the Windows Registry (which is where the API's get it I believe).  The following registry locations will give you the following directories:

- Windows Directory (C:\WINDOWS)
   (Add "\System" to the end of this to get the WinSys Dir)
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\SystemRoot

- Common Directory (C:\Program Files\Common Files):
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\CommonFilesDir

- Program Files Directory (C:\Program Files)
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir

- Program Files Path (C:\Program Files)
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ProgramFilesPath

- .INF Directory (C:\WINDOWS\INF)
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\DevicePath

- Multimedia Directory (C:\WINDOWS\Media)
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\MediaPath

- Internet Explorer "Active Desktop" Wallpaper Directory
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\WallPaperDir
0
 
AzraSoundCommented:
http://www.mvps.org/vbnet/code/shell/csidl.htm
http://www.mvps.org/vbnet/code/shell/shpathidlist.htm

two links with the same idea...hope one of em points you to what you need
0
 
RuchiCommented:
Put a command button on the form.
Msgbox SystemPath(CSIDL_PROGRAMS) -- use the directory type


Option Explicit

Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.DLL" (ByVal hWndOwner As Long, ByVal SHFolder As Long, idl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32.DLL" (ByVal idl As Long, ByVal Path As String) As Long
Private Declare Function GetDesktopWindow Lib "User32.DLL" () As Long

' Enumeration for special directory type
Public Enum SystemFolder
   CSIDL_DESKTOP = 0
   CSIDL_INTERNET = 1
   CSIDL_PROGRAMS = 2
   CSIDL_CONTROLS = 3
   CSIDL_PRINTERS = 4
   CSIDL_PERSONAL = 5
   CSIDL_FAVORITES = 6
   CSIDL_STARTUP = 7
   CSIDL_RECENT = 8
   CSIDL_SENDTO = 9
   CSIDL_BITBUCKET = 10
   CSIDL_STARTMENU = 11
   CSIDL_DESKTOPDIRECTORY = 16
   CSIDL_DRIVES = 17
   CSIDL_NETWORK = 18
   CSIDL_NETHOOD = 19
   CSIDL_FONTS = 20
   CSIDL_TEMPLATES = 21
   CSIDL_COMMON_STARTMENU = 22
   CSIDL_COMMON_PROGRAMS = 23
   CSIDL_COMMON_STARTUP = 24
   CSIDL_COMMON_DESKTOPDIRECTORY = 25
   CSIDL_APPDATA = 26
   CSIDL_PRINTHOOD = 27
   CSIDL_ALTSTARTUP = 29
   CSIDL_COMMON_ALTSTARTUP = 30
   CSIDL_COMMON_FAVORITES = 31
   CSIDL_INTERNET_CACHE = 32
   CSIDL_COOKIES = 33
   CSIDL_HISTORY = 34
   End Enum
   
Public Function SystemPath(ByVal PathID As SystemFolder) As String
' Call to get the directory path of a special directory (any in enum list)
Dim lngIDL As Long
Dim strBuff As String
strBuff = Space$(1024)
Dim n As Long
n = SHGetSpecialFolderLocation(GetDesktopWindow(), PathID, lngIDL)
If n Then Exit Function
n = SHGetPathFromIDList(lngIDL, strBuff)
If n > 0 Then
   n = InStr(strBuff, Chr$(0)) - 1
   strBuff = Left$(strBuff, n)
   If Right$(strBuff, 1) <> "\" Then strBuff = strBuff & "\"
   SystemPath = strBuff
   End If
End Function

Private Sub Command1_Click()
MsgBox SystemPath(CSIDL_COMMON_PROGRAMS)
End Sub
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
RuchiCommented:
Forget to tell you something.
Just change the directory type by using the number. For instance, use MsgBox SystemPath(34) for History folder.
0
 
HATCHETAuthor Commented:
AzraSound & Ruchi,

The SHGetSpecialFolderLocation is NOT the way to get the path to C:\Program Files\Common.  It's a great way to get the paths to a lot of other special paths... adn that's usefull, but it doesn't answer my question.  I'm sorry.

Here's what Microsoft's documentation says about the CSIDL_COMMON_PROGRAMS flag when passed to this function:

"File system directory that contains the directories for the common program groups that appear on the Start menu for all users.  A typical path is C:\Documents and Settings\All Users\Start Menu\Programs. Valid only for Windows NT® systems."


Any other suggestions?

HATCHET
0
 
HATCHETAuthor Commented:
I did some researching into this and in Windows 2000, there's a parameter that you can pass this function that will return the Windows, Windows\System, Program Files, and Program Files\Common directories.  That doesn't help me since I'm programming for Windows 95/98/NT.  There's got to be another way to do this.

HATCHET
0
 
AzraSoundCommented:
CSIDL_PROGRAM_FILES_COMMON - Version 5.0. A folder for components that are shared across applications. A common path is C:\Program Files\Common. Valid only for Windows NT and Windows® 2000 systems.
CSIDL_PROGRAM_FILES_COMMONX86 - Version 5.0. Program Files folder that is common to all users for x86 applications on RISC systems. A common path is C:\Program Files (x86)\Common.


still looking...
0
 
darinwCommented:
I am accepting one of HATCHET's comments as an answer to move this question to the PAQ.
 
darinw
Customer Service
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.