Link to home
Start Free TrialLog in
Avatar of anamika1977
anamika1977

asked on

Windows API call to get Program Files Folder path

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
thanks
anamika
Avatar of ckosloski
ckosloski

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 CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PRINTERS = &H4

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
Avatar of anamika1977

ASKER

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 ???
thanks
anamika
> Whats
the version on your machine ???

the same


> where can i download a newer version of shell32.dll
i'm looking
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.