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.

181 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. InstantCDDVD.cpp
  5. Abstract:
  6. Clean up the filter drivers on the uninstaller on process termination.
  7. Notes:
  8. This is an app specific shim.
  9. History:
  10. 08/20/2002 mnikkel Created
  11. --*/
  12. #include "precomp.h"
  13. IMPLEMENT_SHIM_BEGIN(InstantCDDVD)
  14. #include "ShimHookMacro.h"
  15. APIHOOK_ENUM_BEGIN
  16. APIHOOK_ENUM_END
  17. BOOL
  18. StripStringFromValue(HKEY hKey, const WCHAR *lpValue, const WCHAR *lpStrip)
  19. {
  20. DWORD dwType, dwSize;
  21. BOOL bRet = FALSE;
  22. WCHAR *lpString = NULL;
  23. //
  24. // Build the %systemdir%\drivers\filename.sys to see if it's available
  25. //
  26. CString csSystem;
  27. CSTRING_TRY
  28. {
  29. csSystem.GetSystemDirectoryW();
  30. csSystem += L"\\Drivers\\";
  31. csSystem += lpStrip;
  32. csSystem += L".sys";
  33. }
  34. CSTRING_CATCH
  35. {
  36. goto Exit;
  37. }
  38. //
  39. // Check to see if the file exists - if it does, we don't touch the registry
  40. //
  41. if (GetFileAttributesW(csSystem) != 0xFFFFFFFF) {
  42. DPFN(eDbgLevelError, "%S found so leave registry value alone", lpStrip);
  43. goto Exit;
  44. }
  45. //
  46. // Checking the registry for the bad state now
  47. //
  48. // Get the size
  49. if (ERROR_SUCCESS != RegQueryValueExW(hKey, lpValue, NULL, &dwType, NULL, &dwSize)) {
  50. DPFN(eDbgLevelError, "%S value not found", lpValue);
  51. goto Exit;
  52. }
  53. // Make sure it's a MULTI_STRING
  54. if (dwType != REG_MULTI_SZ) {
  55. DPFN(eDbgLevelError, "%S not correct type, expecting a multi-string", lpStrip);
  56. goto Exit;
  57. }
  58. // Allocate memory for it and clear it
  59. lpString = (WCHAR *) malloc(dwSize);
  60. if (!lpString) {
  61. DPFN(eDbgLevelError, "Out of memory");
  62. goto Exit;
  63. }
  64. ZeroMemory(lpString, dwSize);
  65. // Get the actual data
  66. if (ERROR_SUCCESS != RegQueryValueExW(hKey, lpValue, NULL, &dwType, (LPBYTE)lpString, &dwSize)) {
  67. DPFN(eDbgLevelError, "%S QueryValue failed unexpectedly", lpStrip);
  68. goto Exit;
  69. }
  70. // Allocate an output buffer
  71. WCHAR * lpNewString = (WCHAR *) malloc(dwSize);
  72. if (!lpNewString) {
  73. DPFN(eDbgLevelError, "Out of memory");
  74. goto Exit;
  75. }
  76. ZeroMemory(lpNewString, dwSize);
  77. // Run the input buffer looking for lpStrip
  78. WCHAR *lpCurr = lpString;
  79. WCHAR *lpCurrOut = lpNewString;
  80. BOOL bStripped = FALSE;
  81. while (*lpCurr) {
  82. if (_wcsicmp(lpCurr, lpStrip) != 0) {
  83. // Keep this entry
  84. if (StringCchCopyW(lpCurrOut, dwSize, lpCurr) != S_OK)
  85. {
  86. goto Exit;
  87. }
  88. lpCurrOut += wcslen(lpCurrOut) + 1;
  89. dwSize -= wcslen(lpCurrOut) + 1;
  90. } else {
  91. // Remove this entry
  92. bStripped = TRUE;
  93. }
  94. lpCurr += wcslen(lpCurr) + 1;
  95. }
  96. if (bStripped) {
  97. //
  98. // Fix up the registry with the new value. If there's nothing left, then kill the
  99. // value.
  100. //
  101. LOGN(eDbgLevelError, "Removing filter driver - Value: %S, Name: %S", lpValue, lpStrip);
  102. dwSize = (lpCurrOut - lpNewString) * sizeof(WCHAR);
  103. if (dwSize == 0) {
  104. RegDeleteValueW(hKey, lpValue);
  105. } else {
  106. RegSetValueExW(hKey, lpValue, NULL, dwType, (LPBYTE) lpNewString, dwSize + sizeof(WCHAR));
  107. }
  108. }
  109. bRet = TRUE;
  110. Exit:
  111. if (lpString) {
  112. free(lpString);
  113. }
  114. if (lpNewString) {
  115. free(lpNewString);
  116. }
  117. return bRet;
  118. }
  119. /*++
  120. Register hooked functions
  121. --*/
  122. BOOL
  123. NOTIFY_FUNCTION(DWORD fdwReason)
  124. {
  125. if (fdwReason == DLL_PROCESS_DETACH) {
  126. HKEY hKey;
  127. if (ERROR_SUCCESS == RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E965-E325-11CE-BFC1-08002BE10318}", &hKey)) {
  128. StripStringFromValue(hKey, L"UpperFilters", L"ASAPI");
  129. StripStringFromValue(hKey, L"UpperFilters", L"CDRDRV");
  130. RegCloseKey(hKey);
  131. }
  132. }
  133. return TRUE;
  134. }
  135. HOOK_BEGIN
  136. CALL_NOTIFY_FUNCTION
  137. HOOK_END
  138. IMPLEMENT_SHIM_END