Solved

Windows API call to get Program Files Folder path

Posted on 2002-05-01
7
882 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
[X]
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
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

Industry Leaders: 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 I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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