-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetupBuild.cpp
More file actions
378 lines (320 loc) · 11.2 KB
/
SetupBuild.cpp
File metadata and controls
378 lines (320 loc) · 11.2 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// SetupBuild.cpp : Defines the entry point for the application.
//
#include "StdAfx.h"
#include "SetupBuild.h"
#include "VersionInfo.h"
#include "getopt.h"
DWORD GetConfigFilePath(CString& filePath);
DWORD LoadConfig(LPCTSTR szFileName, CString& configData);
CString GetStringProperties(LPCTSTR szProperty, CString& data);
DWORD AddResource(HANDLE hResource, LPCTSTR szResType, int resId, LPCTSTR szPath);
DWORD AddResourceHelper(HANDLE hResource, LPCTSTR szResType, int resId, CString const & path);
DWORD AddIconToResource(HANDLE hResource, int resId, LPBYTE pFileIcon, DWORD dwFileIconSize);
DWORD RemoveSetupIcons(HMODULE hModule, HANDLE hResource);
void help(void);
#pragma pack( push )
#pragma pack( 2 )
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // how many bytes in this resource?
WORD nID; // the ID
} GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
#pragma pack( pop )
// #pragmas are used here to insure that the structure's
// packing in memory matches the packing of the EXE or DLL.
#pragma pack( push )
#pragma pack( 2 )
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource type (1 for icons)
WORD idCount; // How many images?
// GRPICONDIRENTRY idEntries[]; // The entries for each image
} GRPICONDIR, *LPGRPICONDIR;
#pragma pack( pop )
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved ( must be 0)
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // How many bytes in this resource?
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource Type (1 for icons)
WORD idCount; // How many images?
//ICONDIRENTRY idEntries[]; // An entry for each image (idCount of 'em)
} ICONDIR, *LPICONDIR;
static bool verbose = false;
int _tmain(int argc, LPCTSTR* argv)
{
/* NOTES:
* 1) Read configuration from file before parsing command-line
* options so that the command-line options take precedence.
* 2) ...
*/
if (verbose) _ftprintf(stdout, _T("Setup Build Begin"));
CString filePath, configData;
GetConfigFilePath(filePath);
CString targetPath;
CString sourcePath;
CString configPath;
CString imagePath;
if (verbose) _ftprintf(stdout, _T("Loading config file %s\r\n"), (LPCTSTR) filePath);
DWORD retVal = LoadConfig(filePath, configData);
if (NO_ERROR == retVal)
{
targetPath = GetStringProperties(_T("Setup"), configData);
sourcePath = GetStringProperties(_T("Msi"), configData);
configPath = GetStringProperties(_T("Properties"), configData);
imagePath = GetStringProperties(_T("Icon"), configData);
}
/* check arguments */
while (true)
{
int c = getopt(argc, argv, _T("-hdo:i:p:s:"));
if (c == -1) break;
switch (c)
{
case 'h':
help();
exit(0);
case 'd':
verbose = true;
break;
case 'i':
imagePath = optarg;
break;
case 'o':
targetPath = optarg;
break;
case 'p':
configPath = optarg;
break;
case 's' :
sourcePath = optarg;
break;
}
}
HANDLE hResource = BeginUpdateResource(targetPath, FALSE);
if (NULL == hResource)
{
retVal = GetLastError();
_ftprintf(stderr, _T("Error opening prototypical setup file. %s\r\n"), (LPCTSTR)targetPath);
return retVal;
}
if (sourcePath.IsEmpty())
{
_ftprintf(stderr, _T("Missing MSI file.\r\n"));
return ERROR_BAD_ARGUMENTS;
}
retVal = AddResourceHelper(hResource, _T("FILES"), 1, sourcePath);
if (NO_ERROR != retVal)
return retVal;
retVal = AddResourceHelper(hResource, _T("FILES"), 2, configPath);
if (NO_ERROR != retVal)
return retVal;
retVal = AddResourceHelper(hResource, RT_GROUP_ICON, 1, imagePath);
if (NO_ERROR != retVal)
return retVal;
retVal = AddVersionResource(hResource, targetPath, sourcePath);
if (NO_ERROR != retVal)
return retVal;
EndUpdateResource(hResource, FALSE);
if (verbose) _ftprintf(stdout, _T("Setup Build Complete\r\n"));
return 0;
}
DWORD GetConfigFilePath(CString& filePath)
{
DWORD dwSize = (MAX_PATH + 1) * sizeof(TCHAR);
LPTSTR buffer = filePath.GetBuffer(dwSize);
GetCurrentDirectory(dwSize, buffer);
// GetModuleFileName(NULL, buffer, dwSize);
// PathRemoveFileSpec(buffer);
// SetCurrentDirectory(buffer);
PathAppend(buffer, _T("SetupBuild.config"));
filePath.ReleaseBuffer();
return NO_ERROR;
}
DWORD LoadConfig(LPCTSTR szFileName, CString& configData)
{
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
return GetLastError();
DWORD dwHighSize = 0;
DWORD size = GetFileSize(hFile, &dwHighSize);
size += sizeof(TCHAR);
LPTSTR buffer = configData.GetBuffer(size);
ZeroMemory(buffer, size);
DWORD bytesRead = 0;
if (!ReadFile(hFile, buffer, size, &bytesRead, NULL))
{
CloseHandle(hFile);
configData.ReleaseBuffer();
return GetLastError();
}
CloseHandle(hFile);
configData.ReleaseBuffer();
return NO_ERROR;
}
CString GetStringProperties(LPCTSTR szProperty, CString& data)
{
CString begin;
begin.Append(_T("<"));
begin.Append(szProperty);
begin.Append(_T(">"));
CString end;
end.Append(_T("</"));
end.Append(szProperty);
end.Append(_T(">"));
int indexBegin = data.Find(begin);
int indexEnd = data.Find(end);
CString result;
if (-1 < indexBegin && -1 < indexEnd && indexBegin < indexEnd)
{
indexBegin += begin.GetLength();
result = data.Mid(indexBegin, indexEnd - indexBegin);
}
return result;
}
DWORD AddResource(HANDLE hResource, LPCTSTR szResType, int resId, LPCTSTR szPath)
{
CAtlFile file;
HRESULT hr = file.Create(szPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
if (FAILED(hr))
return hr;
CAtlFileMappingBase fileMap;
hr = fileMap.MapFile(file);
if (FAILED(hr))
{
file.Close();
return hr;
}
DWORD dwError = NO_ERROR;
LPBYTE pData = (LPBYTE)fileMap.GetData();
DWORD dwSize = (DWORD)fileMap.GetMappingSize();
if (RT_GROUP_ICON == szResType)
{
// Icons need to be added differently
dwError = AddIconToResource(hResource, resId, pData, dwSize);
}
else
{
// Remove the existing resource
WORD wLanguage = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
UpdateResource(hResource, szResType, MAKEINTRESOURCE(resId), wLanguage, NULL, 0);
// Add the resource back
if (!UpdateResource(hResource, szResType, MAKEINTRESOURCE(resId), wLanguage, pData, dwSize))
dwError = GetLastError();
}
fileMap.Unmap();
file.Close();
return dwError;
}
DWORD AddResourceHelper(HANDLE hResource, LPCTSTR szResType, int resId, CString const & path)
{
DWORD dwError = NO_ERROR;
if (path.IsEmpty())
{
dwError = ERROR_BAD_ARGUMENTS;
_ftprintf(stderr, _T("Missing path to resource.\r\n"));
}
else
{
if (verbose) _ftprintf(stdout, _T("Adding %s to exe\r\n"), (LPCTSTR)path);
dwError = AddResource(hResource, szResType, resId, path);
}
if (NO_ERROR != dwError)
{
EndUpdateResource(hResource, TRUE);
_ftprintf(stderr, _T("Error adding %s to exe. %d\r\n"), (LPCTSTR)path, dwError);
}
return dwError;
}
BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
{
HANDLE hResource = (HANDLE)lParam;
if (RT_GROUP_ICON != lpszType && RT_ICON != lpszType)
return TRUE; // Continue enumeration
BOOL retVal = UpdateResource(hResource, lpszType, lpszName, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), NULL, 0);
return TRUE; // Continue enumeration
}
DWORD RemoveSetupIcons(HMODULE hModule, HANDLE hResource)
{
if (!EnumResourceNames(hModule, RT_ICON, EnumResNameProc, (LONG_PTR) hResource))
return GetLastError();
if (!EnumResourceNames(hModule, RT_GROUP_ICON, EnumResNameProc, (LONG_PTR) hResource))
return GetLastError();
return NO_ERROR;
}
DWORD AddIconToResource(HANDLE hResource, int resId, LPBYTE pFileIcon, DWORD dwFileIconSize)
{
DWORD dwError = NO_ERROR;
LPICONDIR pFileIconDir = (LPICONDIR)pFileIcon;
if (1 != pFileIconDir->idType)
return ERROR_INVALID_PARAMETER;
CLocalHeap heap;
int nGroupIconSize = sizeof(GRPICONDIR) + (pFileIconDir->idCount * sizeof(GRPICONDIRENTRY));
LPBYTE pGroupIcon = (LPBYTE)heap.Allocate(nGroupIconSize);
LPGRPICONDIR pGroupIconDir = (LPGRPICONDIR)(pGroupIcon);
pGroupIconDir->idReserved = pFileIconDir->idReserved;
pGroupIconDir->idType = pFileIconDir->idType;
pGroupIconDir->idCount = pFileIconDir->idCount;
LPICONDIRENTRY pIconDirEntry = (LPICONDIRENTRY)(pFileIcon + sizeof(ICONDIR));
LPGRPICONDIRENTRY pGroupIconDirEntry = (LPGRPICONDIRENTRY)(pGroupIcon + sizeof(GRPICONDIR));
int iconResID = resId;
WORD wLanguage = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
for(int i = 0; i < pFileIconDir->idCount; i++, iconResID++, pIconDirEntry++, pGroupIconDirEntry++)
{
pGroupIconDirEntry->bWidth = pIconDirEntry->bWidth;
pGroupIconDirEntry->bHeight = pIconDirEntry->bHeight;
pGroupIconDirEntry->bColorCount = pIconDirEntry->bColorCount;
pGroupIconDirEntry->bReserved = pIconDirEntry->bReserved;
pGroupIconDirEntry->wPlanes = pIconDirEntry->wPlanes;
pGroupIconDirEntry->wBitCount = pIconDirEntry->wBitCount;
pGroupIconDirEntry->dwBytesInRes = pIconDirEntry->dwBytesInRes;
pGroupIconDirEntry->nID = iconResID;
pIconDirEntry->dwBytesInRes;
LPBYTE pImage = pFileIcon + pIconDirEntry->dwImageOffset;
if (!UpdateResource(hResource, RT_ICON, MAKEINTRESOURCE(iconResID), wLanguage, pImage, pIconDirEntry->dwBytesInRes))
{
dwError = GetLastError();
heap.Free(pGroupIcon);
return dwError;
}
}
if (!UpdateResource(hResource, RT_GROUP_ICON, MAKEINTRESOURCE(resId), wLanguage, pGroupIcon, nGroupIconSize))
{
dwError = GetLastError();
}
heap.Free(pGroupIcon);
return NO_ERROR;
}
/*****************************************************************************
* help
*/
void help()
{
_tprintf(
_T("Install wrapper builder\n")
_T("Usage: SetupBuild [OPTION] [INPUT]\n")
_T(" INPUT set input MSI filename\n")
_T(" -h help menu (this screen)\n")
_T(" -i filename path to icon\n")
_T(" -p filename path to runtime configuration properties\n")
_T(" -o filename set output filename\n")
_T(" -d prints some debugging messages\n")
);
}