shell32/tests: Fix the last test failure on Win9x.
[wine] / dlls / devenum / devenum_main.c
1 /*
2  *      exported dll functions for devenum.dll
3  *
4  * Copyright (C) 2002 John K. Hohm
5  * Copyright (C) 2002 Robert Shearman
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "devenum_private.h"
23 #include "wine/debug.h"
24 #include "winreg.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
27
28 LONG dll_refs;
29 HINSTANCE DEVENUM_hInstance;
30
31 typedef struct
32 {
33     REFCLSID clsid;
34     LPCWSTR friendly_name;
35     BOOL instance;
36 } register_info;
37
38 static HRESULT register_clsids(int count, const register_info * pRegInfo, LPCWSTR pszThreadingModel);
39 static void DEVENUM_RegisterQuartz(void);
40
41 /***********************************************************************
42  *              Global string constant definitions
43  */
44 const WCHAR clsid_keyname[6] = { 'C', 'L', 'S', 'I', 'D', 0 };
45
46 /***********************************************************************
47  *              DllEntryPoint
48  */
49 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
50 {
51     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
52
53     switch(fdwReason) {
54     case DLL_PROCESS_ATTACH:
55         DEVENUM_hInstance = hinstDLL;
56         DisableThreadLibraryCalls(hinstDLL);
57         break;
58
59     case DLL_PROCESS_DETACH:
60         DEVENUM_hInstance = 0;
61         break;
62     }
63     return TRUE;
64 }
65
66 /***********************************************************************
67  *              DllGetClassObject (DEVENUM.@)
68  */
69 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
70 {
71     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
72
73     *ppv = NULL;
74
75     /* FIXME: we should really have two class factories.
76      * Oh well - works just fine as it is */
77     if (IsEqualGUID(rclsid, &CLSID_SystemDeviceEnum) ||
78         IsEqualGUID(rclsid, &CLSID_CDeviceMoniker))
79         return IClassFactory_QueryInterface((IClassFactory*)&DEVENUM_ClassFactory, iid, ppv);
80
81     FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
82     return CLASS_E_CLASSNOTAVAILABLE;
83 }
84
85 /***********************************************************************
86  *              DllCanUnloadNow (DEVENUM.@)
87  */
88 HRESULT WINAPI DllCanUnloadNow(void)
89 {
90     return dll_refs != 0 ? S_FALSE : S_OK;
91 }
92
93 /***********************************************************************
94  *              DllRegisterServer (DEVENUM.@)
95  */
96 HRESULT WINAPI DllRegisterServer(void)
97 {
98     HRESULT res;
99     HKEY hkeyClsid = NULL;
100     HKEY hkey1 = NULL;
101     HKEY hkey2 = NULL;
102     LPOLESTR pszClsidDevMon = NULL;
103     IFilterMapper2 * pMapper = NULL;
104     LPVOID mapvptr;
105     static const WCHAR threadingModel[] = {'B','o','t','h',0};
106     static const WCHAR sysdevenum[] = {'S','y','s','t','e','m',' ','D','e','v','i','c','e',' ','E','n','u','m',0};
107     static const WCHAR devmon[] = {'D','e','v','i','c','e','M','o','n','i','k','e','r',0};
108     static const WCHAR acmcat[] = {'A','C','M',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
109     static const WCHAR vidcat[] = {'I','C','M',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
110     static const WCHAR filtcat[] = {'A','c','t','i','v','e','M','o','v','i','e',' ','F','i','l','t','e','r',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
111     static const WCHAR vfwcat[] = {'V','F','W',' ','C','a','p','t','u','r','e',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
112     static const WCHAR wavein[] = {'W','a','v','e','I','n',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r', 0};
113     static const WCHAR waveout[] = {'W','a','v','e','O','u','t',' ','a','n','d',' ','D','S','o','u','n','d',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
114     static const WCHAR midiout[] = {'M','i','d','i','O','u','t',' ','C','l','a','s','s',' ','M','a','n','a','g','e','r',0};
115     static const WCHAR amcat[] = {'A','c','t','i','v','e','M','o','v','i','e',' ','F','i','l','t','e','r',' ','C','a','t','e','g','o','r','i','e','s',0};
116     static const WCHAR device[] = {'d','e','v','i','c','e',0};
117     static const WCHAR device_1[] = {'d','e','v','i','c','e','.','1',0};
118     static const register_info ri[] =
119     {
120         {&CLSID_SystemDeviceEnum, sysdevenum, FALSE},
121         {&CLSID_CDeviceMoniker, devmon, FALSE},
122         {&CLSID_AudioCompressorCategory, acmcat, TRUE},
123         {&CLSID_VideoCompressorCategory, vidcat, TRUE},
124         {&CLSID_LegacyAmFilterCategory, filtcat, TRUE},
125         {&CLSID_VideoInputDeviceCategory, vfwcat, FALSE},
126         {&CLSID_AudioInputDeviceCategory, wavein, FALSE},
127         {&CLSID_AudioRendererCategory, waveout, FALSE},
128         {&CLSID_MidiRendererCategory, midiout, FALSE},
129         {&CLSID_ActiveMovieCategories, amcat, TRUE}
130     };
131
132     TRACE("\n");
133
134     res = register_clsids(sizeof(ri) / sizeof(register_info), ri, threadingModel);
135
136     /* Quartz is needed for IFilterMapper2 */
137     DEVENUM_RegisterQuartz();
138
139 /*** ActiveMovieFilter Categories ***/
140
141     CoInitialize(NULL);
142     
143     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
144                            &IID_IFilterMapper2,  &mapvptr);
145     if (SUCCEEDED(res))
146     {
147         static const WCHAR friendlyvidcap[] = {'V','i','d','e','o',' ','C','a','p','t','u','r','e',' ','S','o','u','r','c','e','s',0};
148         static const WCHAR friendlydshow[] = {'D','i','r','e','c','t','S','h','o','w',' ','F','i','l','t','e','r','s',0};
149         static const WCHAR friendlyvidcomp[] = {'V','i','d','e','o',' ','C','o','m','p','r','e','s','s','o','r','s',0};
150         static const WCHAR friendlyaudcap[] = {'A','u','d','i','o',' ','C','a','p','t','u','r','e',' ','S','o','u','r','c','e','s',0};
151         static const WCHAR friendlyaudcomp[] = {'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','o','r','s',0};
152         static const WCHAR friendlyaudrend[] = {'A','u','d','i','o',' ','R','e','n','d','e','r','e','r','s',0};
153         static const WCHAR friendlymidirend[] = {'M','i','d','i',' ','R','e','n','d','e','r','e','r','s',0};
154         static const WCHAR friendlyextrend[] = {'E','x','t','e','r','n','a','l',' ','R','e','n','d','e','r','e','r','s',0};
155         static const WCHAR friendlydevctrl[] = {'D','e','v','i','c','e',' ','C','o','n','t','r','o','l',' ','F','i','l','t','e','r','s',0};
156
157         pMapper = (IFilterMapper2*)mapvptr;
158
159         IFilterMapper2_CreateCategory(pMapper, &CLSID_VideoInputDeviceCategory, MERIT_DO_NOT_USE, friendlyvidcap);
160         IFilterMapper2_CreateCategory(pMapper, &CLSID_LegacyAmFilterCategory, MERIT_NORMAL, friendlydshow);
161         IFilterMapper2_CreateCategory(pMapper, &CLSID_VideoCompressorCategory, MERIT_DO_NOT_USE, friendlyvidcomp);
162         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioInputDeviceCategory, MERIT_DO_NOT_USE, friendlyaudcap);
163         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioCompressorCategory, MERIT_DO_NOT_USE, friendlyaudcomp);
164         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioRendererCategory, MERIT_NORMAL, friendlyaudrend);
165         IFilterMapper2_CreateCategory(pMapper, &CLSID_MidiRendererCategory, MERIT_NORMAL, friendlymidirend);
166         IFilterMapper2_CreateCategory(pMapper, &CLSID_TransmitCategory, MERIT_DO_NOT_USE, friendlyextrend);
167         IFilterMapper2_CreateCategory(pMapper, &CLSID_DeviceControlCategory, MERIT_DO_NOT_USE, friendlydevctrl);
168
169         IFilterMapper2_Release(pMapper);
170     }
171
172 /*** CDeviceMoniker ***/
173     if (SUCCEEDED(res))
174     {
175         res = StringFromCLSID(&CLSID_CDeviceMoniker, &pszClsidDevMon);
176     }
177     if (SUCCEEDED(res))
178     {
179         res = RegOpenKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkeyClsid)
180               == ERROR_SUCCESS ? S_OK : E_FAIL;
181     }
182     if (SUCCEEDED(res))
183     {
184         res = RegOpenKeyW(hkeyClsid, pszClsidDevMon, &hkey1)
185                == ERROR_SUCCESS ? S_OK : E_FAIL;
186     }
187     if (SUCCEEDED(res))
188     {
189         static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
190         res = RegCreateKeyW(hkey1, wszProgID, &hkey2)
191               == ERROR_SUCCESS ? S_OK : E_FAIL;
192     }
193     if (SUCCEEDED(res))
194     {
195         res = RegSetValueW(hkey2, NULL, REG_SZ, device_1, (lstrlenW(device_1) + 1) * sizeof(WCHAR))
196               == ERROR_SUCCESS ? S_OK : E_FAIL;
197     }
198
199     if (hkey2)
200     {
201         RegCloseKey(hkey2);
202         hkey2 = NULL;
203     }
204
205     if (SUCCEEDED(res))
206     {
207         static const WCHAR wszVProgID[] = {'V','e','r','s','i','o','n','I','n','d','e','p','e','d','e','n','t','P','r','o','g','I','D',0};
208         res = RegCreateKeyW(hkey1, wszVProgID, &hkey2)
209               == ERROR_SUCCESS ? S_OK : E_FAIL;
210     }
211     if (SUCCEEDED(res))
212     {
213         res = RegSetValueW(hkey2, NULL, REG_SZ, device, (lstrlenW(device) + 1) * sizeof(WCHAR))
214               == ERROR_SUCCESS ? S_OK : E_FAIL;
215     }
216
217     if (hkey2)
218     {
219         RegCloseKey(hkey2);
220         hkey2 = NULL;
221     }
222
223     if (hkey1)
224     {
225         RegCloseKey(hkey1);
226         hkey1 = NULL;
227     }
228
229     if (SUCCEEDED(res))
230     {
231         res = RegCreateKeyW(HKEY_CLASSES_ROOT, device, &hkey1)
232               == ERROR_SUCCESS ? S_OK : E_FAIL;
233     }
234     if (SUCCEEDED(res))
235     {
236         res = RegCreateKeyW(hkey1, clsid_keyname, &hkey2)
237               == ERROR_SUCCESS ? S_OK : E_FAIL;
238     }
239     if (SUCCEEDED(res))
240     {
241         res = RegSetValueW(hkey2, NULL, REG_SZ, pszClsidDevMon, (lstrlenW(pszClsidDevMon) + 1) * sizeof(WCHAR))
242               == ERROR_SUCCESS ? S_OK : E_FAIL;
243     }
244     if (hkey2)
245     {
246         RegCloseKey(hkey2);
247         hkey2 = NULL;
248     }
249
250     if (hkey1)
251     {
252         RegCloseKey(hkey1);
253         hkey1 = NULL;
254     }
255
256     if (SUCCEEDED(res))
257     {
258         res = RegCreateKeyW(HKEY_CLASSES_ROOT, device_1, &hkey1)
259               == ERROR_SUCCESS ? S_OK : E_FAIL;
260     }
261     if (SUCCEEDED(res))
262     {
263         res = RegCreateKeyW(hkey1, clsid_keyname, &hkey2)
264               == ERROR_SUCCESS ? S_OK : E_FAIL;
265     }
266     if (SUCCEEDED(res))
267     {
268         res = RegSetValueW(hkey2, NULL, REG_SZ, pszClsidDevMon, (lstrlenW(pszClsidDevMon) + 1) * sizeof(WCHAR))
269               == ERROR_SUCCESS ? S_OK : E_FAIL;
270     }
271
272     if (hkey2)
273         RegCloseKey(hkey2);
274
275     if (hkey1)
276         RegCloseKey(hkey1);
277
278     if (hkeyClsid)
279         RegCloseKey(hkeyClsid);
280
281     CoTaskMemFree(pszClsidDevMon);
282     CoUninitialize();
283
284     return res;
285 }
286
287 /***********************************************************************
288  *              DllUnregisterServer (DEVENUM.@)
289  */
290 HRESULT WINAPI DllUnregisterServer(void)
291 {
292         FIXME("stub!\n");
293         return E_FAIL;
294 }
295
296 static HRESULT register_clsids(int count, const register_info * pRegInfo, LPCWSTR pszThreadingModel)
297 {
298     HRESULT res = S_OK;
299     LPOLESTR clsidString = NULL;
300     HKEY hkeyClsid;
301     HKEY hkeySub;
302     HKEY hkeyInproc32;
303     HKEY hkeyInstance = NULL;
304     int i;
305     static const WCHAR wcszInproc32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
306     static const WCHAR wcszThreadingModel[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
307     static const WCHAR dll_module[] = {'d','e','v','e','n','u','m','.','d','l','l',0};
308
309     res = RegOpenKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkeyClsid)
310           == ERROR_SUCCESS ? S_OK : E_FAIL;
311
312     for (i = 0; i < count; i++)
313     {
314         hkeySub = 0;
315         if (SUCCEEDED(res))
316         {
317             res = StringFromCLSID(pRegInfo[i].clsid, &clsidString);
318         }
319         if (SUCCEEDED(res))
320         {
321             res = RegCreateKeyW(hkeyClsid, clsidString, &hkeySub)
322                   == ERROR_SUCCESS ? S_OK : E_FAIL;
323         }
324         if (pRegInfo[i].instance && SUCCEEDED(res))
325         {
326             res = RegCreateKeyW(hkeySub, wszInstanceKeyName, &hkeyInstance)
327                   == ERROR_SUCCESS ? S_OK : E_FAIL;
328             RegCloseKey(hkeyInstance);
329         }
330         if (SUCCEEDED(res))
331         {
332             RegSetValueW(hkeySub,
333                          NULL,
334                          REG_SZ,
335                          pRegInfo->friendly_name ? pRegInfo[i].friendly_name : clsidString,
336                          (lstrlenW(pRegInfo[i].friendly_name ? pRegInfo->friendly_name : clsidString) + 1) * sizeof(WCHAR));
337             res = RegCreateKeyW(hkeySub, wcszInproc32, &hkeyInproc32)
338                   == ERROR_SUCCESS ? S_OK : E_FAIL;
339         }
340         if (SUCCEEDED(res))
341         {
342             RegSetValueW(hkeyInproc32,
343                          NULL,
344                          REG_SZ,
345                          dll_module,
346                          (lstrlenW(dll_module) + 1) * sizeof(WCHAR));
347             RegSetValueExW(hkeyInproc32,
348                            wcszThreadingModel,
349                            0,
350                            REG_SZ,
351                            (LPCVOID)pszThreadingModel,
352                            (lstrlenW(pszThreadingModel) + 1) * sizeof(WCHAR));
353             RegCloseKey(hkeyInproc32);
354         }
355         if (hkeySub) RegCloseKey(hkeySub);
356         CoTaskMemFree(clsidString);
357         clsidString = NULL;
358     }
359
360     RegCloseKey(hkeyClsid);
361
362     return res;
363 }
364
365 typedef HRESULT (WINAPI *DllRegisterServer_func)(void);
366
367 /* calls DllRegisterServer() for the Quartz DLL */
368 static void DEVENUM_RegisterQuartz(void)
369 {
370     HANDLE hDLL = LoadLibraryA("quartz.dll");
371     DllRegisterServer_func pDllRegisterServer = NULL;
372     if (hDLL)
373         pDllRegisterServer = (DllRegisterServer_func)GetProcAddress(hDLL, "DllRegisterServer");
374     if (pDllRegisterServer)
375     {
376         HRESULT hr = pDllRegisterServer();
377         if (FAILED(hr))
378             ERR("Failed to register Quartz. Error was 0x%x)\n", hr);
379     }
380 }