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.

183 lines
3.9 KiB

  1. //
  2. // Dll.cpp
  3. //
  4. // Dll API functions for FldrClnr.dll
  5. //
  6. //
  7. #include <windows.h>
  8. #include <shlwapi.h>
  9. #include <shfusion.h>
  10. #include "CleanupWiz.h"
  11. #include "priv.h"
  12. // declare debug needs to be defined in exactly one source file in the project
  13. #define DECLARE_DEBUG
  14. #include <debug.h>
  15. STDAPI_(int) CleanupDesktop(DWORD, HWND); // defined in fldrclnr.cpp
  16. HINSTANCE g_hInst;
  17. CRITICAL_SECTION g_csDll = {0}; // needed by ENTERCRITICAL in uassist.cpp (UEM code)
  18. //
  19. // Dll functions
  20. //
  21. extern "C" BOOL APIENTRY DllMain(
  22. HINSTANCE hDll,
  23. DWORD dwReason,
  24. LPVOID lpReserved)
  25. {
  26. switch (dwReason)
  27. {
  28. case ( DLL_PROCESS_ATTACH ) :
  29. {
  30. g_hInst = hDll;
  31. SHFusionInitializeFromModule(hDll);
  32. break;
  33. }
  34. case ( DLL_PROCESS_DETACH ) :
  35. {
  36. SHFusionUninitialize();
  37. break;
  38. }
  39. case ( DLL_THREAD_ATTACH ) :
  40. case ( DLL_THREAD_DETACH ) :
  41. {
  42. break;
  43. }
  44. }
  45. return (TRUE);
  46. }
  47. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  48. {
  49. return S_OK;
  50. }
  51. STDAPI DllRegisterServer(void)
  52. {
  53. return S_OK;
  54. }
  55. STDAPI DllUnregisterServer(void)
  56. {
  57. return S_OK;
  58. }
  59. //////////////////////////////////////////////////////
  60. // ensure only one instance is running
  61. HANDLE AnotherCopyRunning()
  62. {
  63. HANDLE hMutex = CreateMutex(NULL, FALSE, TEXT("DesktopCleanupMutex"));
  64. if (!hMutex)
  65. {
  66. // failed to create the mutex
  67. return 0;
  68. }
  69. else if (GetLastError() == ERROR_ALREADY_EXISTS)
  70. {
  71. // Mutex created but by someone else
  72. CloseHandle(hMutex);
  73. return 0;
  74. }
  75. // we are the first
  76. return hMutex;
  77. }
  78. //////////////////////////////////////////////////////
  79. //
  80. // This function checks whether we need to run the cleaner
  81. // We will not run if user is guest, user has forced us not to, or if the requisite
  82. // number of days have not yet elapsed
  83. //
  84. BOOL ShouldRun(DWORD dwCleanMode)
  85. {
  86. BOOL fShouldRun;
  87. if (IsUserAGuest())
  88. {
  89. fShouldRun = FALSE;
  90. }
  91. else if (CLEANUP_MODE_SILENT != dwCleanMode)
  92. {
  93. fShouldRun = !SHRestricted(REST_NODESKTOPCLEANUP);
  94. }
  95. else
  96. {
  97. DWORD dwData = 0;
  98. DWORD cb = sizeof(dwData);
  99. // dwCleanMode is CLEANUP_MODE_SILENT
  100. if ((ERROR_SUCCESS == SHGetValue(HKEY_LOCAL_MACHINE, REGSTR_OEM_PATH, REGSTR_OEM_OPTIN, NULL, &dwData, &cb)) &&
  101. (dwData != 0))
  102. {
  103. fShouldRun = TRUE;
  104. }
  105. else
  106. {
  107. CreateDesktopIcons(); // create default icons on the desktop (IE, MSN Explorer, Media Player)
  108. fShouldRun = FALSE;
  109. }
  110. }
  111. return fShouldRun;
  112. }
  113. ///////////////////////
  114. //
  115. // Our exports
  116. //
  117. ///////////////////////
  118. //
  119. // The rundll32.exe entry point for starting the dekstop cleaner.
  120. // called via "rundll32.exe fldrclnr.dll,Wizard_RunDLL"
  121. //
  122. // can take an optional parameter in the commandline :
  123. //
  124. // "all" - show all the items on the desktop in the UI
  125. // "silent" - silently clean up all the items on the desktop
  126. //
  127. STDAPI_(void) Wizard_RunDLL(HWND hwndStub, HINSTANCE hAppInstance, LPSTR pszCmdLine, int nCmdShow)
  128. {
  129. DWORD dwCleanMode = CLEANUP_MODE_NORMAL;
  130. if (0 == StrCmpNIA(pszCmdLine, "all", 3))
  131. {
  132. dwCleanMode = CLEANUP_MODE_ALL;
  133. }
  134. else if (0 == StrCmpNIA(pszCmdLine, "silent", 6))
  135. {
  136. dwCleanMode = CLEANUP_MODE_SILENT;
  137. }
  138. HANDLE hMutex = AnotherCopyRunning();
  139. if (hMutex)
  140. {
  141. if (ShouldRun(dwCleanMode))
  142. {
  143. InitializeCriticalSection(&g_csDll); // needed for UEM stuff
  144. if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) // also for UEM stuff.
  145. {
  146. CCleanupWiz cfc;
  147. cfc.Run(dwCleanMode, hwndStub);
  148. CoUninitialize();
  149. }
  150. DeleteCriticalSection(&g_csDll);
  151. }
  152. CloseHandle(hMutex);
  153. }
  154. }