Source code of Windows XP (NT5)
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.

189 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. MoveWinInitRenameToReg.cpp
  5. Abstract:
  6. This shim hooks ExitWindowsEx as well as waits for DLL_PROCESS_DETACH
  7. and then moves the contents of the [Rename] section of wininit.ini
  8. into the registry via MoveFileEx().
  9. History:
  10. 07/24/2000 t-adams Created
  11. --*/
  12. #include "precomp.h"
  13. #define KEY_SIZE_STEP MAX_PATH
  14. IMPLEMENT_SHIM_BEGIN(MoveWinInitRenameToReg)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(ExitWindowsEx)
  18. APIHOOK_ENUM_END
  19. /*++
  20. Abstract:
  21. Moves the entries in the Rename section of WinInit.ini into
  22. the registry via MoveFileEx().
  23. History:
  24. 07/24/2000 t-adams Created
  25. --*/
  26. void MoveWinInitRenameToReg(void)
  27. {
  28. LPWSTR szKeys = NULL;
  29. LPWSTR szFrom = NULL;
  30. DWORD dwKeysSize = 0;
  31. LPWSTR pszTo = NULL;
  32. CString csWinInit;
  33. CString csWinInitBak;
  34. // Construct the paths to wininit.ini and wininit.bak
  35. csWinInit.GetWindowsDirectoryW();
  36. csWinInitBak.GetWindowsDirectoryW();
  37. csWinInit.AppendPath(L"\\wininit.ini");
  38. csWinInitBak.AppendPath(L"\\wininit.bak");
  39. // Make sure wininit.ini exists.
  40. if( GetFileAttributesW(csWinInit) != -1 )
  41. {
  42. // Copy wininit.ini to wininit.bak because we will be destroying
  43. // wininit.ini as we read through its keys and can't simply rename
  44. // it to wininit.bak later.
  45. CopyFileW(csWinInit, csWinInitBak, FALSE);
  46. // Read the "key" names.
  47. // Since we can't know how big the list of keys is going to be,
  48. // continue to try to get the list until GetPrivateProfile string
  49. // returns something other than dwKeysSize-2 (indicating too small
  50. // of a buffer).
  51. // Read the "key" names.
  52. // Since we can't know how big the list of keys is going to be,
  53. // continue to try to get the list until GetPrivateProfile string
  54. // returns something other than dwKeysSize-2 (indicating too small
  55. // of a buffer).
  56. do
  57. {
  58. if( NULL != szKeys )
  59. {
  60. free(szKeys);
  61. }
  62. dwKeysSize += KEY_SIZE_STEP;
  63. szKeys = (LPWSTR) malloc(dwKeysSize * sizeof(WCHAR));
  64. if( NULL == szKeys )
  65. {
  66. goto Exit;
  67. }
  68. }
  69. while(GetPrivateProfileStringW(L"Rename", NULL, NULL, szKeys, dwKeysSize, csWinInit)
  70. == dwKeysSize - 2);
  71. szFrom = (LPWSTR) malloc(MAX_PATH * sizeof(WCHAR));
  72. if( NULL != szFrom )
  73. {
  74. // Traverse through the keys.
  75. // Delete each key after we read it so that if there are multiple "NUL" keys,
  76. // our calls to GetPrivateProfileStringA won't continue to return only the
  77. // first NUL key's associated value.
  78. pszTo = szKeys;
  79. while(*pszTo != NULL)
  80. {
  81. GetPrivateProfileStringW(L"Rename", pszTo, NULL, szFrom, MAX_PATH, csWinInit);
  82. WritePrivateProfileStringW(L"Rename", pszTo, NULL, csWinInit);
  83. // If pszTo is "NUL", then the intention is to delete the szFrom file, so pass
  84. // NULL to MoveFileExA().
  85. if( wcscmp(pszTo, L"NUL") == 0 )
  86. {
  87. MoveFileExW(szFrom, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
  88. }
  89. else
  90. {
  91. MoveFileExW(szFrom, pszTo, MOVEFILE_DELAY_UNTIL_REBOOT);
  92. }
  93. // Move to the next file (key)
  94. pszTo += wcslen(pszTo) + 1;
  95. }
  96. // delete WinInit.ini
  97. DeleteFileW(csWinInit);
  98. }
  99. }
  100. Exit:
  101. if( NULL != szKeys )
  102. {
  103. free(szKeys);
  104. }
  105. if( NULL != szFrom )
  106. {
  107. free(szFrom);
  108. }
  109. }
  110. /*++
  111. Abstract:
  112. Hook ExitWindowsEx in case the program resets the machine, keeping
  113. us from receiving the DLL_PROCESS_DETACH message. (Shim originally
  114. written for an uninstall program that caused a reset.)
  115. History:
  116. 07/24/2000 t-adams Created
  117. --*/
  118. BOOL
  119. APIHOOK(ExitWindowsEx)(
  120. UINT uFlags,
  121. DWORD dwReserved)
  122. {
  123. MoveWinInitRenameToReg();
  124. return ORIGINAL_API(ExitWindowsEx)(uFlags, dwReserved);
  125. }
  126. /*++
  127. Register hooked functions
  128. --*/
  129. BOOL
  130. NOTIFY_FUNCTION(
  131. DWORD fdwReason)
  132. {
  133. if (fdwReason == DLL_PROCESS_DETACH)
  134. {
  135. MoveWinInitRenameToReg();
  136. }
  137. return TRUE;
  138. }
  139. HOOK_BEGIN
  140. APIHOOK_ENTRY(USER32.DLL, ExitWindowsEx )
  141. CALL_NOTIFY_FUNCTION
  142. HOOK_END
  143. IMPLEMENT_SHIM_END