API

SHGetSpecialFolderPath 윈도우의 각종 경로 얻기(비스타 이상은 SHGetKnownFolderPath 추가됨)

디버그정 2016. 3. 5. 19:05

1.요약

SHGetSpecialFolderPath를 이용하여 윈도우에서 사용하는 특별한 폴더들의 패스를 얻어오는 방법을 설명합니다.


Window Vista 이후 버전은 SHGetKnownFolderPath 함수가 추가되었다.

Vista에서는 보안 기능(UAC)이 강화됨에 따라 일반 사용자 계정에서 접근(Writable) 가능한 폴더가 제한적으로 바뀌었습니다.

Vista 이전에는 SHGetFolderPath() 함수를 이용해서 특수 폴더의 경로를 구할 수 있었는데, Vista에서는 쓰기 가능한 폴더를 구할 수 있는 SHGetKnownFolderPath() 함수가 추가되었습니다.
SHGetKnownFolderPath 이용시는 함수 내부에서 경로 메모리를 할당하므로 호출부 측에서 CoTaskMemFree(경로) 해야 한다.


2.본문

SHGetSpecialFolderPath()를 이용하면 윈도우에서 사용하는 특별한 폴더들의 패스를 얻어올 수 있습니다.

파라미터는 윈도우 핸들, 폴더이름을 복사할 버퍼, 패스 플래그, 생성 플래그입니다.

세번째와 네번째는 각각 얻고자하는 패스를 지정하는 플래그와 만약 폴더가 없다면 생성할 것인지를 지정하는 플래그입니다.

네번째 플레그가 NULL(0)이 아니고 폴더가 없다면 폴더를 생성합니다. 세번째 플래그는 다음과 같은 폴더를 지정합니다.(MSDN 참조)

CSIDL_ADMINTOOLS
Version 5.0. File system directory that is used to store administrative tools for an individual user. The Microsoft Management Console will save customized consoles to this directory and it will roam with the user.

 

CSIDL_ALTSTARTUP
File system directory that corresponds to the user's nonlocalized Startup program group.

CSIDL_APPDATA
File system directory that serves as a common repository for application-specific data. A common path is C:\WINNT\Profiles\username\Application Data.

 

CSIDL_BITBUCKET
Virtual folder containing the objects in the user's Recycle Bin.

CSIDL_COMMON_ADMINTOOLS
Version 5.0. File system directory containing containing administrative tools for all users of the computer.

 

CSIDL_COMMON_ALTSTARTUP

File system directory that corresponds to the nonlocalized Startup program group for all users. Valid only for Windows NT? systems.

 

CSIDL_COMMON_APPDATA
Version 5.0. Application data for all users. A common path is C:\WINNT\Profiles\All Users\Application Data.

 

CSIDL_COMMON_DESKTOPDIRECTORY
File system directory that contains files and folders that appear on the desktop for all users. A common path is C:\WINNT\Profiles\All Users\Desktop. Valid only for Windows NT? systems.

 

CSIDL_COMMON_DOCUMENTS
File system directory that contains documents that are common to all users. A common path is C:\WINNT\Profiles\All Users\Documents. Valid only for Windows NT? systems.

 

CSIDL_COMMON_FAVORITES
File system directory that serves as a common repository for all users' favorite items. Valid only for Windows NT? systems.

 

CSIDL_COMMON_PROGRAMS
File system directory that contains the directories for the common program groups that appear on the Start menu for all users. A common path is c:\WINNT\Profiles\All Users\Start Menu\Programs. Valid only for Windows NT? systems.

 

CSIDL_COMMON_STARTMENU
File system directory that contains the programs and folders that appear on the Start menu for all users. A common path is C:\WINNT\Profiles\All Users\Start Menu. Valid only for Windows NT? systems.

 

CSIDL_COMMON_STARTUP
File system directory that contains the programs that appear in the Startup folder for all users. A common path is C:\WINNT\Profiles\All Users\Start Menu\Programs\Startup. Valid only for Windows NT? systems.

 

CSIDL_COMMON_TEMPLATES
File system directory that contains the templates that are available to all users. A common path is C:\WINNT\Profiles\All Users\Templates. Valid only for Windows NT? systems.

 

CSIDL_CONTROLS
Virtual folder containing icons for the Control Panel applications.

 

CSIDL_COOKIES
File system directory that serves as a common repository for Internet cookies. A common path is C:\WINNT\Profiles\username\Cookies.

 

CSIDL_DESKTOP
Windows Desktop—virtual folder that is the root of the namespace.

 

CSIDL_DESKTOPDIRECTORY
File system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself). A common path is C:\WINNT\Profiles\username\Desktop

 

CSIDL_DRIVES
My Computer—virtual folder containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.

 

CSIDL_FAVORITES
File system directory that serves as a common repository for the user's favorite items. A common path is C:\WINNT\Profiles\username\Favorites.

 

CSIDL_FONTS
Virtual folder containing fonts. A common path is C:\WINNT\Fonts.

 

CSIDL_HISTORY
File system directory that serves as a common repository for Internet history items.

 

CSIDL_INTERNET
Virtual folder representing the Internet.

 

