-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
131 lines (111 loc) · 4.23 KB
/
test.c
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
// linker flags: -lshell32 -lversion -luuid -lole32
#ifndef UNICODE
#define UNICODE
#endif // !UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif // !_UNICODE
#include <windows.h>
#include <stdio.h>
#include <shlobj.h>
#define showInfopW(name, path) \
printf(name ": %ls\n", path); \
CoTaskMemFree(path);
#define showInfoW(name) \
printf(name ": %ls\n", pathW); \
ZeroMemory(pathW, MAX_PATH * sizeof(WCHAR));
#define showInfoA(name) \
printf(name ": %s\n", pathA); \
ZeroMemory(pathA, MAX_PATH * sizeof(char));
STDAPI SHGetKnownFolderPath (REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
STDAPI SHGetKnownFolderIDList (REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PIDLIST_ABSOLUTE *ppidl);
HRESULT WINAPI SHGetFolderPathEx(
_In_ REFKNOWNFOLDERID rfid,
_In_ DWORD dwFlags,
_In_opt_ HANDLE hToken,
_Out_ LPWSTR pszPath,
_In_ UINT cchPath
);
/**
* GetModulePathA
* @param {CHAR *} pDirBuf - destination buffer
* @param {DWORD} bufSize - size of buffer
* @return {DWORD} length of module path
*/
DWORD WINAPI GetModulePathA(CHAR *pDirBuf, DWORD bufSize) {
CHAR *szEnd = NULL;
GetModuleFileNameA(NULL, pDirBuf, bufSize);
szEnd = strrchr(pDirBuf, '\\');
*(szEnd) = 0;
return szEnd - pDirBuf;
}
/**
* GetModulePathW
* @param {WCHAR *} pDirBuf - destination buffer
* @param {DWORD} bufSize - size of buffer
* @return {DWORD} length of module path
*/
DWORD WINAPI GetModulePathW(WCHAR *pDirBuf, DWORD bufSize) {
WCHAR* szEnd = NULL;
GetModuleFileNameW(NULL, pDirBuf, bufSize);
szEnd = wcsrchr(pDirBuf, L'\\');
*(szEnd) = 0;
return szEnd - pDirBuf;
}
int mainCRTStartup() {
WCHAR versionDllW[MAX_PATH] = {0};
GetSystemDirectory(versionDllW, MAX_PATH);
lstrcat(versionDllW, L"\\version.dll");
LPDWORD lpdwHandle = NULL;
DWORD size = GetFileVersionInfoSizeW(versionDllW, lpdwHandle);
printf("GetFileVersionInfoSizeW: %lu\n", size);
WCHAR modulePathW[MAX_PATH] = {0};
GetModulePathW(modulePathW, MAX_PATH);
printf("GetModulePathW: %ls\n", modulePathW);
char modulePathA[MAX_PATH] = {0};
GetModulePathA(modulePathA, MAX_PATH);
printf("GetModulePathA: %s\n", modulePathA);
WCHAR pathW[MAX_PATH] = {0};
char pathA[MAX_PATH] = {0};
SHGetSpecialFolderPathW(NULL, pathW, CSIDL_APPDATA, FALSE);
showInfoW("SHGetSpecialFolderPathW");
SHGetSpecialFolderPathA(NULL, pathA, CSIDL_APPDATA, FALSE);
showInfoA("SHGetSpecialFolderPathA");
LPITEMIDLIST pidl = 0;
SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl);
SHGetPathFromIDListW(pidl, pathW);
showInfoW("SHGetSpecialFolderLocation");
SHGetPathFromIDListA(pidl, pathA);
showInfoA("SHGetSpecialFolderLocation");
ILFree(pidl);
pidl = 0;
WCHAR *ppathW = NULL;
SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &ppathW);
showInfopW("SHGetKnownFolderPath", ppathW);
SHGetKnownFolderIDList(&FOLDERID_RoamingAppData, 0, NULL, &pidl);
SHGetPathFromIDListW(pidl, pathW);
showInfoW("SHGetKnownFolderIDList");
SHGetPathFromIDListA(pidl, pathA);
showInfoA("SHGetKnownFolderIDList");
ILFree(pidl);
pidl = 0;
SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, L"sub1", pathW);
showInfoW("SHGetFolderPathAndSubDirW");
SHGetFolderPathAndSubDirA(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, "sub2", pathA);
showInfoA("SHGetFolderPathAndSubDirA");
SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, pathW);
showInfoW("SHGetFolderPathW");
SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, pathA);
showInfoA("SHGetFolderPathA");
SHGetFolderLocation(NULL, CSIDL_APPDATA, NULL, 0, &pidl);
SHGetPathFromIDListW(pidl, pathW);
showInfoW("SHGetFolderLocation");
SHGetPathFromIDListA(pidl, pathA);
showInfoA("SHGetFolderLocation");
ILFree(pidl);
pidl = 0;
SHGetFolderPathEx(&FOLDERID_RoamingAppData, 0, NULL, pathW, MAX_PATH);
showInfoW("SHGetFolderPathEx");
ExitProcess(0);
return 0;
}