Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Windows API call to get Program Files Folder path

Posted on 2002-05-01
Last Modified: 2012-08-13
I need an windows API call to get the Program Files Folder path or location . I used
strLongPathName = GetSpecialFolderLocation(CSIDL_PROGRAM_FILES)

but its not working ,can anyone suggest how to get the ProgramFiles folder path , like i get windows directory by GetWindowsDirectory , is there any API call to get Program Files directory??
my current code uses
Public Declare Function SHGetSpecialFolderLocation Lib "shell32" _
   (ByVal hwndOwner As Long, _
    ByVal nFolder As Long, _
    pidl As Long) As Long

Private Function GetSpecialFolderLocation(CSIDL As Long) As String

   Dim sPath As String
   Dim pidl As Long
  'fill the idl structure with the specified folder item
   If SHGetSpecialFolderLocation(Me.hWnd, CSIDL, pidl) = S_OK Then
     'if the pidl is returned, initialize
     'and get the path from the id list
      sPath = Space$(MAX_PATH)
      If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then

        'return the path
         GetSpecialFolderLocation = Left(sPath, InStr(sPath, Chr$(0)) - 1)
      End If
     'free the pidl
      Call CoTaskMemFree(pidl)

    End If
End Function

and SHGetSpecialFolderLocation(Me.hWnd, CSIDL, pidl)  doesn't return S_OK , why so ???

Is there any other API call that i can use
Question by:anamika1977

Expert Comment

ID: 6983922
LVL 18

Expert Comment

ID: 6983986
i tried this:

Private Declare Function SHGetSpecialFolderPath Lib "shell32" _
    Alias "SHGetSpecialFolderPathA" _
    (ByVal hWnd As Long, _
    ByVal lpszPath As String, _
    ByVal nFolder As Integer, _
    ByVal fCreate As Boolean) As Boolean

Private Const CSIDL_PROGRAM_FILES = &H26
Private Const CSIDL_PROGRAMS = &H2

Private Const MAX_PATH = 260
Private Const S_OK = 0

Private Sub Command1_Click()

   Dim sPath As String
   Dim retVal As Long
   sPath = Space(MAX_PATH)
   retVal = SHGetSpecialFolderPath(0, sPath, CSIDL_PROGRAMS, False)
   MsgBox retVal
   sPath = Left(sPath, InStr(sPath, Chr(0)) - 1)
   MsgBox sPath
End Sub

and it only returned a string for CSIDL_PROGRAMS.
I'm thinking we/you might have to get a newer version of shell32.dll

Author Comment

ID: 6984026
from where can i download a newer version of shell32.dll??The one on my system has version 4.72. Whats the version on your machine ???
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 18

Expert Comment

ID: 6984047
> Whats
the version on your machine ???

the same

> where can i download a newer version of shell32.dll
i'm looking
LVL 18

Accepted Solution

bobbit31 earned 100 total points
ID: 6984074
ok, i got a newer version but it screwed up my system... so i'm going with the SHGetFolderPath function in SHFolder.dll

you can get this here: http://www.microsoft.com/downloads/release.asp?releaseid=30340

just make sure to include it in your package.

then do the following:
Option Explicit

Private Const S_OK = &H0                ' Success
Private Const S_FALSE = &H1             ' The Folder is valid, but does not exist
Private Const E_INVALIDARG = &H80070057 ' Invalid CSIDL Value

Private Const CSIDL_FLAG_CREATE = &H8000&

Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1
Private Const MAX_PATH = 260

Private Declare Function SHGetFolderPath Lib "shfolder" _
    Alias "SHGetFolderPathA" _
    (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    ByVal hToken As Long, ByVal dwFlags As Long, _
    ByVal pszPath As String) As Long

Private Const CSIDL_PROGRAM_FILES = &H26
Private Const CSIDL_PROGRAMS = &H2

Private Sub Command1_Click()

Dim sPath As String
Dim RetVal As Long

' Fill our string buffer
sPath = String(MAX_PATH, 0)


Select Case RetVal
    Case S_OK
        ' We retrieved the folder successfully
        ' All C strings are null terminated
        ' So we need to return the string upto the first null character
        sPath = Left(sPath, InStr(1, sPath, Chr(0)) - 1)
        MsgBox sPath
    Case S_FALSE
        ' The CSIDL in nFolder is valid, but the folder does not exist.
        ' Use CSIDL_FLAG_CREATE to have it created automatically
        MsgBox "The folder does not exist"
        ' nFolder is invalid
        MsgBox "An invalid folder ID was specified"
End Select

End Sub

Author Comment

ID: 6985190
I just copied your code and when i click on Command1 button it says SHGetFolderPath sub or function not defined .
Though in my .bas i copied the constants and the function declaration .Why so. Even when i write SHGetFolderPath( it doesn't show me the 5 required parameter.
Do i need to include something to run this API function

Expert Comment

ID: 6985524
Ok this might be a bit of a lame way of doing it but it seems to work on my Windows 2000 system anyway here goes

    StrEnv = Environ("ProgramFiles")
    MsgBox StrEnv

anyway I hope that might help.


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question