DXGL r415 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r414‎ | r415 | r416 >
Date:00:26, 31 March 2014
Author:admin
Status:new
Tags:
Comment:
Fix freeze in administrative mode appcompat deletion by moving out of DLLMain.
Update release version.
Modified paths:
  • /cfgmgr/cfgmgr.cpp (modified) (history)
  • /common/releasever.h (modified) (history)
  • /ddraw/ddraw.rc (modified) (history)

Diff [purge]

Index: cfgmgr/cfgmgr.cpp
@@ -32,7 +32,6 @@
3333 TCHAR regkeybase[] = _T("Software\\DXGL\\");
3434
3535 DXGLCFG defaultmask;
36 -UINT_PTR timerid;
3736
3837 INT_PTR CALLBACK CompatDialogCallback(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
3938 {
@@ -39,7 +38,7 @@
4039 switch (Msg)
4140 {
4241 case WM_INITDIALOG:
43 - timerid = SetTimer(hWnd, NULL, 5000, NULL);
 42+ SetTimer(hWnd, NULL, 3000, NULL);
4443 return TRUE;
4544 case WM_TIMER:
4645 EndDialog(hWnd, 0);
@@ -47,14 +46,22 @@
4847 default:
4948 return FALSE;
5049 }
 50+ return FALSE;
5151 }
5252
5353 void ShowRestartDialog()
5454 {
55 - BOOL(*_GetModuleHandleEx)(DWORD dwFlags, LPCTSTR lpModuleName, HMODULE* phModule) = FALSE;
 55+ BOOL(WINAPI *_GetModuleHandleEx)(DWORD dwFlags, LPCTSTR lpModuleName, HMODULE* phModule) = FALSE;
5656 HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
5757 HMODULE hddraw;
58 - if (hKernel32) _GetModuleHandleEx = (BOOL(*)(DWORD,LPCTSTR,HMODULE*))GetProcAddress(hKernel32, "GetModuleHandleEx");
 58+ if (hKernel32)
 59+ {
 60+#ifdef _UNICODE
 61+ _GetModuleHandleEx = (BOOL(WINAPI*)(DWORD, LPCTSTR, HMODULE*))GetProcAddress(hKernel32, "GetModuleHandleExW");
 62+#else
 63+ _GetModuleHandleEx = (BOOL(WINAPI*)(DWORD, LPCTSTR, HMODULE*))GetProcAddress(hKernel32, "GetModuleHandleExA");
 64+#endif
 65+ }
5966 if (_GetModuleHandleEx) _GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)ShowRestartDialog, &hddraw);
6067 else hddraw = GetModuleHandle(_T("ddraw.dll")); //For old versions of Windows, may fail but they shouldn't have AppCompat anyways.
6168 if (hKernel32) FreeLibrary(hKernel32);
@@ -85,7 +92,6 @@
8693 LRESULT error,error2;
8794 TCHAR filename[MAX_PATH + 1];
8895 TCHAR buffer[1024];
89 - TCHAR *bufferpos;
9096 error = RegCreateKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),
9197 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
9298 if (error == ERROR_SUCCESS)
@@ -134,10 +140,10 @@
135141 return false;
136142 }
137143
138 -bool DelCompatFlag(LPTSTR flag)
 144+bool DelCompatFlag(LPTSTR flag, bool initial)
