ScottyC
Jan 1st, 2000, 08:06 AM
Hi! Does anybody know of a way to extract the current desktop path from the system? I know that in NT for example, each user can have a different desktop path. It also varies between the standard location in Win95 and WinNT, and I would like to be able to find out the correct path for the current user.
Can anybody help me?
Mark Sreeves
Jan 3rd, 2000, 05:10 PM
This might help:
form code:
Option Explicit
Private Sub Command1_Click()
Dim regKey As Long
regKey = OpenRegKey(HKEY_CURRENT_USER, ROOT_REG_KEY)
MsgBox ReadRegValue(regKey, "Desktop")
CloseRegKey (regKey)
End Sub
Module code:
Option Explicit
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryValueExStr Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Public Const ERROR_SUCCESS = 0&
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const REG_SZ = 1
Public Const INVALID_HANDLE_VALUE = -1
Public Const MY_GENERAL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS
Public Const HKEY_CURRENT_USER = &H80000001
Public Const ROOT_REG_KEY = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Public Function ReadRegValue(ByVal Key As Long, ByVal ValueName As String) As String
' Read a value out of the registry
'
' Set the error handler
On Error Resume Next
Dim Buffer As String
Dim Length As Long
Length = 255
Buffer = String(Length, vbNullChar)
If RegQueryValueExStr(Key, ValueName, 0, REG_SZ, Buffer, Length) = ERROR_SUCCESS Then
ReadRegValue = Left(Buffer, Length - 1)
Else
ReadRegValue = ""
End If
End Function
Public Function OpenRegKey(ByVal Root As Long, ByVal Path As String) As Long
' Open the specified registry key and return the key.
'
' @param: Root - The key of an open root branch
' @param: Path - The path to the branch to open
'
' Set the error handler
On Error Resume Next
Dim MyRegKEy As Long
If RegOpenKeyEx(Root, Path, 0, MY_GENERAL_ACCESS, MyRegKEy) = ERROR_SUCCESS Then
OpenRegKey = MyRegKEy
Else
OpenRegKey = INVALID_HANDLE_VALUE
End If
End Function
Public Sub CloseRegKey(ByRef regKey As Long)
' Close the registry key
'
' Set the error handler
On Error Resume Next
If IsValidHandle(regKey) Then
RegCloseKey (regKey)
regKey = INVALID_HANDLE_VALUE
End If
End Sub
Public Function IsValidHandle(ByVal Handle As Long) As Boolean
' Check a handle to see if it is valid
'
' @return: Boolean indication of a valid hanndle
'
' Set the error handler
On Error Resume Next
IsValidHandle = Not (Handle = INVALID_HANDLE_VALUE)
End Function
------------------
Mark Sreeves
Analyst Programmer
Mark.Sreeves@Softlab.co.uk
A BMW Group Company
Frans C
Jan 4th, 2000, 01:34 AM
Or you could user the Shell api's
Option Explicit
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
Private Sub Command1_Click()
Dim retVal As Long
Dim lPidl As Long
Dim sPath As String
sPath = Space(255)
retVal = SHGetSpecialFolderLocation(Me.hWnd, 0, lPidl) '0 = desktop
If retVal = 0 Then
retVal = SHGetPathFromIDList(lPidl, sPath)
If retVal = 1 Then
sPath = Left(sPath, InStr(sPath, Chr(0)) - 1)
MsgBox sPath
End If
End If
End Sub
[This message has been edited by Frans C (edited 01-04-2000).]