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
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/** (c) Nullsoft, Inc. C O N F I D E N T I A L
** Filename:
** Project:
** Description:
** Author: Ben Allison benski@nullsoft.com
** Created:
**/
#include <windows.h>
#include <shlwapi.h>
#include <strsafe.h>
#include <shlobj.h>
#include <shellapi.h>
#include "../Winamp/in2.h"
extern In_Module mod;
#ifdef __cplusplus
extern "C" BOOL UtilGetSpecialFolderPath(HWND hwnd, TCHAR *path, int folder)
#else
BOOL UtilGetSpecialFolderPath(HWND hwnd, TCHAR *path, int folder)
#endif
{
ITEMIDLIST *pidl; // Shell Item ID List ptr
IMalloc *imalloc; // Shell IMalloc interface ptr
BOOL result; // Return value
if (SHGetSpecialFolderLocation(hwnd, folder, &pidl) != NOERROR)
return FALSE;
result = SHGetPathFromIDList (pidl, path);
if (SHGetMalloc (&imalloc) == NOERROR)
{
#ifdef __cplusplus
imalloc->Free(pidl);
imalloc->Release();
#else
imalloc->lpVtbl->Free(imalloc, pidl);
imalloc->lpVtbl->Release(imalloc);
#endif
}
return result;
}
/*
This ugly function converts our specially coded paths
environment variables are surrounded with %'s and CSIDLs are surrounded with {}
note that CSIDLs need to be DECIMAL!
*/
#ifdef __cplusplus
extern "C" void ResolveEnvironmentVariables2(wchar_t *string, wchar_t *destString, size_t stringSize)
#else
void ResolveEnvironmentVariables2(TCHAR *string, TCHAR *destString, size_t stringSize)
#endif
{
//char *saveStart = string;
wchar_t *p = string;
int inPercent = 0, inBrace = 0;
wchar_t environString[MAX_PATH] = {0};
wchar_t *dest = destString;
wchar_t helper[MAX_PATH] = {0};
wchar_t *pEnv = environString;
*dest = 0;
while (p && *p)
{
if (*p == L'%')
{
if (inPercent)
{
*pEnv = 0;
helper[0] = 0;
GetEnvironmentVariableW(environString, helper, MAX_PATH);
StringCchCatW(dest, stringSize, helper);
pEnv = environString;
string = CharNextW(p);
*p = 0;
p = string;
}
else
{
wchar_t *newP = CharNextW(p);
*p = 0;
StringCchCatW(dest, stringSize, string);
string = p = newP;
}
inPercent = !inPercent;
}
else if (*p == L'{')
{
if (inPercent)
{
ptrdiff_t count = CharNextW(p) - p;
while (count-- && (pEnv - environString) < MAX_PATH)
*pEnv++ = *p++;
}
else
{
wchar_t *newP = CharNextW(p);
*p = 0;
StringCchCatW(dest, stringSize, string);
string = p = newP;
inBrace = 1;
}
}
else if (*p == L'}')
{
if (inPercent)
{
ptrdiff_t count = CharNextW(p) - p;
while (count-- && (pEnv - environString) < MAX_PATH)
*pEnv++ = *p++;
}
*pEnv = 0;
//SHGetSpecialFolderPath(NULL, helper, atoi(environString), FALSE);
#if defined(UNICODE) || defined(_UNICODE)
SHGetSpecialFolderPath(NULL, helper, StrToInt(environString), FALSE);
//UtilGetSpecialFolderPath(NULL, helper, _wtoi(environString));
#else
SHGetSpecialFolderPathW(NULL, helper, StrToIntW(environString), FALSE);
//UtilGetSpecialFolderPath(NULL, helper, _aoi(environString));
#endif
StringCchCatW(dest, stringSize, helper);
pEnv = environString;
string = CharNextW(p);
*p = 0;
p = string;
inBrace = 0;
}
else
{
if (inPercent || inBrace)
{
ptrdiff_t count = CharNextW(p) - p;
while (count-- && (pEnv - environString) < MAX_PATH)
*pEnv++ = *p++;
}
else
p = CharNextW(p);
}
}
StringCchCatW(dest, stringSize, string);
}
#ifndef NO_INPLACE_RESOLVE
#include <malloc.h>
// call this function if you want to modify in-place
#include "main.h"
#ifdef __cplusplus
extern "C" void ResolveEnvironmentVariables(wchar_t *string, size_t stringSize) //
#else
void ResolveEnvironmentVariables(wchar_t *string, size_t stringSize) //
#endif
{
wchar_t *dest = (wchar_t *) alloca(stringSize * sizeof(dest[0]));
ResolveEnvironmentVariables2(string, dest, stringSize);
StringCchCopyW(string, stringSize, dest);
}
#endif
|