Solved

Windows API call to get Program Files Folder path

Posted on 2002-05-01
7
878 Views
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
thanks
anamika
0
Comment
Question by:anamika1977
7 Comments
 
LVL 4

Expert Comment

by:ckosloski
ID: 6983922
0
 
LVL 18

Expert Comment

by:bobbit31
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 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
0
 

Author Comment

by:anamika1977
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 ???
thanks
anamika
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

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

the same


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

Accepted Solution

by:
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_LOCAL_APPDATA = &H1C&
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 Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PRINTERS = &H4

Private Sub Command1_Click()

Dim sPath As String
Dim RetVal As Long

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

RetVal = SHGetFolderPath(0, CSIDL_PROGRAM_FILES, 0, SHGFP_TYPE_CURRENT, sPath)

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"
    Case E_INVALIDARG
        ' nFolder is invalid
        MsgBox "An invalid folder ID was specified"
   
End Select

       
End Sub
0
 

Author Comment

by:anamika1977
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
0
 
LVL 4

Expert Comment

by:dreamvb
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.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

761 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