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

Community
  • 1
  • 1
DavRob60
  • 3,517
  • 7
  • 34
  • 56
  • Eh? Anything in `Shell32.dll` **is** an API call. – MarkJ Jun 17 '10 at 18:03
  • @ MarkJ: I was saying that I don't want to add shell32.dll in the references (under the project menu). – DavRob60 Jun 18 '10 at 00:26
  • 2
    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 http://stackoverflow.com/questions/3054802/commonappdata-in-vb6/3073836#3073836 – MarkJ Jun 21 '10 at 12:16
  • 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! – DavRob60 Jun 21 '10 at 16:50
  • Yes, simply declare them As Object and all is well. – Bob77 Jun 22 '10 at 02:04

3 Answers3

9

Use late binding:

Const ssfCOMMONAPPDATA = &H23
Dim strCommonAppData As String

strCommonAppData = _
    CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path
Bob77
  • 13,167
  • 1
  • 29
  • 37
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
DavRob60
  • 3,517
  • 7
  • 34
  • 56
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 :)

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • Karl hasn't thought it through. That's a lot of baggage for something so trivial. – Bob77 Jun 19 '10 at 00:34
  • @Bob +1 to your answer that uses the shell object. – MarkJ Jun 21 '10 at 12:17
  • 3
    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. – Bob77 Jun 22 '10 at 02:01