2 * ICreateDevEnum implementation for DEVENUM.dll
4 * Copyright (C) 2002 Robert Shearman
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.
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.
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 St, Fifth Floor, Boston, MA 02110-1301, USA
21 * - Implements ICreateDevEnum interface which creates an IEnumMoniker
23 * - Also creates the special registry keys created at run-time
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "devenum_private.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
38 extern HINSTANCE DEVENUM_hInstance;
40 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
42 static const WCHAR wszRegSeparator[] = {'\\', 0 };
43 static const WCHAR wszActiveMovieKey[] = {'S','o','f','t','w','a','r','e','\\',
44 'M','i','c','r','o','s','o','f','t','\\',
45 'A','c','t','i','v','e','M','o','v','i','e','\\',
46 'd','e','v','e','n','u','m','\\',0};
48 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
49 static HRESULT DEVENUM_CreateSpecialCategories(void);
51 /**********************************************************************
52 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
54 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
55 ICreateDevEnum * iface,
59 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
61 if (ppvObj == NULL) return E_POINTER;
63 if (IsEqualGUID(riid, &IID_IUnknown) ||
64 IsEqualGUID(riid, &IID_ICreateDevEnum))
66 *ppvObj = (LPVOID)iface;
67 DEVENUM_ICreateDevEnum_AddRef(iface);
71 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
75 /**********************************************************************
76 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
78 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
84 return 2; /* non-heap based object */
87 /**********************************************************************
88 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
90 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
94 DEVENUM_UnlockModule();
96 return 1; /* non-heap based object */
99 HRESULT DEVENUM_GetCategoryKey(REFCLSID clsidDeviceClass, HKEY *pBaseKey, WCHAR *wszRegKeyName, UINT maxLen)
101 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
102 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
103 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
104 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
106 *pBaseKey = HKEY_CURRENT_USER;
107 strcpyW(wszRegKeyName, wszActiveMovieKey);
109 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + strlenW(wszRegKeyName), maxLen - strlenW(wszRegKeyName)))
110 return E_OUTOFMEMORY;
114 *pBaseKey = HKEY_CLASSES_ROOT;
115 strcpyW(wszRegKeyName, clsid_keyname);
116 strcatW(wszRegKeyName, wszRegSeparator);
118 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + CLSID_STR_LEN, maxLen - CLSID_STR_LEN))
119 return E_OUTOFMEMORY;
121 strcatW(wszRegKeyName, wszRegSeparator);
122 strcatW(wszRegKeyName, wszInstanceKeyName);
128 /**********************************************************************
129 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
131 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
132 ICreateDevEnum * iface,
133 REFCLSID clsidDeviceClass,
134 IEnumMoniker **ppEnumMoniker,
137 WCHAR wszRegKey[MAX_PATH];
141 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
143 TRACE("(%p)->(%s, %p, %x)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
148 *ppEnumMoniker = NULL;
150 hr = DEVENUM_GetCategoryKey(clsidDeviceClass, &hbasekey, wszRegKey, MAX_PATH);
154 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
156 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
157 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
158 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
159 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
161 hr = DEVENUM_CreateSpecialCategories();
164 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
166 ERR("Couldn't open registry key for special device: %s\n",
167 debugstr_guid(clsidDeviceClass));
173 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
178 return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
181 /**********************************************************************
182 * ICreateDevEnum_Vtbl
184 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
186 DEVENUM_ICreateDevEnum_QueryInterface,
187 DEVENUM_ICreateDevEnum_AddRef,
188 DEVENUM_ICreateDevEnum_Release,
189 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
192 /**********************************************************************
193 * static CreateDevEnum instance
195 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
197 /**********************************************************************
198 * DEVENUM_CreateAMCategoryKey (INTERNAL)
200 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
201 * Microsoft\ActiveMovie\devenum\{clsid}
203 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
205 WCHAR wszRegKey[MAX_PATH];
207 HKEY hkeyDummy = NULL;
209 strcpyW(wszRegKey, wszActiveMovieKey);
211 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
216 LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
217 res = HRESULT_FROM_WIN32(lRes);
221 RegCloseKey(hkeyDummy);
224 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
229 /**********************************************************************
230 * DEVENUM_CreateSpecialCategories (INTERNAL)
232 * Creates the keys in the registry for the dynamic categories
234 static HRESULT DEVENUM_CreateSpecialCategories(void)
237 WCHAR szDSoundNameFormat[MAX_PATH + 1];
238 WCHAR szDSoundName[MAX_PATH + 1];
239 DWORD iDefaultDevice = -1;
241 IFilterMapper2 * pMapper = NULL;
246 rf2.dwMerit = MERIT_PREFERRED;
248 rf2.u.s1.rgPins2 = &rfp2;
251 rfp2.lpMedium = NULL;
252 rfp2.clsPinCategory = &IID_NULL;
254 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
256 ERR("Couldn't get string resource (GetLastError() is %d)\n", GetLastError());
257 return HRESULT_FROM_WIN32(GetLastError());
260 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
261 &IID_IFilterMapper2, (void **) &pMapper);
263 * Fill in info for devices
271 REGPINTYPES * pTypes;
273 numDevs = waveOutGetNumDevs();
275 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
276 if (FAILED(res)) /* can't register any devices in this category */
279 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
280 for (i = 0; i < numDevs; i++)
282 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
285 IMoniker * pMoniker = NULL;
287 rfp2.nMediaTypes = 1;
288 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
291 IFilterMapper2_Release(pMapper);
292 return E_OUTOFMEMORY;
294 /* FIXME: Native devenum seems to register a lot more types for
295 * DSound than we do. Not sure what purpose they serve */
296 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
297 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
299 rfp2.lpMediaType = pTypes;
301 res = IFilterMapper2_RegisterFilter(pMapper,
305 &CLSID_AudioRendererCategory,
309 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
312 IMoniker_Release(pMoniker);
314 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
315 res = IFilterMapper2_RegisterFilter(pMapper,
319 &CLSID_AudioRendererCategory,
323 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
326 IMoniker_Release(pMoniker);
328 if (i == iDefaultDevice)
330 FIXME("Default device\n");
333 CoTaskMemFree(pTypes);
337 numDevs = waveInGetNumDevs();
339 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
340 if (FAILED(res)) /* can't register any devices in this category */
343 rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
344 for (i = 0; i < numDevs; i++)
346 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
349 IMoniker * pMoniker = NULL;
351 rfp2.nMediaTypes = 1;
352 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
355 IFilterMapper2_Release(pMapper);
356 return E_OUTOFMEMORY;
359 /* FIXME: Not sure if these are correct */
360 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
361 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
363 rfp2.lpMediaType = pTypes;
365 res = IFilterMapper2_RegisterFilter(pMapper,
369 &CLSID_AudioInputDeviceCategory,
373 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
376 IMoniker_Release(pMoniker);
378 CoTaskMemFree(pTypes);
382 numDevs = midiOutGetNumDevs();
384 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
385 if (FAILED(res)) /* can't register any devices in this category */
388 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
389 for (i = 0; i < numDevs; i++)
391 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
394 IMoniker * pMoniker = NULL;
396 rfp2.nMediaTypes = 1;
397 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
400 IFilterMapper2_Release(pMapper);
401 return E_OUTOFMEMORY;
404 /* FIXME: Not sure if these are correct */
405 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
406 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
408 rfp2.lpMediaType = pTypes;
410 res = IFilterMapper2_RegisterFilter(pMapper,
411 &CLSID_AVIMIDIRender,
414 &CLSID_MidiRendererCategory,
418 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
419 /* Native version sets MidiOutId */
422 IMoniker_Release(pMoniker);
424 if (i == iDefaultDevice)
426 FIXME("Default device\n");
429 CoTaskMemFree(pTypes);
432 res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
434 for (i = 0; i < 10; i++)
436 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
438 if (capGetDriverDescriptionW ((WORD) i,
439 szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
440 szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
442 IMoniker * pMoniker = NULL;
443 IPropertyBag * pPropBag = NULL;
444 WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
445 snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
446 /* The above code prevents 1 device with a different ID overwriting another */
448 rfp2.nMediaTypes = 1;
449 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
451 IFilterMapper2_Release(pMapper);
452 return E_OUTOFMEMORY;
455 pTypes[0].clsMajorType = &MEDIATYPE_Video;
456 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
458 rfp2.lpMediaType = pTypes;
460 res = IFilterMapper2_RegisterFilter(pMapper,
464 &CLSID_VideoInputDeviceCategory,
469 OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
472 V_UNION(&var, ulVal) = i;
473 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
475 res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
476 IMoniker_Release(pMoniker);
479 if (i == iDefaultDevice) FIXME("Default device\n");
480 CoTaskMemFree(pTypes);
486 IFilterMapper2_Release(pMapper);