-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysToolX.c
289 lines (273 loc) · 7.32 KB
/
SysToolX.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <windows.h>
#include <wininet.h>
#include "SysToolX.h"
void FreeMem(void *block) {
if (block) {
LocalFree(block);
}
}
void *GetMem(DWORD dwSize) {
return((void *) LocalAlloc(LPTR, dwSize));
}
void *GrowMem(void *block, DWORD dwSize) {
if (!dwSize) {
if (block) {
FreeMem(block);
}
block = NULL;
} else {
if (block) {
block = (void *) LocalReAlloc(block, dwSize, LMEM_MOVEABLE | LMEM_ZEROINIT);
} else {
block = GetMem(dwSize);
}
}
return(block);
}
TCHAR *LangLoadString(UINT sid) {
TCHAR *res;
WORD *p;
HRSRC hr;
int i;
res = NULL;
hr = FindResource(NULL, MAKEINTRESOURCE(sid / 16 + 1), RT_STRING);
p = hr ? (WORD *) LockResource(LoadResource(NULL, hr)) : NULL;
if (p != NULL) {
for (i = 0; i < (sid & 15); i++) {
p += 1 + *p;
}
res = STR_ALLOC(*p);
LoadString(NULL, sid, res, *p + 1);
}
return(res);
}
/* == COMMAD LINE ROUTINES ================================================= */
/* http://msdn.microsoft.com/en-us/library/17w5ykft.aspx */
#define CMD_PARSE_QUOTE 1
#define CMD_PARSE_COPY 2
void ParseCmdLine(TCHAR *p, TCHAR **argv, TCHAR *lpstr, DWORD *numargs, DWORD *numbytes) {
DWORD flags;
DWORD slashes;
*numbytes = 0;
*numargs = 1;
if (argv) {
*argv++ = lpstr;
}
flags = (*p == TEXT('\"')) ? 1 : 0;
p += flags;
while (
(*p != TEXT('\0')) && (
( flags && (*p != TEXT('\"'))) ||
((!flags) && (*p != TEXT('\t')) && (*p != TEXT(' ')))
)
) {
*numbytes += sizeof(TCHAR);
if (lpstr) {
*lpstr++ = *p;
}
p++;
}
*numbytes += sizeof(TCHAR);
if (lpstr) {
*lpstr++ = TEXT('\0');
}
flags &= (*p == TEXT('\"')) ? 1 : 0;
p += flags;
flags = 0;
for ( ; ; ) {
while ((*p == TEXT(' ')) || (*p == TEXT('\t'))) {
++p;
}
if (*p == TEXT('\0')) {
break;
}
if (argv) {
*argv++ = lpstr;
}
++*numargs;
for ( ; ; ) {
flags |= CMD_PARSE_COPY;
slashes = 0;
while (*p == TEXT('\\')) {
++p;
++slashes;
}
if (*p == TEXT('\"')) {
if ((slashes % 2) == 0) {
if (flags & CMD_PARSE_QUOTE) {
if (p[1] == TEXT('\"')) {
p++;
} else {
flags ^= CMD_PARSE_COPY;
}
} else {
flags ^= CMD_PARSE_COPY;
}
flags ^= CMD_PARSE_QUOTE;
}
slashes /= 2;
}
while (slashes--) {
if (lpstr) {
*lpstr++ = TEXT('\\');
}
*numbytes += sizeof(TCHAR);
}
if ((*p == TEXT('\0')) || ((!(flags & CMD_PARSE_QUOTE)) && ((*p == TEXT(' ')) || (*p == TEXT('\t'))))) {
break;
}
if (flags & CMD_PARSE_COPY) {
if (lpstr) {
*lpstr++ = *p;
}
*numbytes += sizeof(TCHAR);
}
++p;
}
if (lpstr) {
*lpstr++ = TEXT('\0');
}
*numbytes += sizeof(TCHAR);
}
}
TCHAR **GetCmdLineArgs(DWORD *argc) {
TCHAR **argv;
TCHAR *cmdline;
DWORD cmdsize;
TCHAR prgname[MAX_PATH];
argv = NULL;
if (argc) {
cmdline = GetCommandLine();
if ((!cmdline) || (!*cmdline)) {
GetModuleFileName(NULL, prgname, MAX_PATH);
cmdline = prgname;
}
ParseCmdLine(cmdline, NULL, NULL, argc, &cmdsize);
argv = (TCHAR **) GetMem((*argc + 1) * sizeof(cmdline) + cmdsize);
if (argv) {
argv[*argc] = NULL;
ParseCmdLine(
cmdline, argv,
(TCHAR *) (((BYTE *) argv) + (*argc + 1) * sizeof(cmdline)),
argc, &cmdsize
);
}
}
return(argv);
}
/* == FILE AND DISK ROUTINES =============================================== */
TCHAR *GetFullFilePath(TCHAR *filename) {
TCHAR *result, *np;
int sz;
sz = GetFullPathName(filename, 0, NULL, &np);
result = STR_ALLOC(sz);
if (result) {
GetFullPathName(filename, sz + 1, result, &np);
result[sz] = 0;
}
return(result);
}
TCHAR *GetWndText(HWND wnd) {
TCHAR *result;
int sz;
sz = GetWindowTextLength(wnd);
result = STR_ALLOC(sz);
if (result) {
GetWindowText(wnd, result, sz + 1);
result[sz] = 0;
}
return(result);
}
TCHAR *OpenSaveDialog(HWND wnd, TCHAR *filemask, TCHAR *defext, int savedlg) {
OPENFILENAME ofn;
TCHAR filename[MAX_PATH], *result;
result = NULL;
filename[0] = 0;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = wnd;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFile = filename;
ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; // | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR
/// OFN_ALLOWMULTISELECT
// http://stackoverflow.com/questions/656655/getopenfilename-with-ofn-allowmultiselect-flag-set
// http://support.microsoft.com/kb/131462
ofn.lpstrFilter = filemask;
ofn.lpstrDefExt = defext;
if ((savedlg ? GetSaveFileName : GetOpenFileName)(&ofn)) {
result = GetFullFilePath(ofn.lpstrFile);
}
return(result);
}
// v1.1
void URLOpenLink(HWND wnd, TCHAR *s) {
CoInitialize(NULL);
ShellExecute(wnd, NULL, s, NULL, NULL, SW_SHOWNORMAL);
CoUninitialize();
}
int MsgBox(HWND wnd, TCHAR *lpText, UINT uType) {
int result;
TCHAR *s, *t;
s = NULL;
if (!HIWORD(lpText)) {
s = LangLoadString(LOWORD(lpText));
}
t = GetWndText(wnd);
result = MessageBox(wnd, s ? s : lpText, t, uType);
if (t) { FreeMem(t); }
if (s) { FreeMem(s); }
return(result);
}
// http://blogs.msdn.com/b/oldnewthing/archive/2004/08/04/208005.aspx
void DialogEnableWindow(HWND hdlg, int idControl, BOOL bEnable) {
HWND hctl;
hctl = GetDlgItem(hdlg, idControl);
if ((bEnable == FALSE) && (hctl == GetFocus())) {
SendMessage(hdlg, WM_NEXTDLGCTL, 0, FALSE);
}
EnableWindow(hctl, bEnable);
}
// PATCH: Windows 98 combobox fix (common controls version < 6)
// http://blogs.msdn.com/b/oldnewthing/archive/2006/03/10/548537.aspx
// ! http://vbnet.mvps.org/index.html?code/comboapi/comboheight.htm
void AdjustComboBoxHeight(HWND hWndCmbBox, DWORD MaxVisItems) {
RECT rc;
GetWindowRect(hWndCmbBox, &rc);
rc.right -= rc.left;
ScreenToClient(GetParent(hWndCmbBox), (POINT *) &rc);
rc.bottom = (MaxVisItems + 2) * SendMessage(hWndCmbBox, CB_GETITEMHEIGHT, 0, 0);
MoveWindow(hWndCmbBox, rc.left, rc.top, rc.right, rc.bottom, TRUE);
// PATCH: enable integral height, ComboBox must be created with CBS_NOINTEGRALHEIGHT
SetWindowLong(hWndCmbBox, GWL_STYLE, (GetWindowLong(hWndCmbBox, GWL_STYLE) | CBS_NOINTEGRALHEIGHT) ^ CBS_NOINTEGRALHEIGHT);
}
DWORD GetFileVersionMS(TCHAR *filename) {
DWORD result;
HINSTANCE hl;
HGLOBAL hg;
HRSRC hr;
BYTE *data;
result = 0;
hl = LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE);
if (hl) {
// http://stackoverflow.com/questions/13941837/how-to-get-version-info-from-resources
hr = FindResource(hl, MAKEINTRESOURCE(1), RT_VERSION);
if (hr) {
hg = LoadResource(hl, hr);
if (hg) {
data = (BYTE *) LockResource(hg);
// http://www.csn.ul.ie/~caolan/publink/winresdump/winresdump/doc/resfmt.txt
if (data) {
result = MEM_MOVE(data, WORD);
// "Each block of information is dword aligned." (c) link above
while ((result > sizeof(VS_FIXEDFILEINFO)) && (MEM_MOVE(data, DWORD) != VS_FFI_SIGNATURE)) {
result -= sizeof(DWORD);
data += sizeof(DWORD);
}
result = ((VS_FIXEDFILEINFO *) data)->dwFileVersionMS;
} // data
} // hg
} // hr
FreeLibrary(hl);
} // hl
return(result);
}