139145 {
140146 HKEY hKey;
141 - HKEY hKeyWrite;
 147+ HKEY hKeyWrite = NULL;
142148 HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
143149 BOOL(WINAPI *iswow64)(HANDLE, PBOOL) = NULL;
144150 if (hKernel32) iswow64 = (BOOL(WINAPI*)(HANDLE, PBOOL))GetProcAddress(hKernel32, "IsWow64Process");
@@ -170,34 +176,49 @@
171177 {
172178 error = RegSetValueEx(hKeyWrite, filename, 0, REG_SZ, (BYTE*)buffer, (_tcslen(bufferpos + _tcslen(flag)))*sizeof(TCHAR)+sizeof(TCHAR));
173179 RegCloseKey(hKeyWrite);
 180+ hKeyWrite = NULL;
174181 }
175182 if (error == ERROR_ACCESS_DENIED)
176183 {
177 - if (MessageBox(NULL, _T("DXGL has detected an incompatible AppCompat flag for the program you are currently running and requires administrative rights to remove it.\nWould you like to continue?"),
178 - _T("AppCompat error"), MB_YESNO | MB_ICONWARNING) == IDYES)
 184+ if (!initial)
179185 {
180 - tstring command;
181 - if (is64) command.assign(_T("ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
182 - else command.assign(_T("ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
183 - command.append(filename);
184 - command.append(_T("\" /t REG_SZ /d \""));
185 - command.append(buffer);
186 - command.append(_T("\""));
187 - SHELLEXECUTEINFO info;
188 - ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
189 - info.cbSize = sizeof(SHELLEXECUTEINFO);
190 - info.lpVerb = _T("runas");
191 - info.lpFile = _T("reg.exe");
192 - info.lpParameters = command.c_str();
193 - info.nShow = SW_SHOWNORMAL;
194 - info.fMask = SEE_MASK_NOCLOSEPROCESS;
195 - ShellExecuteEx(&info);
196 - WaitForSingleObject(info.hProcess, INFINITE);
197 - GetExitCodeProcess(info.hProcess, (LPDWORD)&error);
 186+ if (MessageBox(NULL, _T("DXGL has detected an incompatible AppCompat flag for the program you are currently running and requires administrative rights to remove it.\nWould you like to continue?"),
 187+ _T("AppCompat error"), MB_YESNO | MB_ICONWARNING) == IDYES)
 188+ {
 189+ tstring command;
 190+ if (is64) command.assign(_T("ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
 191+ else command.assign(_T("ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
 192+ command.append(filename);
 193+ command.append(_T("\" /t REG_SZ /d \""));
 194+ command.append(buffer);
 195+ command.append(_T("\""));
 196+ SHELLEXECUTEINFO info;
 197+ ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
 198+ info.cbSize = sizeof(SHELLEXECUTEINFO);
 199+ info.lpVerb = _T("runas");
 200+ info.lpFile = _T("reg.exe");
 201+ info.lpParameters = command.c_str();
 202+ info.nShow = SW_SHOWNORMAL;
 203+ info.fMask = SEE_MASK_NOCLOSEPROCESS;
 204+ ShellExecuteEx(&info);
 205+ WaitForSingleObject(info.hProcess, INFINITE);
 206+ GetExitCodeProcess(info.hProcess, (LPDWORD)&error);
 207+ }
 208+ if (!error) ShowRestartDialog();
 209+ else
 210+ {
 211+ MessageBox(NULL, _T("Registry value could not be updated. Your program may crash as a result."), _T("Error"), MB_OK | MB_ICONWARNING);
 212+ if(hKeyWrite) RegCloseKey(hKeyWrite);
 213+ RegCloseKey(hKey);
 214+ }
 215+ return false;
198216 }
199 - if (!error) ShowRestartDialog();
200 - else MessageBox(NULL, _T("Registry value could not be updated. Your program may crash as a result."), _T("Error"), MB_OK | MB_ICONWARNING);
201 - return false;
 217+ else
 218+ {
 219+ if (hKeyWrite) RegCloseKey(hKeyWrite);
 220+ RegCloseKey(hKey);
 221+ return false;
 222+ }
202223 }
203224 else if (error == ERROR_SUCCESS) ShowRestartDialog();
204225 }
@@ -223,36 +244,51 @@
224245 {
225246 error = RegSetValueEx(hKeyWrite, filename, 0, REG_SZ, (BYTE*)buffer, (_tcslen(bufferpos + _tcslen(flag)))*sizeof(TCHAR)+sizeof(TCHAR));
226247 RegCloseKey(hKeyWrite);
 248+ hKeyWrite = NULL;
227249 }
228250 if (error == ERROR_ACCESS_DENIED)
229251 {
230 - if (MessageBox(NULL, _T("DXGL has detected an incompatible AppCompat flag for the program you are currently running and requires administrative rights to remove it.\nWould you like to continue?"),
231 - _T("AppCompat error"), MB_YESNO | MB_ICONWARNING) == IDYES)
 252+ if (hKeyWrite)
232253 {
233 - tstring command;
234 - command.assign(_T("ADD \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
235 - command.append(filename);
236 - command.append(_T("\" /t REG_SZ /d \""));
237 - command.append(buffer);
238 - command.append(_T("\""));
239 - SHELLEXECUTEINFO info;
240 - ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
241 - info.cbSize = sizeof(SHELLEXECUTEINFO);
242 - info.lpVerb = _T("runas");
243 - info.lpFile = _T("reg.exe");
244 - info.lpParameters = command.c_str();
245 - info.nShow = SW_SHOWNORMAL;
246 - info.fMask = SEE_MASK_NOCLOSEPROCESS;
247 - ShellExecuteEx(&info);
248 - WaitForSingleObject(info.hProcess, INFINITE);
249 - GetExitCodeProcess(info.hProcess, (LPDWORD)&error);
 254+ RegCloseKey(hKeyWrite);
 255+ hKeyWrite = NULL;
250256 }
251 - if (!error)
 257+ if (!initial)
252258 {
253 - ShowRestartDialog();
 259+ if (MessageBox(NULL, _T("DXGL has detected an incompatible AppCompat flag for the program you are currently running and requires administrative rights to remove it.\nWould you like to continue?"),
 260+ _T("AppCompat error"), MB_YESNO | MB_ICONWARNING) == IDYES)
 261+ {
 262+ tstring command;
 263+ command.assign(_T("ADD \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
 264+ command.append(filename);
 265+ command.append(_T("\" /t REG_SZ /d \""));
 266+ command.append(buffer);
 267+ command.append(_T("\""));
 268+ SHELLEXECUTEINFO info;
 269+ ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
 270+ info.cbSize = sizeof(SHELLEXECUTEINFO);
 271+ info.lpVerb = _T("runas");
 272+ info.lpFile = _T("reg.exe");
 273+ info.lpParameters = command.c_str();
 274+ info.nShow = SW_SHOWNORMAL;
 275+ info.fMask = SEE_MASK_NOCLOSEPROCESS;
 276+ ShellExecuteEx(&info);
 277+ WaitForSingleObject(info.hProcess, INFINITE);
 278+ GetExitCodeProcess(info.hProcess, (LPDWORD)&error);
 279+ }
 280+ if (!error)
 281+ {
 282+ ShowRestartDialog();
 283+ }
 284+ else MessageBox(NULL, _T("Registry value could not be updated. Your program may crash as a result."), _T("Error"), MB_OK | MB_ICONWARNING);
 285+ if (hKeyWrite) RegCloseKey(hKeyWrite);
 286+ return false;
254287 }
255 - else MessageBox(NULL, _T("Registry value could not be updated. Your program may crash as a result."), _T("Error"), MB_OK | MB_ICONWARNING);
256 - return false;
 288+ else
 289+ {
 290+ if (hKeyWrite) RegCloseKey(hKeyWrite);
 291+ return false;
 292+ }
257293 }
258294 else if (error == ERROR_SUCCESS)
259295 {
@@ -567,10 +603,10 @@
568604 hKey = NULL;
569605 HMODULE hSHCore = NULL;
570606 HMODULE hUser32 = NULL;
 607+ if (cfg->DPIScale == 2) AddCompatFlag(_T("HIGHDPIAWARE"));
 608+ else DelCompatFlag(_T("HIGHDPIAWARE"),initial);
571609 if (initial)
572610 {
573 - if (cfg->DPIScale == 2) AddCompatFlag(_T("HIGHDPIAWARE"));
574 - else DelCompatFlag(_T("HIGHDPIAWARE"));
575611 if (cfg->DPIScale == 1)
576612 {
577613 bool DPIAwarePM = false;
@@ -598,8 +634,8 @@
599635 if (hSHCore) FreeLibrary(hSHCore);
600636 if (hUser32) FreeLibrary(hUser32);
601637 }
602 - DelCompatFlag(_T("DWM8And16BitMitigation"));
603638 }
 639+ DelCompatFlag(_T("DWM8And16BitMitigation"), initial);
604640 }
605641 void GetGlobalConfig(DXGLCFG *cfg)
606642 {
Index: common/releasever.h
@@ -4,7 +4,7 @@
55
66 #define DXGLMAJORVER 0
77 #define DXGLMINORVER 5
8 -#define DXGLPOINTVER 1
 8+#define DXGLPOINTVER 2
99
1010 #define STR2(x) #x
1111 #define STR(x) STR2(x)
Index: ddraw/ddraw.rc
@@ -34,12 +34,12 @@
3535 // Dialog resources
3636 //
3737 LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
38 -IDD_COMPAT DIALOG 0, 0, 193, 19
 38+IDD_COMPAT DIALOG 0, 0, 196, 19
3939 STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_VISIBLE | WS_POPUP
4040 EXSTYLE WS_EX_WINDOWEDGE
4141 FONT 8, "Ms Shell Dlg"
4242 {
43 - LTEXT "Compatibility settings applied; now restarting your program.", IDC_STATIC, 5, 5, 183, 8, SS_LEFT
 43+ CTEXT "Compatibility settings applied; now restarting your program.", IDC_STATIC, 5, 5, 186, 8, SS_CENTER, WS_EX_LEFT
4444 }
4545
4646