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.

414 lines
9.2 KiB

  1. /* ADUTIL.CPP
  2. **
  3. ** Copyright (C) Microsoft Corp. 1998-1999, All Rights Reserved.
  4. **
  5. ** Utility functions for interfacing with the IActiveDesktop
  6. ** services. These services are documented in the Internet SDK.
  7. **
  8. ** Created by: David Schott, January 1998
  9. */
  10. #include <windows.h>
  11. #include <mbctype.h>
  12. #include <objbase.h>
  13. #include <initguid.h>
  14. #include "..\inc\wininet.h"
  15. #include "..\inc\shlguid.h"
  16. #include "..\inc\shlobj.h"
  17. #define MAX_VALUELEN 1024 // Also defined in FROST.H!!
  18. // Extern globals
  19. extern "C" HWND hWndApp; // Handle to Desktop Themes window
  20. // Globals
  21. IActiveDesktop *g_pIAD = NULL; // IActiveDesktop interface
  22. BOOL InitIAD()
  23. {
  24. HRESULT hr = E_FAIL;
  25. // Bring in the library
  26. hr = CoInitialize(NULL);
  27. if (FAILED(hr))
  28. {
  29. //MessageBox(hWndApp, TEXT("CoInit failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  30. return FALSE;
  31. }
  32. g_pIAD = NULL;
  33. hr = CoCreateInstance(CLSID_ActiveDesktop, NULL,
  34. CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void **)&g_pIAD);
  35. if(!FAILED(hr))
  36. {
  37. //MessageBox(hWndApp, TEXT("CoCreateInsance successful"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  38. return TRUE;
  39. }
  40. else
  41. {
  42. //MessageBox(hWndApp, TEXT("CoCreateInstance failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  43. CoUninitialize();
  44. return FALSE;
  45. }
  46. }
  47. void IADCleanUp()
  48. {
  49. if (g_pIAD) g_pIAD->Release();
  50. g_pIAD = NULL;
  51. CoUninitialize();
  52. return;
  53. }
  54. extern "C" BOOL IsActiveDesktopOn()
  55. {
  56. HRESULT hr = E_FAIL;
  57. COMPONENTSOPT ADOptions;
  58. SHELLFLAGSTATE sfs;
  59. HINSTANCE hInst = 0;
  60. typedef VOID (*MYPROC)(LPSHELLFLAGSTATE, DWORD);
  61. MYPROC SHGetSettings;
  62. // Rumor has it this will help guarantee that our AD interface
  63. // is in sync.
  64. // SHGetSettings(&sfs, 0);
  65. hInst = LoadLibrary(TEXT("SHELL32.DLL"));
  66. if (hInst)
  67. {
  68. SHGetSettings = (MYPROC)GetProcAddress(hInst, "SHGetSettings");
  69. if (SHGetSettings)
  70. {
  71. //MessageBox(NULL, TEXT("Calling SHGetSettings"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  72. SHGetSettings(&sfs, 0);
  73. }
  74. FreeLibrary(hInst);
  75. }
  76. if (!InitIAD()) {
  77. //MessageBox(hWndApp, TEXT("INITIAD Failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  78. return FALSE;
  79. }
  80. ZeroMemory(&ADOptions, sizeof(ADOptions));
  81. ADOptions.dwSize = sizeof(ADOptions);
  82. // Paranoid check
  83. if (!g_pIAD) {
  84. IADCleanUp();
  85. return FALSE;
  86. }
  87. hr = g_pIAD->GetDesktopItemOptions(&ADOptions, 0);
  88. if (FAILED(hr)) {
  89. IADCleanUp();
  90. return FALSE;
  91. }
  92. else {
  93. //if (ADOptions.fActiveDesktop)
  94. // MessageBox(hWndApp, TEXT("ActiveDesktop enabled."), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  95. //else MessageBox(hWndApp, TEXT("ActiveDesktop disabled."), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  96. if (ADOptions.fActiveDesktop) {
  97. IADCleanUp();
  98. return TRUE;
  99. }
  100. else {
  101. IADCleanUp();
  102. return FALSE;
  103. }
  104. }
  105. // SHOULD NEVER FALL THROUGH TO THIS BUT JUST IN CASE
  106. IADCleanUp();
  107. return FALSE;
  108. }
  109. extern "C" BOOL SetADWallpaper(LPTSTR lpszWallpaper, BOOL bForceADOn)
  110. {
  111. HRESULT hr = E_FAIL;
  112. COMPONENTSOPT ADOptions;
  113. SHELLFLAGSTATE sfs;
  114. HINSTANCE hInst = 0;
  115. typedef VOID (*MYPROC)(LPSHELLFLAGSTATE, DWORD);
  116. MYPROC SHGetSettings;
  117. #ifndef UNICODE
  118. WCHAR wszWallpaper[MAX_PATH];
  119. #endif
  120. UINT uCodePage;
  121. // Rumor has it this will help guarantee that our AD interface
  122. // is in sync.
  123. // SHGetSettings(&sfs, 0);
  124. hInst = LoadLibrary(TEXT("SHELL32.DLL"));
  125. if (hInst)
  126. {
  127. SHGetSettings = (MYPROC)GetProcAddress(hInst, "SHGetSettings");
  128. if (SHGetSettings)
  129. {
  130. //MessageBox(NULL, TEXT("Calling SHGetSettings"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  131. SHGetSettings(&sfs, 0);
  132. }
  133. FreeLibrary(hInst);
  134. }
  135. if (!InitIAD()) {
  136. return FALSE;
  137. }
  138. // Paranoid check
  139. if (!g_pIAD) {
  140. IADCleanUp();
  141. return FALSE;
  142. }
  143. uCodePage = _getmbcp();
  144. #ifndef UNICODE
  145. //MessageBox(hWndApp, lpszWallpaper, TEXT("SetADWallpaper"), MB_OK | MB_APPLMODAL);
  146. MultiByteToWideChar(uCodePage, 0, lpszWallpaper, -1,
  147. wszWallpaper, MAX_PATH);
  148. #endif
  149. #ifdef UNICODE
  150. //MessageBox(hWndApp, lpszWallpaper, TEXT("SetADWallpaper"), MB_OK | MB_APPLMODAL);
  151. hr = g_pIAD->SetWallpaper(lpszWallpaper, 0);
  152. #else
  153. hr = g_pIAD->SetWallpaper(wszWallpaper, 0);
  154. #endif
  155. if (FAILED(hr)) {
  156. IADCleanUp();
  157. return FALSE;
  158. }
  159. if (bForceADOn) {
  160. ZeroMemory(&ADOptions, sizeof(ADOptions));
  161. ADOptions.dwSize = sizeof(ADOptions);
  162. hr = g_pIAD->GetDesktopItemOptions(&ADOptions, 0);
  163. if (FAILED(hr)) {
  164. IADCleanUp();
  165. return FALSE;
  166. }
  167. //if (ADOptions.fActiveDesktop)
  168. // MessageBox(hWndApp, TEXT("ActiveDesktop enabled."), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  169. //else MessageBox(hWndApp, TEXT("ActiveDesktop disabled."), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  170. if (!ADOptions.fActiveDesktop) {
  171. // Turn AD on
  172. ADOptions.dwSize = sizeof(ADOptions);
  173. ADOptions.fActiveDesktop = TRUE;
  174. g_pIAD->SetDesktopItemOptions(&ADOptions, 0);
  175. }
  176. }
  177. g_pIAD->ApplyChanges(AD_APPLY_ALL | AD_APPLY_FORCE);
  178. IADCleanUp();
  179. return TRUE;
  180. }
  181. extern "C" BOOL GetADWallpaper(LPTSTR lpszWallpaper)
  182. {
  183. HRESULT hr = E_FAIL;
  184. #ifndef UNICODE
  185. WCHAR wszWallpaper[MAX_PATH];
  186. #endif
  187. UINT uCodePage;
  188. if (!InitIAD()) {
  189. // MessageBox(hWndApp, TEXT("INITIAD Failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  190. return FALSE;
  191. }
  192. // Paranoid check
  193. if (!g_pIAD) {
  194. IADCleanUp();
  195. return FALSE;
  196. }
  197. uCodePage = _getmbcp();
  198. #ifdef UNICODE
  199. hr = g_pIAD->GetWallpaper(lpszWallpaper, MAX_PATH, 0);
  200. #else
  201. hr = g_pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0);
  202. #endif
  203. if (FAILED(hr)) {
  204. IADCleanUp();
  205. return FALSE;
  206. }
  207. else {
  208. #ifndef UNICODE
  209. WideCharToMultiByte(uCodePage, 0, wszWallpaper, -1, lpszWallpaper,
  210. MAX_PATH, NULL, NULL);
  211. #endif
  212. //MessageBox(hWndApp, lpszWallpaper, TEXT("GetWallpaper"), MB_OK | MB_APPLMODAL);
  213. IADCleanUp();
  214. return TRUE;
  215. }
  216. }
  217. extern "C" BOOL GetADWPOptions (DWORD *dwStyle)
  218. {
  219. WALLPAPEROPT WPOptions;
  220. HRESULT hr = E_FAIL;
  221. if (!InitIAD()) {
  222. // MessageBox(hWndApp, TEXT("INITIAD Failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  223. return FALSE;
  224. }
  225. // Paranoid check
  226. if (!g_pIAD) {
  227. IADCleanUp();
  228. return FALSE;
  229. }
  230. ZeroMemory(&WPOptions, sizeof(WALLPAPEROPT));
  231. WPOptions.dwSize = sizeof(WALLPAPEROPT);
  232. hr = g_pIAD->GetWallpaperOptions(&WPOptions, 0);
  233. if (FAILED(hr)) {
  234. IADCleanUp();
  235. return FALSE;
  236. }
  237. else {
  238. *dwStyle = WPOptions.dwStyle;
  239. IADCleanUp();
  240. return TRUE;
  241. }
  242. }
  243. extern "C" BOOL SetADWPOptions (DWORD dwStyle)
  244. {
  245. WALLPAPEROPT WPOptions;
  246. HRESULT hr = E_FAIL;
  247. if (!InitIAD()) {
  248. // MessageBox(hWndApp, TEXT("INITIAD Failed"), TEXT("ADUTIL"), MB_OK | MB_APPLMODAL);
  249. return FALSE;
  250. }
  251. // Paranoid check
  252. if (!g_pIAD) {
  253. IADCleanUp();
  254. return FALSE;
  255. }
  256. ZeroMemory(&WPOptions, sizeof(WALLPAPEROPT));
  257. WPOptions.dwSize = sizeof(WALLPAPEROPT);
  258. WPOptions.dwStyle = dwStyle;
  259. hr = g_pIAD->SetWallpaperOptions(&WPOptions, 0);
  260. if (FAILED(hr)) {
  261. IADCleanUp();
  262. return FALSE;
  263. }
  264. else {
  265. g_pIAD->ApplyChanges(AD_APPLY_ALL);
  266. IADCleanUp();
  267. return TRUE;
  268. }
  269. }
  270. extern "C" BOOL GetADWPPattern (LPTSTR lpszPattern)
  271. {
  272. HRESULT hr = E_FAIL;
  273. #ifndef UNICODE
  274. WCHAR wszPat[MAX_VALUELEN]; // Assume this is big enough?
  275. #endif
  276. UINT uCodePage;
  277. if (!InitIAD()) {
  278. return FALSE;
  279. }
  280. // Paranoid check
  281. if (!g_pIAD) {
  282. IADCleanUp();
  283. return FALSE;
  284. }
  285. uCodePage = _getmbcp();
  286. #ifdef UNICODE
  287. hr = g_pIAD->GetPattern(lpszPattern, MAX_VALUELEN, 0);
  288. #else
  289. hr = g_pIAD->GetPattern(wszPat, MAX_VALUELEN, 0);
  290. #endif
  291. if (FAILED(hr)) {
  292. IADCleanUp();
  293. return FALSE;
  294. }
  295. else {
  296. #ifndef UNICODE
  297. WideCharToMultiByte(uCodePage, 0, wszPat, -1, lpszPattern,
  298. MAX_VALUELEN, NULL, NULL);
  299. #endif
  300. IADCleanUp();
  301. return TRUE;
  302. }
  303. }
  304. extern "C" BOOL SetADWPPattern (LPTSTR lpszPattern)
  305. {
  306. HRESULT hr = E_FAIL;
  307. #ifndef UNICODE
  308. WCHAR wszPat[MAX_VALUELEN]; // Assume this is big enough?
  309. #endif
  310. UINT uCodePage;
  311. if (!InitIAD()) {
  312. return FALSE;
  313. }
  314. // Paranoid check
  315. if (!g_pIAD) {
  316. IADCleanUp();
  317. return FALSE;
  318. }
  319. uCodePage = _getmbcp();
  320. #ifndef UNICODE
  321. MultiByteToWideChar(uCodePage, 0, lpszPattern, -1,
  322. wszPat, MAX_VALUELEN);
  323. #endif
  324. #ifdef UNICODE
  325. hr = g_pIAD->SetPattern(lpszPattern, 0);
  326. #else
  327. hr = g_pIAD->SetPattern(wszPat, 0);
  328. #endif
  329. if (FAILED(hr)) {
  330. IADCleanUp();
  331. return FALSE;
  332. }
  333. else {
  334. g_pIAD->ApplyChanges(AD_APPLY_ALL);
  335. IADCleanUp();
  336. return TRUE;
  337. }
  338. }