CSIDL_INTERNET_CACHE
File system directory that serves as a common repository for temporary Internet files. A common path is C:\WINNT\Profiles\username\Temporary Internet Files.

 

CSIDL_LOCAL_APPDATA
Version 5.0. File system directory that serves as a data repository for local (non-roaming) applications. A common path is C:\WINNT\Profiles\username\Local Settings\Application Data.

 

CSIDL_MYPICTURES
Version 5.0. My Pictures folder. A common path is C:\WINNT\Profiles\username\My Documents\My Pictures.

 

CSIDL_NETHOOD
File system directory containing objects that appear in the network neighborhood. A common path is

C:\WINNT\Profiles\username\NetHood.

 

CSIDL_NETWORK
Network Neighborhood—virtual folder representing the top level of the network hierarchy.

 

CSIDL_PERSONAL
File system directory that serves as a common repository for documents. A common path is C:\WINNT\Profiles\username\My Documents.

 

CSIDL_PRINTERS
Virtual folder containing installed printers.

 

CSIDL_PRINTHOOD
File system directory that serves as a common repository for printer links. A common path is C:\WINNT\Profiles\username\PrintHood.

 

CSIDL_PROFILE
Version 5.0. User's profile folder.

 

CSIDL_PROGRAM_FILES
Version 5.0. Program Files folder. A common path is C:\Program Files.

 

CSIDL_PROGRAM_FILES_COMMON
Version 5.0. Program Files folder that is common to all users. A common path is C:\Program Files\Common. Valid only for Windows NT? systems.

 

CSIDL_PROGRAM_FILES_COMMONX86
Version 5.0. Program Files folder that is common to all users for x86 applications on RISC systems. A common path is C:\Program Files (x86)\Common.

 

CSIDL_PROGRAM_FILESX86
Version 5.0. Program Files folder for x86 applications on RISC systems. Corresponds to the %PROGRAMFILES(X86)% environment variable. A common path is C:\Program Files (x86).

 

CSIDL_PROGRAMS
File system directory that contains the user's program groups (which are also file system directories). A common path is C:\WINNT\Profiles\username\Start Menu\Programs.

 

CSIDL_RECENT
File system directory that contains the user's most recently used documents. A common path is C:\WINNT\Profiles\username\Recent. To create a shortcut in this folder, use SHAddToRecentDocs. In addition to creating the shortcut, this function updates the shell's list of recent documents and adds the shortcut to the Documents submenu of the Start menu.

 

CSIDL_SENDTO
File system directory that contains Send To menu items. A common path is c:\WINNT\Profiles\username\SendTo.

 

CSIDL_STARTMENU
File system directory containing Start menu items. A common path is c:\WINNT\Profiles\username\Start Menu.

 

CSIDL_STARTUP
File system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user logs onto Windows NT? or starts Windows? 95. A common path is C:\WINNT\Profiles\username\Start Menu\Programs\Startup.

 

CSIDL_SYSTEM
Version 5.0. System folder. A common path is C:\WINNT\SYSTEM32.

 

CSIDL_SYSTEMX86
Version 5.0. System folder for x86 applications on RISC systems. A common path is C:\WINNT\SYS32X86.

 

CSIDL_TEMPLATES
File system directory that serves as a common repository for document templates.

 

CSIDL_WINDOWS
Version 5.0. Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. A common path is C:\WINNT.

 


3.예제


TCHAR sz[MAX_PATH];
SHGetSpecialFolderPath(NULL, sz, CSIDL_DESKTOP, FALSE); // 바탕화면의 경로 얻기

SHGetSpecialFolderPath(NULL, sz, CSIDL_PERSONAL, FALSE); // 내 문서의 경로 얻기

 

 

참고)


//-------------------------------------------------------------------------
//
// SHGetSpecialFolderLocation
//
//  Caller should use SHGetMalloc to obtain an allocator that can free the pidl
//
//
//-------------------------------------------------------------------------
//
// registry entries for special paths are kept in :
#define REGSTR_PATH_SPECIAL_FOLDERS    REGSTR_PATH_EXPLORER TEXT("\\Shell Folders")


