Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

  1. #include "precomp.h"
  2. IMPLEMENT_SHIM_BEGIN(Win2kPropagateLayer)
  3. #include "ShimHookMacro.h"
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #define APPCOMPAT_KEYW L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\AppCompatibility"
  8. BOOL
  9. CleanupRegistryForCurrentExe(
  10. void
  11. )
  12. {
  13. NTSTATUS status;
  14. OBJECT_ATTRIBUTES objA;
  15. HANDLE hkey;
  16. WCHAR wszExeName[MAX_PATH];
  17. WCHAR wszKey[MAX_PATH];
  18. UNICODE_STRING strKey;
  19. UNICODE_STRING strValue;
  20. DWORD dwChars = GetModuleFileNameW(NULL, wszExeName, MAX_PATH);
  21. // If there was an error, or the path was truncated.
  22. if (dwChars == 0 || dwChars == MAX_PATH) {
  23. return FALSE;
  24. }
  25. WCHAR* pwsz = wszExeName + dwChars;
  26. while (pwsz >= wszExeName) {
  27. if (*pwsz == '\\') {
  28. break;
  29. }
  30. pwsz--;
  31. }
  32. pwsz++;
  33. LOGN(
  34. eDbgLevelInfo,
  35. "[CleanupRegistryForCurrentExe] Cleanup for \"%S\"",
  36. pwsz);
  37. if( FAILED(StringCchPrintf(wszKey, MAX_PATH, L"%ls\\%ls", APPCOMPAT_KEYW, pwsz)) )
  38. {
  39. return FALSE;
  40. }
  41. RtlInitUnicodeString(&strKey, wszKey);
  42. InitializeObjectAttributes(&objA,
  43. &strKey,
  44. OBJ_CASE_INSENSITIVE,
  45. NULL,
  46. NULL);
  47. status = NtOpenKey(&hkey,
  48. MAXIMUM_ALLOWED,
  49. &objA);
  50. if (!NT_SUCCESS(status)) {
  51. LOGN(
  52. eDbgLevelError,
  53. "[CleanupRegistryForCurrentExe] Failed to open key \"%S\"",
  54. wszKey);
  55. return TRUE;
  56. }
  57. RtlInitUnicodeString(&strValue, L"DllPatch-y");
  58. NtDeleteValueKey(hkey, &strValue);
  59. RtlInitUnicodeString(&strValue, L"y");
  60. NtDeleteValueKey(hkey, &strValue);
  61. //
  62. // Now check to see if there are any more values under this key.
  63. // Delete it if there are no more values.
  64. //
  65. KEY_FULL_INFORMATION keyInfo;
  66. DWORD dwReturnLength = 0;
  67. status = NtQueryKey(hkey,
  68. KeyFullInformation,
  69. &keyInfo,
  70. sizeof(keyInfo),
  71. &dwReturnLength);
  72. if (NT_SUCCESS(status) && keyInfo.Values == 0) {
  73. NtDeleteKey(hkey);
  74. }
  75. NtClose(hkey);
  76. return TRUE;
  77. }
  78. IMPLEMENT_SHIM_END