Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Multi-User Support in Your Applications

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multi-User Support in Your Applications


    Use the following "GetDataStore" function to obtain a user-specific folder to store data files and settings for your applications. If the system cannot get a user-specific folder, then the application path is used instead.

    I use this code in my programs

    -Kev.

    Code:
    '------------------------------------------------------------------------------
    ' Returns the specified shell folder. NOTE: See %CSIDL_XXX values for dwType.
    ' NEW 2005: Doesn't need declare for call.
    '------------------------------------------------------------------------------
    Function GetShellFolder(ByVal hParent As Dword, ByVal dwType As Dword) As String
     
      Local hLib As Dword, hShell As Dword, zBuffer As Asciiz * %MAX_PATH
      Local nRet As Long, pzStr As Dword, nDoCreate As Long, sReturn As String
     
      sReturn = ""
      hLib = LoadLibrary("SHELL32.DLL")
      If hLib Then
         hShell = GetProcAddress(hLib, "SHGetSpecialFolderPathA")
         If hShell Then
            pzStr = VarPtr(zBuffer)
            nDoCreate = %TRUE
            ! push nDoCreate
            ! push dwType
            ! push pzStr
            ! push hParent
            ! call hShell
            ! mov nRet, eax
            If nRet Then sReturn = RTrim$(zBuffer, Any "\/")
         End If
         FreeLibrary hLib
      End If
      Function = sReturn
    End Function
     
    
    '------------------------------------------------------------------------------
    ' Returns application-specific data folder. Uses GetShellFolder() above.
    '------------------------------------------------------------------------------
    Function GetDataStore(BYVAL sCompany AS STRING, BYVAL sTitle AS STRING) As String
      Local zPath As Asciiz * 1024
      ' Get shell data folder...
      zPath = RTrim$(GetShellFolder(%NULL, %CSIDL_APPDATA), Any "\/")
      If Len(zPath) Then
         ' Use our moniker...
         zPath = zPath + "\" + sCompany
         If Dir$(zPath, %SUBDIR) = "" Then MkDir zPath
         ' Localize to application...
         zPath = zPath + "\" + sTitle
         If Dir$(zPath, %SUBDIR) = "" Then MkDir zPath
      Else
         ' Use application folder if shell data not supported by OS...
         GetModuleFileName %NULL, zPath, SizeOf(zPath)
         zPath = Left$(zPath, InStr(-1, zPath, Any "\/")-1)
      End If
      Function = zPath
    End Function
    ------------------
    contact me
    kgpsoftware.com - Free and Commercial Software
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

  • #2
    Quick example for using the above code (since the above was from 2005 I can't edit the post):

    Code:
     
    Function PBMAIN
     
      Local sPathToINI As String
     
      ' Get path to INI file...
      sPathToINI = GetDataStore("Your Business", "Program Title") + "\settings.ini"
     
    End Function
    Result on Vista (English):
    C:\Users\<user account>\AppData\Roaming\Your Business\Program Title\settings.ini

    Result on Windows XP (English):
    C:\Documents and Settings\<user account>\Application Data\Your Business\Program Title\settings.ini


    IIRC, will also work on Windows 2000, 98 and ME, only returns app path on '95 and NT4.

    The folder returned should always be writable.
    Last edited by Kev Peel; 11 May 2009, 06:42 AM.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      The following is an updated function with additional support for the common folder (XP's "All Users" folder), which is the same on all user accounts.

      Code:
      #Compile Exe
      #Dim All
      #Include "win32api.inc"
       
      '------------------------------------------------------------------------------
      ' Returns the specified shell folder. NOTE: See %CSIDL_XXX values for dwType.
      ' NEW 2005: Doesn't need declare for call.
      '------------------------------------------------------------------------------
      Function GetShellFolder(ByVal hParent As Dword, ByVal dwType As Dword) As String
       
        Local hLib As Dword, hShell As Dword, szPath As Asciiz * %MAX_PATH
        Local nRet As Long, pzStr As Dword, nDoCreate As Long
       
        hLib = LoadLibrary("SHELL32.DLL")
        If hLib Then
           hShell = GetProcAddress(hLib, "SHGetSpecialFolderPathA")
           If hShell Then
              pzStr = VarPtr(szPath)
              nDoCreate = %TRUE
              ! push nDoCreate
              ! push dwType
              ! push pzStr
              ! push hParent
              ! call hShell
              ! mov nRet, eax
              If nRet Then Function = RTrim$(szPath, Any "\/")
           End If
           FreeLibrary hLib
        End If
       
      End Function
       
      '------------------------------------------------------------------------------
      ' Returns application-specific data folder. Uses GetShellFolder() above.
      ' The path returned has been fully qualified (created), and should be writable.
      '
      ' NEW: If nAllUsers is TRUE (nonzero), then the equivalent "All Users" path is returned.
      '------------------------------------------------------------------------------
      Function GetDataStore(ByRef szCompany As Asciiz, ByRef szTitle As Asciiz, Optional ByVal nAllUsers As Long) As String
        Local szPath As Asciiz * 1024
        ' Get shell data folder...
        szPath = RTrim$(GetShellFolder(%NULL, IIf&(nAllUsers, %CSIDL_COMMON_APPDATA, %CSIDL_APPDATA)), Any "\/")
        If Len(szPath) Then
           ' Use our moniker...
           szPath = szPath + "\" + szCompany
           If Dir$(szPath, %SUBDIR) = "" Then MkDir szPath
           ' Localize to application...
           szPath = szPath + "\" + szTitle
           If Dir$(szPath, %SUBDIR) = "" Then MkDir szPath
        Else
           ' Use application folder if shell data not supported by OS...
           ' This is only expected to happen if the OS is Win95 or NT4.
           GetModuleFileName %NULL, szPath, SizeOf(szPath)
           szPath = Left$(szPath, InStr(-1, szPath, Any "\/")-1)
        End If
        Function = szPath
        
      End Function
       
       
      Function PBMain
       
        Local sPathToINI As String
       
        ' Get path to INI file...
        ' Note: specify TRUE instead of FALSE to return a path that is the same system-wide (from all user accounts)
        sPathToINI = GetDataStore("Your Business", "Program Title", %FALSE) + "\settings.ini"
        ? "Your INI file will be located at:" + $CrLf + $CrLf + sPathToINI
       
      End Function
      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

      Comment

      Working...
      X