CommonAppData in vb6
Asked Answered
J

3

7

Basically the same as this question, but for VB6.

A customer's application "AppName" has its configuration files stored in CommonAppData.

  • Under Windows XP that is C:\Documents and Settings\All Users\Application Data\AppName
  • Under Windows Vista that is C:\ProgramData\AppName

How do I get the correct foldername with VB6??

Additional notes, I prefer to use a API Call instead of adding a reference to the shell32.dll

Jola answered 16/6, 2010 at 15:35 Comment(5)
Eh? Anything in Shell32.dll is an API call.Laktasic
@ MarkJ: I was saying that I don't want to add shell32.dll in the references (under the project menu).Jola
You mean you don't want to use the VBScript code you originally linked, because you don't want to add any references? But you don't have to add any references, you can use late-binding. Like in Bob's answer https://mcmap.net/q/829745/-commonappdata-in-vb6/…Laktasic
I did not know about late-binding. I was under the impression that I was not able to do this because I did now know witch types to use to declare the objShell, objFolder and objFolderItem variables. Good to know!Jola
Yes, simply declare them As Object and all is well.Hornmad
H
9

Use late binding:

Const ssfCOMMONAPPDATA = &H23
Dim strCommonAppData As String

strCommonAppData = _
    CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path
Hornmad answered 19/6, 2010 at 0:28 Comment(0)
J
3

found it;

Private Declare Function SHGetFolderPath _
                        Lib "shfolder.dll" Alias "SHGetFolderPathA" _
                        (ByVal hwndOwner As Long, _
                         ByVal nFolder As Long, _
                         ByVal hToken As Long, _
                         ByVal dwReserved As Long, _
                         ByVal lpszPath As String) As Long
Private Const CSIDL_COMMON_APPDATA = &H23
Private Const CSIDL_COMMON_DOCUMENTS = &H2E

Public Function strGetCommonAppDataPath() As String
    Dim strPath As String

    strPath = Space$(512)
    Call SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, strPath)
    strPath = Left$(strPath, InStr(strPath, vbNullChar))

    strGetCommonAppDataPath = strPath
End Function
Jola answered 16/6, 2010 at 18:6 Comment(0)
L
2

Karl Peterson has published a drop-in VB6 class called CSystemFolders that will find CSIDL_APPDATA, CSIDL_LOCAL_APPDATA and CSIDL_COMMON_APPDATA.

Karl's code is always reliable, accept no substitutes :)

Laktasic answered 17/6, 2010 at 18:4 Comment(3)
Karl hasn't thought it through. That's a lot of baggage for something so trivial.Hornmad
@Bob +1 to your answer that uses the shell object.Laktasic
I'm a Karl fan too, but I think he goes overboard at times. Something like this is usually called only a very few times in the lifetime of a program, so performance isn't really an issue. Also, the Shell32 COM interface didn't maintain binary compatibility across all Windows versions so late binding is a good idea here.Hornmad

© 2022 - 2024 — McMap. All rights reserved.