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.

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