5 out of 6 rated this helpful - Rate this topic

SHGetKnownFolderPath function

Applies to: desktop apps only

Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID.

Syntax

HRESULT SHGetKnownFolderPath(
  __in      REFKNOWNFOLDERID rfid,
  __in      DWORD dwFlags,
  __in_opt  HANDLE hToken,
  __out     PWSTR *ppszPath
);

Parameters

rfid [in]

Type: REFKNOWNFOLDERID

A reference to the KNOWNFOLDERID that identifies the folder.

dwFlags [in]

Type: DWORD

Flags that specify special retrieval options. This value can be 0; otherwise, one or more of the KNOWN_FOLDER_FLAG values.

hToken [in, optional]

Type: HANDLE

An access token that represents a particular user. If this parameter is NULL, which is the most common usage, the function requests the known folder for the current user.

Request a specific user's folder by passing the hToken of that user. This is typically done in the context of a service that has sufficient privileges to retrieve the token of a given user. That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights. In addition to passing the user's hToken, the registry hive of that specific user must be mounted. See Access Control for further discussion of access control issues.

Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderPath to find folder locations (such as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the Default User folders requires administrator privileges.

ppszPath [out]

Type: PWSTR*

When this method returns, contains the address of a pointer to a null-terminated Unicode string that specifies the path of the known folder. The calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree. The returned path does not include a trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".

Return value

Type: HRESULT

Returns S_OK if successful, or an error value otherwise, including the following:

Return codeDescription
E_FAIL

Among other things, this value can indicate that the rfid parameter references a KNOWNFOLDERID which does not have a path (such as a folder marked as KF_CATEGORY_VIRTUAL).

E_INVALIDARG

Among other things, this value can indicate that the rfid parameter references a KNOWNFOLDERID that is not present on the system. Not all KNOWNFOLDERID values are present on all systems. Use IKnownFolderManager::GetFolderIds to retrieve the set of KNOWNFOLDERID values for the current system.

 

Remarks

This function replaces SHGetFolderPath. That older function is now simply a wrapper for SHGetKnownFolderPath.

Requirements

Minimum supported client

Windows Vista

Minimum supported server

Windows Server 2008

Header

Shlobj.h

DLL

Shell32.dll (version 6.0.6000 or later)

See also

SHGetKnownFolderIDList
SHSetKnownFolderPath
IKnownFolder::GetPath

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