DXGL r407 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r406‎ | r407 | r408 >
Date:19:08, 7 December 2013
Author:admin
Status:new
Tags:
Comment:
Check appcompat registry keys only on DLL load.
Modified paths:
  • /cfgmgr/cfgmgr.cpp (modified) (history)
  • /cfgmgr/cfgmgr.h (modified) (history)
  • /ddraw/ddraw.cpp (modified) (history)
  • /ddraw/dllmain.cpp (modified) (history)

Diff [purge]

Index: cfgmgr/cfgmgr.cpp
@@ -278,7 +278,7 @@
279279 else return false;
280280 }
281281
282 -void GetCurrentConfig(DXGLCFG *cfg)
 282+void GetCurrentConfig(DXGLCFG *cfg, bool initial)
283283 {
284284 HKEY hKey;
285285 unsigned long crc;
@@ -312,100 +312,103 @@
313313 ReadSettings(hKey,cfg,NULL,false,true,NULL);
314314 RegCloseKey(hKey);
315315 hKey = NULL;
316 - HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
317 - BOOL (WINAPI *iswow64)(HANDLE,PBOOL) = NULL;
318 - if(hKernel32) iswow64 = (BOOL(WINAPI*)(HANDLE,PBOOL))GetProcAddress(hKernel32,"IsWow64Process");
319 - BOOL is64 = FALSE;
320 - if(iswow64) iswow64(GetCurrentProcess(),&is64);
321 - if(hKernel32) FreeLibrary(hKernel32);
322 - LRESULT error;
323 - if(is64) error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ|KEY_WOW64_64KEY,&hKey);
324 - else error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ,&hKey);
325 - if(error == ERROR_SUCCESS)
 316+ if (initial)
326317 {
327 - GetModuleFileName(NULL,filename,MAX_PATH);
328 - TCHAR buffer[1024];
329 - ZeroMemory(buffer,1024*sizeof(TCHAR));
330 - DWORD sizeout = 1024*sizeof(TCHAR);
331 - if(RegQueryValueEx(hKey,filename,NULL,NULL,(LPBYTE)buffer,&sizeout) == ERROR_SUCCESS)
 318+ HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
 319+ BOOL (WINAPI *iswow64)(HANDLE,PBOOL) = NULL;
 320+ if(hKernel32) iswow64 = (BOOL(WINAPI*)(HANDLE,PBOOL))GetProcAddress(hKernel32,"IsWow64Process");
 321+ BOOL is64 = FALSE;
 322+ if(iswow64) iswow64(GetCurrentProcess(),&is64);
 323+ if(hKernel32) FreeLibrary(hKernel32);
 324+ LRESULT error;
 325+ if(is64) error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ|KEY_WOW64_64KEY,&hKey);
 326+ else error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ,&hKey);
 327+ if(error == ERROR_SUCCESS)
