Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows API call to get Program Files Folder path

Posted on 2002-05-01
7
Medium Priority
?
916 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

971 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