39a83cb91b0069483dba9968e5f6b3b0c510dd06
[reactos.git] / reactos / base / shell / explorer-new / shellservice.cpp
1 /*
2 * ReactOS Explorer
3 *
4 * Copyright 2014 - David Quintana
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "precomp.h"
22
23 extern HRESULT InitShellServices(HDPA * phdpa);
24 extern HRESULT ShutdownShellServices(HDPA hdpa);
25
26 static int CALLBACK InitializeAllCallback(void* pItem, void* pData)
27 {
28 IOleCommandTarget * pOct = reinterpret_cast<IOleCommandTarget *>(pItem);
29 HRESULT * phr = reinterpret_cast<HRESULT *>(pData);
30 TRACE("Initializing SSO %p\n", pOct);
31 *phr = pOct->Exec(&CGID_ShellServiceObject, OLECMDID_NEW, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
32 return SUCCEEDED(*phr);
33 }
34
35 static int CALLBACK ShutdownAllCallback(void* pItem, void* pData)
36 {
37 IOleCommandTarget * pOct = reinterpret_cast<IOleCommandTarget *>(pItem);
38 TRACE("Shutting down SSO %p\n", pOct);
39 pOct->Exec(&CGID_ShellServiceObject, OLECMDID_SAVE, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
40 return TRUE;
41 }
42
43 static int CALLBACK DeleteAllEnumCallback(void* pItem, void* pData)
44 {
45 IOleCommandTarget * pOct = reinterpret_cast<IOleCommandTarget *>(pItem);
46 TRACE("Releasing SSO %p\n", pOct);
47 pOct->Release();
48 return TRUE;
49 }
50
51 HRESULT InitShellServices(HDPA * phdpa)
52 {
53 IOleCommandTarget * pOct;
54 HKEY hkey;
55 CLSID clsid;
56 WCHAR name[MAX_PATH];
57 WCHAR value[MAX_PATH];
58 DWORD type;
59 LONG ret;
60 HDPA hdpa;
61 HRESULT hr = S_OK;
62 int count = 0;
63
64 *phdpa = NULL;
65
66 TRACE("Enumerating Shell Service Ojbect GUIDs...\n");
67
68 if (RegOpenKey(HKEY_LOCAL_MACHINE,
69 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ShellServiceObjectDelayLoad",
70 &hkey))
71 {
72 ERR("RegOpenKey failed.\n");
73 return HRESULT_FROM_WIN32(GetLastError());
74 }
75
76 hdpa = DPA_Create(5);
77
78 /* Enumerate */
79 do
80 {
81 DWORD name_len = MAX_PATH;
82 DWORD value_len = sizeof(value); /* byte count! */
83
84 ret = RegEnumValueW(hkey, count, name, &name_len, 0, &type, (LPBYTE) &value, &value_len);
85 if (ret)
86 break;
87
88 if (type != REG_SZ)
89 {
90 WARN("Value type was not REG_SZ.\n");
91 continue;
92 }
93
94 hr = CLSIDFromString(value, &clsid);
95 if (FAILED_UNEXPECTEDLY(hr))
96 {
97 ERR("CLSIDFromString failed %08x.\n", hr);
98 goto cleanup;
99 }
100
101 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IOleCommandTarget, &pOct));
102 if (FAILED_UNEXPECTEDLY(hr))
103 {
104 ERR("CoCreateInstance failed %08x.\n", hr);
105 goto cleanup;
106 }
107
108 DPA_AppendPtr(hdpa, pOct);
109
110 count++;
111 }
112 while (1);
113
114 if (ret != ERROR_NO_MORE_ITEMS)
115 {
116 ERR("RegEnumValueW failed %08x.\n", ret);
117 hr = HRESULT_FROM_WIN32(GetLastError());
118 goto cleanup;
119 }
120
121 RegCloseKey(hkey);
122
123 /* Initialize */
124 DPA_EnumCallback(hdpa, InitializeAllCallback, &hr);
125 if (FAILED_UNEXPECTEDLY(hr))
126 goto cleanup;
127
128 *phdpa = hdpa;
129 return count > 0 ? S_OK : S_FALSE;
130
131 cleanup:
132 *phdpa = NULL;
133 ShutdownShellServices(hdpa);
134 return hr;
135 }
136
137 HRESULT ShutdownShellServices(HDPA hdpa)
138 {
139 DPA_EnumCallback(hdpa, ShutdownAllCallback, NULL);
140 DPA_EnumCallback(hdpa, DeleteAllEnumCallback, NULL);
141 DPA_Destroy(hdpa);
142 return S_OK;
143 }