#define CSIDL_DESKTOP                   0x0000        // <desktop>
#define CSIDL_INTERNET                  0x0001        // Internet Explorer (icon on desktop)
#define CSIDL_PROGRAMS                  0x0002        // Start Menu\Programs
#define CSIDL_CONTROLS                  0x0003        // My Computer\Control Panel
#define CSIDL_PRINTERS                  0x0004        // My Computer\Printers
#define CSIDL_PERSONAL                  0x0005        // My Documents
#define CSIDL_FAVORITES                 0x0006        // <user name>\Favorites
#define CSIDL_STARTUP                   0x0007        // Start Menu\Programs\Startup
#define CSIDL_RECENT                    0x0008        // <user name>\Recent
#define CSIDL_SENDTO                    0x0009        // <user name>\SendTo
#define CSIDL_BITBUCKET                 0x000a        // <desktop>\Recycle Bin
#define CSIDL_STARTMENU                 0x000b        // <user name>\Start Menu
#define CSIDL_MYDOCUMENTS               0x000c        // logical "My Documents" desktop icon
#define CSIDL_MYMUSIC                   0x000d        // "My Music" folder
#define CSIDL_MYVIDEO                   0x000e        // "My Videos" folder
#define CSIDL_DESKTOPDIRECTORY          0x0010        // <user name>\Desktop
#define CSIDL_DRIVES                    0x0011        // My Computer
#define CSIDL_NETWORK                   0x0012        // Network Neighborhood (My Network Places)
#define CSIDL_NETHOOD                   0x0013        // <user name>\nethood
#define CSIDL_FONTS                     0x0014        // windows\fonts
#define CSIDL_TEMPLATES                 0x0015
#define CSIDL_COMMON_STARTMENU          0x0016        // All Users\Start Menu
#define CSIDL_COMMON_PROGRAMS           0X0017        // All Users\Start Menu\Programs
#define CSIDL_COMMON_STARTUP            0x0018        // All Users\Startup
#define CSIDL_COMMON_DESKTOPDIRECTORY   0x0019        // All Users\Desktop
#define CSIDL_APPDATA                   0x001a        // <user name>\Application Data
#define CSIDL_PRINTHOOD                 0x001b        // <user name>\PrintHood

#ifndef CSIDL_LOCAL_APPDATA
#define CSIDL_LOCAL_APPDATA             0x001c        // <user name>\Local Settings\Applicaiton Data (non roaming)
#endif // CSIDL_LOCAL_APPDATA

#define CSIDL_ALTSTARTUP                0x001d        // non localized startup
#define CSIDL_COMMON_ALTSTARTUP         0x001e        // non localized common startup
#define CSIDL_COMMON_FAVORITES          0x001f

#ifndef _SHFOLDER_H_
#define CSIDL_INTERNET_CACHE            0x0020
#define CSIDL_COOKIES                   0x0021
#define CSIDL_HISTORY                   0x0022
#define CSIDL_COMMON_APPDATA            0x0023        // All Users\Application Data
#define CSIDL_WINDOWS                   0x0024        // GetWindowsDirectory()
#define CSIDL_SYSTEM                    0x0025        // GetSystemDirectory()
#define CSIDL_PROGRAM_FILES             0x0026        // C:\Program Files
#define CSIDL_MYPICTURES                0x0027        // C:\Program Files\My Pictures
#endif // _SHFOLDER_H_

#define CSIDL_PROFILE                   0x0028        // USERPROFILE
#define CSIDL_SYSTEMX86                 0x0029        // x86 system directory on RISC
#define CSIDL_PROGRAM_FILESX86          0x002a        // x86 C:\Program Files on RISC

#ifndef _SHFOLDER_H_
#define CSIDL_PROGRAM_FILES_COMMON      0x002b        // C:\Program Files\Common
#endif // _SHFOLDER_H_

#define CSIDL_PROGRAM_FILES_COMMONX86   0x002c        // x86 Program Files\Common on RISC
#define CSIDL_COMMON_TEMPLATES          0x002d        // All Users\Templates

#ifndef _SHFOLDER_H_
#define CSIDL_COMMON_DOCUMENTS          0x002e        // All Users\Documents
#define CSIDL_COMMON_ADMINTOOLS         0x002f        // All Users\Start Menu\Programs\Administrative Tools
#define CSIDL_ADMINTOOLS                0x0030        // <user name>\Start Menu\Programs\Administrative Tools
#endif // _SHFOLDER_H_

#define CSIDL_CONNECTIONS               0x0031        // Network and Dial-up Connections
#define CSIDL_COMMON_MUSIC              0x0035        // All Users\My Music
#define CSIDL_COMMON_PICTURES           0x0036        // All Users\My Pictures
#define CSIDL_COMMON_VIDEO              0x0037        // All Users\My Video
#define CSIDL_RESOURCES                 0x0038        // Resource Direcotry

#ifndef _SHFOLDER_H_
#define CSIDL_RESOURCES_LOCALIZED       0x0039        // Localized Resource Direcotry
#endif // _SHFOLDER_H_

#define CSIDL_COMMON_OEM_LINKS          0x003a        // Links to All Users OEM specific apps
#define CSIDL_CDBURN_AREA               0x003b        // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
// unused                               0x003c
#define CSIDL_COMPUTERSNEARME           0x003d        // Computers Near Me (computered from Workgroup membership)

#ifndef _SHFOLDER_H_
#define CSIDL_FLAG_CREATE               0x8000        // combine with CSIDL_ value to force folder creation in SHGetFolderPath()
#endif // _SHFOLDER_H_

#define CSIDL_FLAG_DONT_VERIFY          0x4000        // combine with CSIDL_ value to return an unverified folder path
#define CSIDL_FLAG_NO_ALIAS             0x1000        // combine with CSIDL_ value to insure non-alias versions of the pidl
#define CSIDL_FLAG_PER_USER_INIT        0x0800        // combine with CSIDL_ value to indicate per-user init (eg. upgrade)
#define CSIDL_FLAG_MASK                 0xFF00        // mask for all possible flag values

 

4. Window Vista 이후 버젼은 SHGetKnownFolderPath() 함수를 사용