Jump to content

Getting the Local Application Data folder for the current user


Recommended Posts

I have tried a dllcall based on the SHGetSpecialFolderPath function from shell32.dll which was based on an example script however this only returns a boolean.

Const $CSIDL_LOCAL_APPDATA = 28

$localappdata = LOCAL_APPDATA()
    MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$localappdata' & @lf & @lf & 'Return:' & @lf & $localappdata & @lf & @lf & '@Error:' & @lf & @Error) ;### Debug MSGBOX


Func LOCAL_APPDATA()
Return SHGetSpecialFolderPath($CSIDL_LOCAL_APPDATA)
EndFunc

Func SHGetSpecialFolderPath($csidl)
;hwndOwner
;Reserved.

;lpszPath
;[out] A pointer to a null-terminated string that receives the drive and path of the 
;specified folder. This buffer must be at least MAX_PATH characters in size.

;csidl
;[in] A CSIDL that identifies the folder of interest. If a virtual folder is specified
;, this function will fail.

;fCreate
;[in] Indicates whether the folder should be created if it does not already exist. If 
;this value is nonzero, the folder is created. If this value is zero, the folder is
;not created.
Local $hwndOwner = 0 , $lpszPath = "" , $fCreate = False , $MAX_PATH = 260
$lpszPath = DllStructCreate("char[" & $MAX_PATH & "]")

$BOOL = DllCall("shell32.dll","int","SHGetSpecialFolderPath","int",$hwndOwner,"ptr", DllStructGetPtr($lpszPath),"int",$csidl,"int",$fCreate)
if Not @error Then
    

Return SetError($BOOL[0],0,$BOOL[0])
Else
Return SetError(@error,0,3)
EndFunc

Here is the link to the example I have lifted my code from

http://www.autoitscript.com/forum/index.php?showtopic=102783&st=0&p=729546&hl=$CSIDL_LOCAL_APPDATA&fromsearch=1&entry729546

Can also return the path as well (__out LPTSTR lpszPath) but how could I do this? See here for the MSDN documentation

http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx

Or is there another way for me to find the Local Application Data folder for the current user. There does not seem to be an Autoit macro for this value.

I need to find this information for all windows versions from Win 2000 onwards. Also would need this information if the user was logged on via a windows terminal server session as well.

Thank you in advance for any help that you can give me.

Link to comment
Share on other sites

Whipped this one up in a few seconds. Works on Win7 and seems to be the simplest way to do it.

MsgBox (0, "", _GetAppDir())

Func _GetAppDir()
    $old_opt = Opt ("ExpandEnvStrings", 1)
    $dir = "%APPDATA%"
    Opt ("ExpandEnvStrings", $old_opt)
    Return $dir
EndFunc

Cheers,

Brett

Link to comment
Share on other sites

Whipped this one up in a few seconds. Works on Win7 and seems to be the simplest way to do

Open a command prompt and type set and then press enter. Now look for "APPDATA" and "LOCALAPPDATA" and notice the difference. One is for roaming and the other is local. Win2k will not have "LOCALAPPDATA". Also use EnvGet() to use environmental variables which is easier. :)

@Lee Evans

Looks like you tried the minor change at the bottom of your linked post. The one at the top of that page works for me under WinXP.

Const $CSIDL_LOCAL_APPDATA = 28

$localappdata = LOCAL_APPDATA()
MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @LF & '$localappdata' & @LF & @LF & 'Return:' & @LF & $localappdata & @LF & @LF & '@Error:' & @LF & @error) ;### Debug MSGBOX

Func LOCAL_APPDATA()
    Return SHGetSpecialFolderPath($CSIDL_LOCAL_APPDATA)
EndFunc   ;==>LOCAL_APPDATA

Func SHGetSpecialFolderPath($csidl)
    ;hwndOwner
    ;Reserved.

    ;lpszPath
    ;[out] A pointer to a null-terminated string that receives the drive and path of the
    ;specified folder. This buffer must be at least MAX_PATH characters in size.

    ;csidl
    ;[in] A CSIDL that identifies the folder of interest. If a virtual folder is specified
    ;, this function will fail.

    ;fCreate
    ;[in] Indicates whether the folder should be created if it does not already exist. If
    ;this value is nonzero, the folder is created. If this value is zero, the folder is
    ;not created.
    Local $hwndOwner = 0, $lpszPath = "", $fCreate = False, $MAX_PATH = 260
    $lpszPath = DllStructCreate("char[" & $MAX_PATH & "]")
    $BOOL = DllCall("shell32.dll", "int", "SHGetSpecialFolderPath", "int", $hwndOwner, "ptr", _
            DllStructGetPtr($lpszPath), "int", $csidl, "int", $fCreate)
    If Not @error Then
;~      Return SetError($BOOL[0],0,$BOOL[0])
        Return SetError($BOOL[0], 0, DllStructGetData($lpszPath, 1))
    Else
        Return SetError(@error, 0, 0)
    EndIf
EndFunc   ;==>SHGetSpecialFolderPath

I get the output of:

---------------------------

Debug line ~4

---------------------------

Selection:

$localappdata

Return:

C:\Documents and Settings\Michael\Local Settings\Application Data

@Error:

0

---------------------------

OK

---------------------------

:(
Link to comment
Share on other sites

Sometimes you just can't see the wood for the trees.

@MHz you were right the first example returns exactly what I wanted which I thought the function could do as the MSDN documentation listed it as an output.

For anyone intereseted here is my fixed example

Const $CSIDL_LOCAL_APPDATA = 28

$localappdata = LOCAL_APPDATA()
    MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$localappdata' & @lf & @lf & 'Return:' & @lf & $localappdata & @lf & @lf & '@Error:' & @lf & @Error) ;### Debug MSGBOX


Func LOCAL_APPDATA()
Return SHGetSpecialFolderPath($CSIDL_LOCAL_APPDATA)
EndFunc

Func SHGetSpecialFolderPath($csidl)
;hwndOwner
;Reserved.

;lpszPath
;[out] A pointer to a null-terminated string that receives the drive and path of the 
;specified folder. This buffer must be at least MAX_PATH characters in size.

;csidl
;[in] A CSIDL that identifies the folder of interest. If a virtual folder is specified
;, this function will fail.

;fCreate
;[in] Indicates whether the folder should be created if it does not already exist. If 
;this value is nonzero, the folder is created. If this value is zero, the folder is
;not created.
Local $hwndOwner = 0 , $lpszPath = "" , $fCreate = False , $MAX_PATH = 260
$lpszPath = DllStructCreate("char[" & $MAX_PATH & "]")

$BOOL = DllCall("shell32.dll","int","SHGetSpecialFolderPath","int",$hwndOwner,"ptr", DllStructGetPtr($lpszPath),"int",$csidl,"int",$fCreate)
if Not @error Then
    

Return SetError($BOOL[0],0,DllStructGetData($lpszPath,1))
Else
Return SetError(@error,0,3)
EndIf

I was returning the boolean value and not the DllStructGetData($lpszPath,1)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...