-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVolumeSerialDlg.cpp
More file actions
326 lines (270 loc) · 8.19 KB
/
VolumeSerialDlg.cpp
File metadata and controls
326 lines (270 loc) · 8.19 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
// VolumeSerialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VolumeSerial.h"
#include "VolumeSerialDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVolumeSerialDlg dialog
CVolumeSerialDlg::CVolumeSerialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVolumeSerialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVolumeSerialDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVolumeSerialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVolumeSerialDlg)
DDX_Control(pDX, IDC_EDT_NEWSERIAL, m_edtNewSerial);
DDX_Control(pDX, IDC_EDT_INFORMATION, m_edtInformation);
DDX_Control(pDX, IDC_LB_DRIVES, m_lbDrives);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVolumeSerialDlg, CDialog)
//{{AFX_MSG_MAP(CVolumeSerialDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_LB_DRIVES, OnSelchangeLbDrives)
ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
ON_BN_CLICKED(IDC_BTN_CHANGESERIAL, OnBtnChangeserial)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVolumeSerialDlg message handlers
BOOL CVolumeSerialDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Populate the drives
PopulateDriveListBox();
m_edtNewSerial.SetWindowText(_T("12345678"));
/*
if (GetVersion() > 0x80000000)
{
MessageBox(_T("This program will only work in Windows NT based OS"), _T("Info"), MB_ICONINFORMATION);
GetDlgItem(IDC_BTN_CHANGESERIAL)->EnableWindow(FALSE);
}
*/
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVolumeSerialDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVolumeSerialDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::ShowErrorString(const DWORD nErr)
{
TCHAR szErrorMsg[100];
::FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nErr,
LANG_USER_DEFAULT,
(LPTSTR)szErrorMsg,
sizeof(szErrorMsg),
NULL);
MessageBox(szErrorMsg, _T("Error"), MB_ICONERROR);
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::OnOK()
{
CDialog::OnOK();
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::PopulateDriveListBox()
{
// clear old list
m_lbDrives.ResetContent();
// add a special flag that will allow the list to be refreshed
m_lbDrives.AddString(_T("-- refresh list --"));
m_lbDrives.SetItemData(0, 0);
// get all mapped drives (in form of bits)
DWORD iLogicalDrives = ::GetLogicalDrives();
int sel_drive_c = -1;
for (DWORD j=1, i=0; i< 26; i++)
{
// is drive bit set?
if (((1 << i) & iLogicalDrives) != 0)
{
// Add drive to list
int drive = 'A'+i;
CString str;
str.Format(_T("%c"), drive);
m_lbDrives.AddString(str);
m_lbDrives.SetItemData(j, drive);
if (drive == 'C')
sel_drive_c = j;
j++;
}
}
// try to select drive 'C' (assuming that drive C is the 2nd drive in the system)
if (sel_drive_c != -1)
m_lbDrives.SetCurSel(sel_drive_c);
//
OnSelchangeLbDrives();
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::OnSelchangeLbDrives()
{
int curSel = m_lbDrives.GetCurSel();
if (curSel == 0)
{
PopulateDriveListBox();
return;
}
GetDriveInfo(m_lbDrives.GetItemData(curSel));
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::GetDriveInfo(DWORD Drive)
{
DWORD serialNumber, maxComponentLength, fsFlags;
TCHAR szFileSystem[12];
TCHAR szVolumeName[24];
TCHAR szRoot[4] = "C:\\";
szRoot[0] = (TCHAR)(Drive & 0xFF);
if (!::GetVolumeInformation(
szRoot,
szVolumeName,
sizeof(szVolumeName),
&serialNumber,
&maxComponentLength,
&fsFlags,
szFileSystem,
sizeof(szFileSystem)
))
{
m_edtInformation.SetWindowText(_T("Failed to retrieve drive information"));
return;
}
CString info;
info.Format(_T("File system: %s\r\n")
_T("Serial Number: %04X-%04X\r\n")
_T("Label: %s"), szFileSystem, serialNumber >> 16, serialNumber & 0xFFFF, szVolumeName);
m_edtInformation.SetWindowText(info);
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::ChangeSerialNumber(DWORD Drive, const DWORD newSerial)
{
const size_t max_pbsi = 3;
struct partial_boot_sector_info
{
LPSTR Fs; // file system name
DWORD FsOffs; // offset of file system name in the boot sector
DWORD SerialOffs; // offset of the serial number in the boot sector
};
static const partial_boot_sector_info pbsi[max_pbsi] =
{
{"FAT32", 0x52, 0x43},
{"FAT", 0x36, 0x27},
{"NTFS", 0x03, 0x48}
};
TCHAR szDrive[12];
_snprintf(szDrive, _countof(szDrive), "%c:\\", Drive & 0xFF);
if (!disk.Open(szDrive))
{
ShowErrorString("Could not open disk!");
return;
}
// read sector
char Sector[512];
if (!disk.ReadSector(0, Sector))
{
ShowErrorString("Could not read sector!");
return;
}
// Try to search for a valid boot sector
DWORD i;
for (i=0;i<max_pbsi;i++)
{
if (strncmp(pbsi[i].Fs, Sector+pbsi[i].FsOffs, strlen(pbsi[i].Fs)) == 0)
{
// We found a valid signature
break;
}
}
if (i >= max_pbsi)
{
MessageBox(_T("Cannot change serial number of this file system!"), _T("Error"), MB_ICONERROR);
return;
}
// Patch the serial number
*(PDWORD)(Sector+pbsi[i].SerialOffs) = newSerial;
// write boot sector
if (!disk.WriteSector(0, Sector))
{
ShowErrorString("Could not write sector!");
return;
}
ShowErrorString("Volume serial number changed successfully!\r"
"You might want to restart your system for changes to take effect!");
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::OnBtnAbout()
{
MessageBox(_T("Change Volume Serial Number v1.3\n")
_T("(c) Elias Bachaalany <lallousz-x86@yahoo.com>\n"),
_T("About"),
MB_ICONINFORMATION);
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::OnBtnChangeserial()
{
if (MessageBox(_T("Are you sure you want to do this?"), _T("Question"), MB_ICONQUESTION | MB_ICONWARNING | MB_YESNO) == IDNO)
return;
int drive = m_lbDrives.GetItemData(m_lbDrives.GetCurSel() & 0xFF);
if (drive != 0)
{
TCHAR szNewSerial[10] = {0};
LPTSTR *temp = NULL;
m_edtNewSerial.GetWindowText(szNewSerial, sizeof(szNewSerial));
if (_tcslen(szNewSerial) != 8)
{
MessageBox(_T("Please enter a valid 8 digit hex number!"));
return;
}
DWORD newSerial = _tcstoul(szNewSerial, temp, 16);
ChangeSerialNumber(drive, newSerial);
}
}
//-------------------------------------------------------------------------
void CVolumeSerialDlg::ShowErrorString(const char *msg)
{
MessageBox(msg, "Info", MB_ICONINFORMATION);
}