332328 {
333 - if(_tcsstr(buffer,_T("DWM8And16BitMitigation")))
 329+ GetModuleFileName(NULL,filename,MAX_PATH);
 330+ TCHAR buffer[1024];
 331+ ZeroMemory(buffer,1024*sizeof(TCHAR));
 332+ DWORD sizeout = 1024*sizeof(TCHAR);
 333+ if(RegQueryValueEx(hKey,filename,NULL,NULL,(LPBYTE)buffer,&sizeout) == ERROR_SUCCESS)
334334 {
335 - MessageBox(NULL,_T("DXGL has detected an incompatible AppCompat flag for the program you are currently running. To continue, the registry value must be deleted.\nIf you see a UAC prompt, you must click Yes."),
336 - _T("AppCompat error"),MB_OK|MB_ICONHAND);
337 - error = RegDeleteValue(hKey,filename);
338 - if(error == ERROR_ACCESS_DENIED)
 335+ if(_tcsstr(buffer,_T("DWM8And16BitMitigation")))
339336 {
340 - tstring command;
341 - if(is64) command.assign(_T("DELETE \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
342 - else command.assign(_T("DELETE \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
343 - command.append(filename);
344 - command.append(_T("\""));
345 - SHELLEXECUTEINFO info;
346 - ZeroMemory(&info,sizeof(SHELLEXECUTEINFO));
347 - info.cbSize = sizeof(SHELLEXECUTEINFO);
348 - info.lpVerb = _T("runas");
349 - info.lpFile = _T("reg.exe");
350 - info.lpParameters = command.c_str();
351 - info.nShow = SW_SHOWNORMAL;
352 - info.fMask = SEE_MASK_NOCLOSEPROCESS;
353 - ShellExecuteEx(&info);
354 - WaitForSingleObject(info.hProcess,INFINITE);
355 - GetExitCodeProcess(info.hProcess,(LPDWORD)&error);
 337+ MessageBox(NULL,_T("DXGL has detected an incompatible AppCompat flag for the program you are currently running. To continue, the registry value must be deleted.\nIf you see a UAC prompt, you must click Yes."),
 338+ _T("AppCompat error"),MB_OK|MB_ICONHAND);
 339+ error = RegDeleteValue(hKey,filename);
 340+ if(error == ERROR_ACCESS_DENIED)
 341+ {
 342+ tstring command;
 343+ if(is64) command.assign(_T("DELETE \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
 344+ else command.assign(_T("DELETE \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
 345+ command.append(filename);
 346+ command.append(_T("\""));
 347+ SHELLEXECUTEINFO info;
 348+ ZeroMemory(&info,sizeof(SHELLEXECUTEINFO));
 349+ info.cbSize = sizeof(SHELLEXECUTEINFO);
 350+ info.lpVerb = _T("runas");
 351+ info.lpFile = _T("reg.exe");
 352+ info.lpParameters = command.c_str();
 353+ info.nShow = SW_SHOWNORMAL;
 354+ info.fMask = SEE_MASK_NOCLOSEPROCESS;
 355+ ShellExecuteEx(&info);
 356+ WaitForSingleObject(info.hProcess,INFINITE);
 357+ GetExitCodeProcess(info.hProcess,(LPDWORD)&error);
 358+ }
 359+ if(!error)
 360+ {
 361+ MessageBox(NULL,_T("Registry value successfully deleted. Please restart the program."),_T("Success"),MB_OK|MB_ICONINFORMATION);
 362+ exit(0);
 363+ }
 364+ else MessageBox(NULL,_T("Registry value could not be deleted. Your program may crash as a result."),_T("Error"),MB_OK|MB_ICONWARNING);
356365 }
357 - if(!error)
358 - {
359 - MessageBox(NULL,_T("Registry value successfully deleted. Please restart the program."),_T("Success"),MB_OK|MB_ICONINFORMATION);
360 - exit(0);
361 - }
362 - else MessageBox(NULL,_T("Registry value could not be deleted. Your program may crash as a result."),_T("Error"),MB_OK|MB_ICONWARNING);
363366 }
 367+ RegCloseKey(hKey);
364368 }
365 - RegCloseKey(hKey);
366 - }
367 - if(is64) error = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ|KEY_WOW64_64KEY,&hKey);
368 - else error = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ,&hKey);
369 - if(error == ERROR_SUCCESS)
370 - {
371 - GetModuleFileName(NULL,filename,MAX_PATH);
372 - TCHAR buffer[1024];
373 - ZeroMemory(buffer,1024*sizeof(TCHAR));
374 - DWORD sizeout = 1024*sizeof(TCHAR);
375 - if(RegQueryValueEx(hKey,filename,NULL,NULL,(LPBYTE)buffer,&sizeout) == ERROR_SUCCESS)
 369+ if(is64) error = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ|KEY_WOW64_64KEY,&hKey);
 370+ else error = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers"),0,KEY_READ,&hKey);
 371+ if(error == ERROR_SUCCESS)
376372 {
377 - if(_tcsstr(buffer,_T("DWM8And16BitMitigation")))
 373+ GetModuleFileName(NULL,filename,MAX_PATH);
 374+ TCHAR buffer[1024];
 375+ ZeroMemory(buffer,1024*sizeof(TCHAR));
 376+ DWORD sizeout = 1024*sizeof(TCHAR);
 377+ if(RegQueryValueEx(hKey,filename,NULL,NULL,(LPBYTE)buffer,&sizeout) == ERROR_SUCCESS)
378378 {
379 - MessageBox(NULL,_T("DXGL has detected an incompatible AppCompat flag for the program you are currently running. To continue, the registry value must be deleted.\nIf you see a UAC prompt, you must click Yes."),
380 - _T("AppCompat error"),MB_OK|MB_ICONHAND);
381 - error = RegDeleteValue(hKey,filename);
382 - if(error == ERROR_ACCESS_DENIED)
 379+ if(_tcsstr(buffer,_T("DWM8And16BitMitigation")))
383380 {
384 - tstring command;
385 - if(is64) command.assign(_T("DELETE \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
386 - else command.assign(_T("DELETE \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
387 - command.append(filename);
388 - command.append(_T("\""));
389 - SHELLEXECUTEINFO info;
390 - ZeroMemory(&info,sizeof(SHELLEXECUTEINFO));
391 - info.cbSize = sizeof(SHELLEXECUTEINFO);
392 - info.lpVerb = _T("runas");
393 - info.lpFile = _T("reg.exe");
394 - info.lpParameters = command.c_str();
395 - info.nShow = SW_SHOWNORMAL;
396 - info.fMask = SEE_MASK_NOCLOSEPROCESS;
397 - ShellExecuteEx(&info);
398 - WaitForSingleObject(info.hProcess,INFINITE);
399 - GetExitCodeProcess(info.hProcess,(LPDWORD)&error);
 381+ MessageBox(NULL,_T("DXGL has detected an incompatible AppCompat flag for the program you are currently running. To continue, the registry value must be deleted.\nIf you see a UAC prompt, you must click Yes."),
 382+ _T("AppCompat error"),MB_OK|MB_ICONHAND);
 383+ error = RegDeleteValue(hKey,filename);
 384+ if(error == ERROR_ACCESS_DENIED)
 385+ {
 386+ tstring command;
 387+ if(is64) command.assign(_T("DELETE \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /reg:64 /f /v \""));
 388+ else command.assign(_T("DELETE \"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /f /v \""));
 389+ command.append(filename);
 390+ command.append(_T("\""));
 391+ SHELLEXECUTEINFO info;
 392+ ZeroMemory(&info,sizeof(SHELLEXECUTEINFO));
 393+ info.cbSize = sizeof(SHELLEXECUTEINFO);
 394+ info.lpVerb = _T("runas");
 395+ info.lpFile = _T("reg.exe");
 396+ info.lpParameters = command.c_str();
 397+ info.nShow = SW_SHOWNORMAL;
 398+ info.fMask = SEE_MASK_NOCLOSEPROCESS;
 399+ ShellExecuteEx(&info);
 400+ WaitForSingleObject(info.hProcess,INFINITE);
 401+ GetExitCodeProcess(info.hProcess,(LPDWORD)&error);
 402+ }
 403+ if(!error)
 404+ {
 405+ MessageBox(NULL,_T("Registry value successfully deleted. Please restart the program."),_T("Success"),MB_OK|MB_ICONINFORMATION);
 406+ exit(0);
 407+ }
 408+ else MessageBox(NULL,_T("Registry value could not be deleted. Your program may crash as a result."),_T("Error"),MB_OK|MB_ICONWARNING);
400409 }
401 - if(!error)
402 - {
403 - MessageBox(NULL,_T("Registry value successfully deleted. Please restart the program."),_T("Success"),MB_OK|MB_ICONINFORMATION);
404 - exit(0);
405 - }
406 - else MessageBox(NULL,_T("Registry value could not be deleted. Your program may crash as a result."),_T("Error"),MB_OK|MB_ICONWARNING);
407410 }
 411+ RegCloseKey(hKey);
408412 }
409 - RegCloseKey(hKey);
410413 }
411414 }
412415 void GetGlobalConfig(DXGLCFG *cfg)
Index: cfgmgr/cfgmgr.h
@@ -42,7 +42,7 @@
4343
4444 void ReadSettings(HKEY hKey, DXGLCFG *cfg, DXGLCFG *mask, bool global, bool dll, LPTSTR dir);
4545 void WriteSettings(HKEY hKey, const DXGLCFG *cfg, const DXGLCFG *mask, bool global);
46 -void GetCurrentConfig(DXGLCFG *cfg);
 46+void GetCurrentConfig(DXGLCFG *cfg, bool initial);
4747 void GetGlobalConfig(DXGLCFG *cfg);
4848 void SetGlobalConfig(const DXGLCFG *cfg);
4949 void GetConfig(DXGLCFG *cfg, DXGLCFG *mask, LPCTSTR name);
Index: ddraw/ddraw.cpp
@@ -194,7 +194,7 @@
195195 TRACE_EXIT(23,DDERR_DIRECTDRAWALREADYCREATED);
196196 return DDERR_DIRECTDRAWALREADYCREATED;
197197 }
198 - GetCurrentConfig(&dxglcfg);
 198+ GetCurrentConfig(&dxglcfg,false);
199199 glDirectDraw7 *myddraw7;
200200 glDirectDraw1 *myddraw;
201201 HRESULT error;
@@ -267,7 +267,7 @@
268268 TRACE_EXIT(23,DDERR_DIRECTDRAWALREADYCREATED);
269269 return DDERR_DIRECTDRAWALREADYCREATED;
270270 }
271 - GetCurrentConfig(&dxglcfg);
 271+ GetCurrentConfig(&dxglcfg,false);
272272 glDirectDraw7 *myddraw;
273273 HRESULT error;
274274 if(iid != IID_IDirectDraw7)
@@ -527,7 +527,7 @@
528528 TRACE_EXIT(23,CLASS_E_CLASSNOTAVAILABLE);
529529 return CLASS_E_CLASSNOTAVAILABLE;
530530 }
531 - GetCurrentConfig(&dxglcfg);
 531+ GetCurrentConfig(&dxglcfg,false);
532532 glClassFactory *factory = new glClassFactory;
533533 if(factory == NULL)
534534 {
Index: ddraw/dllmain.cpp
@@ -26,6 +26,8 @@
2727 switch (ul_reason_for_call)
2828 {
2929 case DLL_PROCESS_ATTACH:
 30+ GetCurrentConfig(&dxglcfg, true);
 31+ break;
3032 case DLL_THREAD_ATTACH:
3133 case DLL_THREAD_DETACH:
3234 case DLL_PROCESS_DETACH: