-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIconChanger.c
240 lines (224 loc) · 8.11 KB
/
IconChanger.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
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include "resource.h"
// g (Global optimization), s (Favor small code), y (No frame pointers).
#pragma optimize("gsy", on)
#pragma comment(linker, "/FILEALIGN:0x200")
#pragma comment(linker, "/MERGE:.rdata=.data")
#pragma comment(linker, "/MERGE:.text=.data")
#pragma comment(linker, "/MERGE:.reloc=.data")
#pragma comment(linker, "/SECTION:.text, EWR /IGNORE:4078")
#pragma comment(linker, "/OPT:NOWIN98") // Make section alignment really small.
#define WIN32_LEAN_AND_MEAN
#define BUFFERSIZE 32768
typedef struct _ICONDIRENTRY {
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
DWORD dwImageOffset;
} ICONDIRENTRY,
* LPICONDIRENTRY;
typedef struct _ICONDIR {
WORD idReserved;
WORD idType;
WORD idCount;
ICONDIRENTRY idEntries[1];
} ICONDIR,
* LPICONDIR;
#pragma pack(push)
#pragma pack(2)
typedef struct _GRPICONDIRENTRY {
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
WORD nID;
} GRPICONDIRENTRY,
* LPGRPICONDIRENTRY;
#pragma pack(pop)
#pragma pack(push)
#pragma pack(2)
typedef struct _GRPICONDIR {
WORD idReserved;
WORD idType;
WORD idCount;
GRPICONDIRENTRY idEntries[1];
} GRPICONDIR,
* LPGRPICONDIR;
#pragma pack(pop)
BOOL CALLBACK Main(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);
HINSTANCE hInst;
HWND hDlg;
char szFileName[MAX_PATH]= "";
char iconfilename[MAX_PATH]= "";
char *szBuffer;
int WINAPI WinMain(HINSTANCE hinstCurrent,HINSTANCE hinstPrevious,LPSTR lpszCmdLine,int nCmdShow){
hInst= hinstCurrent;
DialogBox(hinstCurrent,MAKEINTRESOURCE(IDD_MAIN),NULL,(DLGPROC)Main);
return 0;
}
BOOL CALLBACK Main(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){
HDC hdc;
PAINTSTRUCT ps;
HMODULE hLibModule;
HANDLE hFile, hLoadFile= NULL, hIcon= NULL;
LPICONDIR lpID= NULL;
LPGRPICONDIR lpGID= NULL;
OPENFILENAME ofn, oin;
DWORD dwFileSize, dwBytesRead;
LPBYTE lpBuffer;
char szBackup[MAX_PATH];
int i;
HANDLE hUpdate;
switch(uMsg){
case WM_INITDIALOG:
SendMessageA(hDlg,WM_SETICON,ICON_SMALL, (LPARAM) LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON)));
SendMessageA(hDlg,WM_SETICON, ICON_BIG,(LPARAM) LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON)));
break;
case WM_COMMAND:
if( LOWORD(wParam)==IDC_LOADFILE ){
if( HIWORD(wParam)==BN_CLICKED ){
ZeroMemory(&ofn,sizeof(ofn));
szFileName[0]= '\0';
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner=hDlg;
ofn.lpstrFilter= "All files (*.*)\0*.*\0";
ofn.lpstrFile= szFileName;
ofn.nMaxFile= MAX_PATH;
ofn.Flags= OFN_EXPLORER|OFN_PATHMUSTEXIST;
GetOpenFileName(&ofn);
if( hLoadFile!=NULL ){
CloseHandle(hLoadFile);
}
hLoadFile= CreateFile(szFileName, GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if( hLoadFile==INVALID_HANDLE_VALUE ){
MessageBox(hDlg,"No File or could not open the selected file.","Error accessing file.",MB_OK);
}else{
SetDlgItemText(hDlg, IDC_FILENAME, szFileName);
}
CloseHandle(hLoadFile);
}
}
if( LOWORD(wParam)==IDC_LOADICON ){
if( HIWORD(wParam)==BN_CLICKED ){
ZeroMemory(&oin,sizeof(oin));
iconfilename[0]= '\0';
oin.lStructSize= sizeof(OPENFILENAME);
oin.hwndOwner= hDlg;
oin.lpstrFilter= "Icon files (*.ico)\0*.ico\0";
oin.lpstrFile= iconfilename;
oin.nMaxFile= MAX_PATH;
oin.Flags= OFN_EXPLORER|OFN_PATHMUSTEXIST;
GetOpenFileName(&oin);
if( hIcon!=NULL ){
CloseHandle(hIcon);
}
hIcon= CreateFile(iconfilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
dwFileSize= GetFileSize(hIcon, NULL);
szBuffer= (char*) malloc(4096);
memset(szBuffer, 0, sizeof(szBuffer));
ReadFile(hIcon, szBuffer, dwFileSize, &dwBytesRead, NULL);
if( hIcon==INVALID_HANDLE_VALUE ){
MessageBox(hDlg, "No Icon or could not open the selected icon.", "Error accessing icon.",MB_OK);
}else{
EnableWindow(GetDlgItem(hDlg,IDC_OK), TRUE);
CloseHandle(hIcon);
hIcon= ExtractIcon(hInst, iconfilename, 0);
SendDlgItemMessage(hDlg, IDC_ICONIMG, STM_SETICON, hIcon, 0);
}
}
}
if( LOWORD(wParam)==IDC_OK ){
if( HIWORD(wParam)==BN_CLICKED ){
// Create a backup of the .exe file.
GetDlgItemText(hDlg, IDC_FILENAME, szBackup, MAX_PATH);
lstrcat(szBackup, ".bak");
GetDlgItemText(hDlg, IDC_FILENAME, szFileName, MAX_PATH);
CopyFile(szFileName, szBackup, FALSE);
hFile= CreateFile(iconfilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if( hFile==INVALID_HANDLE_VALUE ){
MessageBox(hDlg, "Could not change icon!", "Error", MB_ICONERROR);
return TRUE;
}
lpID= (LPICONDIR)malloc(sizeof(ICONDIR));
ReadFile(hFile, &lpID->idReserved, sizeof(WORD), &dwBytesRead, NULL);
ReadFile(hFile, &lpID->idType, sizeof(WORD), &dwBytesRead, NULL);
ReadFile(hFile, &lpID->idCount, sizeof(WORD), &dwBytesRead, NULL);
lpID= (LPICONDIR)realloc(lpID, (sizeof(WORD) * 3) + (sizeof(ICONDIRENTRY) * lpID->idCount));
ReadFile(hFile, &lpID->idEntries[0], sizeof(ICONDIRENTRY) * lpID->idCount, &dwBytesRead, NULL);
lpGID= (LPGRPICONDIR)malloc(sizeof(GRPICONDIR));
lpGID->idReserved= lpID->idReserved;
lpGID->idType= lpID->idType;
lpGID->idCount= lpID->idCount;
lpGID= (LPGRPICONDIR)realloc(lpGID, (sizeof(WORD) * 3) + (sizeof(GRPICONDIRENTRY) * lpGID->idCount));
for( i=0; i<lpGID->idCount; i++){
lpGID->idEntries[i].bWidth= lpID->idEntries[i].bWidth;
lpGID->idEntries[i].bHeight= lpID->idEntries[i].bHeight;
lpGID->idEntries[i].bColorCount= lpID->idEntries[i].bColorCount;
lpGID->idEntries[i].bReserved= lpID->idEntries[i].bReserved;
lpGID->idEntries[i].wPlanes= lpID->idEntries[i].wPlanes;
lpGID->idEntries[i].wBitCount= lpID->idEntries[i].wBitCount;
lpGID->idEntries[i].dwBytesInRes= lpID->idEntries[i].dwBytesInRes;
lpGID->idEntries[i].nID= i+1;
}
GetDlgItemText(hDlg, IDC_FILENAME, szFileName, MAX_PATH);
hUpdate= BeginUpdateResource(szFileName, FALSE);
if( hUpdate==NULL ){
MessageBox(hDlg, "Could not change icon!", "Error", MB_ICONERROR);
return TRUE;
}
for( i=0; i<lpID->idCount; i++){
lpBuffer= (LPBYTE)malloc(lpID->idEntries[i].dwBytesInRes);
SetFilePointer(hFile, lpID->idEntries[i].dwImageOffset, NULL, FILE_BEGIN);
ReadFile(hFile, lpBuffer, lpID->idEntries[i].dwBytesInRes, &dwBytesRead, NULL);
if( UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(lpGID->idEntries[i].nID), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), &lpBuffer[0], lpID->idEntries[i].dwBytesInRes)==FALSE ){
MessageBox(hDlg, "Could not change icon!", "Error", MB_ICONERROR);
free(lpBuffer);
return TRUE;
}
free(lpBuffer);
}
CloseHandle(hFile);
if( UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), &lpGID[0], (sizeof(WORD) * 3) + (sizeof(GRPICONDIRENTRY) * lpGID->idCount))==FALSE ){
MessageBox(hDlg, "Could not change icon!", "Error", MB_ICONERROR);
return TRUE;
}
if( EndUpdateResource(hUpdate, FALSE)==FALSE ){
MessageBox(hDlg, "Could not change icon!", "Error", MB_ICONERROR);
return TRUE;
}
MessageBox(hDlg, "Icon successfully changed!", "Information", MB_ICONINFORMATION);
break;
}
}
if( LOWORD(wParam)==IDC_EXIT ){
if( HIWORD(wParam)==BN_CLICKED ){
EndDialog(hDlg,wParam);
DestroyWindow(hDlg);
}
}
break;
case WM_PAINT:
hdc= BeginPaint(hDlg, &ps);
InvalidateRect(hDlg, NULL, TRUE);
EndPaint (hDlg, &ps);
return 0;
case WM_CLOSE:
EndDialog(hDlg,wParam);
DestroyWindow(hDlg);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return 0;
}