Skip to content

Commit 45db238

Browse files
author
skulltrail
committed
2 parents 001978f + a8bd1e3 commit 45db238

File tree

1 file changed

+187
-32
lines changed

1 file changed

+187
-32
lines changed

wrappers/tools/wct/CompatibilityTool.c

+187-32
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
#include <commctrl.h>
33
#include <tchar.h>
44
#include <winreg.h>
5+
#include <stdbool.h>
6+
#include <stdio.h>
57

68
// IDs dos controles
79
#define IDC_COMBOBOX 101
810
#define IDC_APPLY_BUTTON 102
911
#define IDC_DELETE_BUTTON 103
1012
#define IDC_CANCEL_BUTTON 104
1113

12-
// Opções do dropdown
14+
// Opções do dropdown
1315
const TCHAR* versions[] = {
1416
_T("Windows 2000"),
1517
_T("Windows XP SP3"),
@@ -25,7 +27,7 @@ const TCHAR* versions[] = {
2527
};
2628

2729

28-
// Valores associados às opções
30+
// Valores associados às opções
2931
const TCHAR* versionValues[] = {
3032
_T("5.0.2195"),
3133
_T("5.1.2600"),
@@ -40,12 +42,101 @@ const TCHAR* versionValues[] = {
4042
_T("10.0.22600")
4143
};
4244

43-
// Prototipo das funções
45+
// Prototipo das funções
4446
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
45-
void WriteToRegistry(const TCHAR*);
46-
void DeleteRegistryKey();
47+
void WriteToRegistry(const TCHAR*, bool);
48+
void DeleteRegistryKey(bool);
4749

4850
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
51+
LPTSTR cmdLine = GetCommandLine(); // Gets the full command line
52+
53+
// Skip the program's own name
54+
if (cmdLine[0] == _T('"'))
55+
{
56+
// If the program name is quoted, skip until the closing quote
57+
cmdLine = _tcschr(cmdLine + 1, _T('"'));
58+
if (cmdLine)
59+
cmdLine++;
60+
}
61+
else
62+
{
63+
// Otherwise, skip until the first space
64+
cmdLine = _tcschr(cmdLine, _T(' '));
65+
}
66+
67+
// Skip any spaces after the program name
68+
if (cmdLine)
69+
while (*cmdLine == _T(' '))
70+
cmdLine++;
71+
72+
// Check if there's anything left in the command line
73+
if (cmdLine && *cmdLine)
74+
{
75+
if (_tcsicmp(cmdLine, _T("/win2000")) == 0)
76+
{
77+
WriteToRegistry(versionValues[0], false); // Windows 2000
78+
return 0;
79+
}
80+
else if (_tcsicmp(cmdLine, _T("/winxp")) == 0)
81+
{
82+
WriteToRegistry(versionValues[1], false); // Windows XP SP3
83+
return 0;
84+
}
85+
else if (_tcsicmp(cmdLine, _T("/win2003")) == 0)
86+
{
87+
WriteToRegistry(versionValues[2], false); // Windows Server 2003 SP2
88+
return 0;
89+
}
90+
else if (_tcsicmp(cmdLine, _T("/winvista")) == 0)
91+
{
92+
WriteToRegistry(versionValues[3], false); // Windows Vista SP2
93+
return 0;
94+
}
95+
else if (_tcsicmp(cmdLine, _T("/win7")) == 0)
96+
{
97+
WriteToRegistry(versionValues[4], false); // Windows 7 SP1
98+
return 0;
99+
}
100+
else if (_tcsicmp(cmdLine, _T("/win8")) == 0)
101+
{
102+
WriteToRegistry(versionValues[5], false); // Windows 8.1
103+
return 0;
104+
}
105+
else if (_tcsicmp(cmdLine, _T("/win10")) == 0)
106+
{
107+
WriteToRegistry(versionValues[9], false); // Windows 10 22H2
108+
return 0;
109+
}
110+
else if (_tcsicmp(cmdLine, _T("/win11")) == 0)
111+
{
112+
WriteToRegistry(versionValues[10], false); // Windows 11 24H2
113+
return 0;
114+
}
115+
else if (_tcsicmp(cmdLine, _T("/delete")) == 0)
116+
{
117+
DeleteRegistryKey(false);
118+
return 0;
119+
}
120+
else
121+
{
122+
MessageBox(
123+
NULL,
124+
_T("Usage: wct.exe [options]\n\n"
125+
"Options:\n"
126+
"/win2000 - Set Windows 2000 compatibility\n"
127+
"/winxp - Set Windows XP SP3 compatibility\n"
128+
"/win2003 - Set Windows Server 2003 SP2 compatibility\n"
129+
"/winvista - Set Windows Vista SP2 compatibility\n"
130+
"/win7 - Set Windows 7 SP1 compatibility\n"
131+
"/win8 - Set Windows 8.1 compatibility\n"
132+
"/win10 - Set Windows 10 22H2 compatibility\n"
133+
"/win11 - Set Windows 11 24H2 compatibility\n"
134+
"/delete - Remove the compatibility"),
135+
_T("Help"), MB_OK | MB_ICONINFORMATION);
136+
return 1;
137+
}
138+
}
139+
49140
WNDCLASS wc = {0};
50141
HWND hwnd;
51142
MSG msg = {0};
@@ -57,12 +148,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
57148

58149
if (!RegisterClass(&wc)) return -1;
59150

60-
hwnd = CreateWindow(
151+
hwnd = CreateWindowExW(
152+
WS_EX_APPWINDOW | WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME,
61153
_T("DropdownApp"),
62154
_T("Windows Compatibility Tool"),
63-
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
155+
WS_VISIBLE | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX,
64156
CW_USEDEFAULT, CW_USEDEFAULT,
65-
400, 200,
157+
398, 165,
66158
NULL, NULL, hInstance, NULL
67159
);
68160

@@ -74,6 +166,23 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
74166
return 0;
75167
}
76168

169+
// Function to enable or disable the window and its controls
170+
void HandleWindow(bool enable, HWND hComboBox, HWND hApplyButton, HWND hDeleteButton, HWND hCancelButton, HWND hwnd)
171+
{
172+
EnableWindow(hwnd, enable);
173+
EnableWindow(hComboBox, enable);
174+
EnableWindow(hApplyButton, enable);
175+
EnableWindow(hDeleteButton, enable);
176+
EnableWindow(hCancelButton, enable);
177+
EnableMenuItem(
178+
GetSystemMenu(hwnd, FALSE), SC_CLOSE,
179+
enable ? MF_BYCOMMAND | MF_ENABLED : MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
180+
EnableMenuItem(
181+
GetSystemMenu(hwnd, FALSE), SC_MINIMIZE,
182+
enable ? MF_BYCOMMAND | MF_ENABLED : MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
183+
}
184+
185+
77186
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
78187
static HWND hComboBox, hApplyButton, hDeleteButton, hCancelButton;
79188
static HFONT hFont;
@@ -97,14 +206,14 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
97206
GetModuleHandle(NULL), NULL
98207
);
99208

100-
// Adiciona as opções ao ComboBox
209+
// Adiciona as opções ao ComboBox
101210
for (i = 0; i < sizeof(versions) / sizeof(versions[0]); ++i) {
102211
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)versions[i]);
103212
}
104213

105-
// Definir o índice padrão (por exemplo, 1 - "Item 2")
214+
// Definir o índice padrão (por exemplo, 1 - "Item 2")
106215
SendMessage(hComboBox, CB_SETCURSEL, 0, 0);
107-
// Botão Aplicar
216+
// Botão Aplicar
108217
hApplyButton = CreateWindow(
109218
_T("BUTTON"), _T("Apply"),
110219
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
@@ -113,7 +222,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
113222
GetModuleHandle(NULL), NULL
114223
);
115224

116-
// Botão Deletar
225+
// Botão Deletar
117226
hDeleteButton = CreateWindow(
118227
_T("BUTTON"), _T("Delete"),
119228
WS_TABSTOP | WS_VISIBLE | WS_CHILD,
@@ -122,7 +231,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
122231
GetModuleHandle(NULL), NULL
123232
);
124233

125-
// Botão Cancelar
234+
// Botão Cancelar
126235
hCancelButton = CreateWindow(
127236
_T("BUTTON"), _T("Cancel"),
128237
WS_TABSTOP | WS_VISIBLE | WS_CHILD,
@@ -141,26 +250,34 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
141250

142251
case WM_COMMAND: {
143252
if (HIWORD(wParam) == CBN_SELCHANGE && LOWORD(wParam) == IDC_COMBOBOX) {
144-
// Seleção mudou no ComboBox
253+
// Seleção mudou no ComboBox
145254
}
146255

147256
switch (LOWORD(wParam)) {
148257
case IDC_APPLY_BUTTON: {
149-
// Obtém a seleção do ComboBox
258+
// Obtém a seleção do ComboBox
150259
int idx = SendMessage(hComboBox, CB_GETCURSEL, 0, 0);
260+
// Disable the window
261+
HandleWindow(FALSE, hComboBox, hApplyButton, hDeleteButton, hCancelButton, hwnd);
151262
if (idx != CB_ERR) {
152263
const TCHAR* selectedValue = versionValues[idx];
153-
WriteToRegistry(selectedValue);
264+
WriteToRegistry(selectedValue, true);
154265
} else {
155-
MessageBox(hwnd, _T("Please, select a version."), _T("Error"), MB_OK | MB_ICONERROR);
266+
MessageBox(hwnd, _T("Please select a version."), _T("Error"), MB_OK | MB_ICONERROR);
156267
}
268+
// Enable the window
269+
HandleWindow(TRUE, hComboBox, hApplyButton, hDeleteButton, hCancelButton, hwnd);
157270
break;
158271
}
159272

160273
case IDC_DELETE_BUTTON: {
161274
SendMessage(hComboBox, CB_SETCURSEL, -1, 0);
162-
DeleteRegistryKey();
163-
//MessageBox(hwnd, _T("Seleção deletada e chave de registro removida!"), _T("Deletar"), MB_OK | MB_ICONINFORMATION);
275+
// Disable the window
276+
HandleWindow(FALSE, hComboBox, hApplyButton, hDeleteButton, hCancelButton, hwnd);
277+
DeleteRegistryKey(true);
278+
//MessageBox(hwnd, _T("Seleção deletada e chave de registro removida!"), _T("Deletar"), MB_OK | MB_ICONINFORMATION);
279+
// Enable the window
280+
HandleWindow(TRUE, hComboBox, hApplyButton, hDeleteButton, hCancelButton, hwnd);
164281
break;
165282
}
166283

@@ -183,7 +300,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
183300
return 0;
184301
}
185302

186-
void WriteToRegistry(const TCHAR* value) {
303+
void WriteToRegistry(const TCHAR* value, bool useMessageBox) {
187304
HKEY hKey;
188305
int msgboxID;
189306
LONG result;
@@ -214,16 +331,32 @@ void WriteToRegistry(const TCHAR* value) {
214331
}
215332
#endif
216333
RegCloseKey(hKey);
217-
msgboxID = MessageBox(NULL, _T("Value saved with success on registry!"), _T("Success"), MB_OK | MB_ICONINFORMATION);
218-
if(msgboxID == IDOK){
219-
PostQuitMessage(0);
220-
}
334+
if (useMessageBox)
335+
{
336+
msgboxID = MessageBox(NULL, _T("Value saved with success on registry!"), _T("Success"), MB_OK | MB_ICONINFORMATION);
337+
338+
if (msgboxID == IDOK)
339+
{
340+
PostQuitMessage(0);
341+
}
342+
}
343+
else
344+
{
345+
PostQuitMessage(0);
346+
}
221347
} else {
222-
MessageBox(NULL, _T("Error while trying access the registry key."), _T("Error"), MB_OK | MB_ICONERROR);
348+
if (useMessageBox)
349+
{
350+
MessageBox(NULL, _T("Error while trying access the registry key."), _T("Error"), MB_OK | MB_ICONERROR);
351+
}
352+
else
353+
{
354+
PostQuitMessage(1);
355+
}
223356
}
224357
}
225358

226-
void DeleteRegistryKey() {
359+
void DeleteRegistryKey(bool useMessageBox) {
227360
HKEY hKey;
228361
LONG result;
229362
int msgboxID;
@@ -259,15 +392,37 @@ void DeleteRegistryKey() {
259392
#endif
260393

261394
if (result == ERROR_SUCCESS) {
262-
msgboxID = MessageBox(NULL, _T("Registry Key removed with success!"), _T("Success"), MB_OK | MB_ICONINFORMATION);
263-
if(msgboxID == IDOK){
264-
PostQuitMessage(0);
265-
}
395+
if (useMessageBox)
396+
{
397+
msgboxID = MessageBox(NULL, _T("Registry Key removed with success!"), _T("Success"), MB_OK | MB_ICONINFORMATION);
398+
if (msgboxID == IDOK)
399+
{
400+
PostQuitMessage(0);
401+
}
402+
}
403+
else
404+
{
405+
PostQuitMessage(0);
406+
}
266407
} else {
267-
MessageBox(NULL, _T("Error while trying remove the registry key value."), _T("Error"), MB_OK | MB_ICONERROR);
408+
if (useMessageBox)
409+
{
410+
MessageBox(NULL, _T("Error while trying remove the registry key value."), _T("Error"), MB_OK | MB_ICONERROR);
411+
}
412+
else
413+
{
414+
PostQuitMessage(1);
415+
}
268416
}
269417
} else {
270-
MessageBox(NULL, _T("Error trying open the registry key."), _T("Erro"), MB_OK | MB_ICONERROR);
418+
if (useMessageBox)
419+
{
420+
MessageBox(NULL, _T("Error trying open the registry key."), _T("Error"), MB_OK | MB_ICONERROR);
421+
}
422+
else
423+
{
424+
PostQuitMessage(1);
425+
}
271426
}
272427
}
273428

0 commit comments

Comments
 (0)