-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapptest.cpp
151 lines (138 loc) · 4.79 KB
/
apptest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//*************************************************************************
// Copyright (c) 2013 Daniel D Miller
// This program is freeware, with no restrictions on use, of any sort.
// Finding Windows system directories
//
// Written by: Daniel D Miller
//
// build: g++ -Wall -O2 -DUNICODE -D_UNICODE -Wno-write-strings apptest.cpp -o apptest.exe
//*************************************************************************
// Data on UNICODE printf() is from:
// http://blog.kalmbachnet.de/?postid=98
//*************************************************************************
// Results:
//
// Windows 7 64-bit
// programs: [C:\Users\derelict\AppData\Roaming\Microsoft\Windows\Start Menu\Programs]
// personal: [C:\Users\derelict\Documents]
// appdata : [C:\Users\derelict\AppData\Roaming]
// common : [C:\ProgramData\Microsoft\Windows\Start Menu\Programs]
// sysroot : [C:\Windows]
// system : [C:\Windows\system32]
//
// Windows XP 32-bit
// programs: [C:\Documents and Settings\systest\Start Menu\Programs]
// personal: [C:\Documents and Settings\systest\My Documents]
// appdata : [C:\Documents and Settings\systest\Application Data]
// common : [C:\Documents and Settings\All Users\Start Menu\Programs]
// sysroot : [C:\WINDOWS]
// system : [C:\WINDOWS\system32]
//*************************************************************************
// the elevated __MSVCRT_VERSION__ declaration is required in order to
// enable _O_U16TEXT in fcntl.h
#define __MSVCRT_VERSION__ 0x0800
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h> // _O_U16TEXT
#include <shlobj.h> // CSIDL_*
#include <tchar.h>
typedef unsigned int uint ;
//**********************************************************************
void strip_newlines(char *rstr)
{
int slen = (int) strlen(rstr) ;
while (1) {
if (slen == 0)
break;
if (*(rstr+slen-1) == '\n' || *(rstr+slen-1) == '\r') {
slen-- ;
*(rstr+slen) = 0 ;
} else {
break;
}
}
}
//*************************************************************
// each subsequent call to this function overwrites
// the previous report.
//*************************************************************
char *get_system_message(void)
{
static char msg[261] ;
// int slen ;
LPVOID lpMsgBuf;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR) &lpMsgBuf,
0, 0);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
strncpy(msg, (char *) lpMsgBuf, 260) ;
// Free the buffer.
LocalFree( lpMsgBuf );
// trim the newline off the message before copying it...
strip_newlines(msg) ;
return msg;
}
//**********************************************************************
// https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpatha
// Only some CSIDL values are supported, including the following:
//
// CSIDL_ADMINTOOLS
// CSIDL_APPDATA
// CSIDL_COMMON_ADMINTOOLS
// CSIDL_COMMON_APPDATA
// CSIDL_COMMON_DOCUMENTS
// CSIDL_COOKIES
// CSIDL_FLAG_CREATE
// CSIDL_FLAG_DONT_VERIFY
// CSIDL_HISTORY
// CSIDL_INTERNET_CACHE
// CSIDL_LOCAL_APPDATA
// CSIDL_MYPICTURES
// CSIDL_PERSONAL
// CSIDL_PROGRAM_FILES
// CSIDL_PROGRAM_FILES_COMMON
// CSIDL_SYSTEM
// CSIDL_WINDOWS
//**********************************************************************
typedef struct shgfp_s {
int csidl_number ;
TCHAR *desc ;
} shgfp_t ;
shgfp_t path_info[] = {
{ CSIDL_PROGRAMS, L"programs" },
{ CSIDL_PERSONAL, L"personal" },
{ CSIDL_APPDATA, L"appdata" },
{ CSIDL_COMMON_APPDATA, L"common appdata" },
{ CSIDL_COMMON_PROGRAMS, L"common" },
{ CSIDL_COMMON_DOCUMENTS, L"common documents" },
{ CSIDL_WINDOWS, L"sysroot" },
{ CSIDL_SYSTEM, L"system" },
{ CSIDL_COOKIES, L"cookies" },
{ CSIDL_INTERNET_CACHE, L"inet cache" },
{ 0, L"end of list" }} ;
int main(void)
{
TCHAR szPath[MAX_PATH];
_setmode(_fileno(stdout), _O_U16TEXT); // enable UNICODE printf()
uint idx ;
for (idx=0; path_info[idx].csidl_number != 0; idx++) {
HRESULT result = SHGetFolderPath(NULL,
path_info[idx].csidl_number,
NULL, 0, szPath) ;
if (result == 0) {
_tprintf(L"%-17s: [%s]\n", path_info[idx].desc, szPath) ;
} else {
_tprintf(L"%-17s: \n", path_info[idx].desc, get_system_message()) ;
}
}
return 0;
}