Use Interlocked* functions in AddRef and Release.
[wine.git] / dlls / devenum / createdevenum.c
blob8860e43e1c92949be50c5e29f60eace7d9eaf3da
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTES ON THIS FILE:
21 * - Implements ICreateDevEnum interface which creates an IEnumMoniker
22 * implementation
23 * - Also creates the special registry keys created at run-time
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "devenum_private.h"
31 #include "wine/debug.h"
32 #include "mmddk.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
36 extern IEnumMonikerVtbl IEnumMoniker_Vtbl;
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();
51 /**********************************************************************
52 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
54 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
55 ICreateDevEnum * iface,
56 REFIID riid,
57 LPVOID *ppvObj)
59 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
60 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
62 if (This == NULL || ppvObj == NULL) return E_POINTER;
64 if (IsEqualGUID(riid, &IID_IUnknown) ||
65 IsEqualGUID(riid, &IID_ICreateDevEnum))
67 *ppvObj = (LPVOID)iface;
68 DEVENUM_ICreateDevEnum_AddRef(iface);
69 return S_OK;
72 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
73 return E_NOINTERFACE;
76 /**********************************************************************
77 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
79 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
81 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
82 TRACE("\n");
84 if (This == NULL) return E_POINTER;
86 if (InterlockedIncrement(&This->ref) == 1) {
87 InterlockedIncrement(&dll_ref);
89 return This->ref;
92 /**********************************************************************
93 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
95 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
97 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
98 TRACE("\n");
100 if (This == NULL) return E_POINTER;
102 if (InterlockedDecrement(&This->ref) == 0) {
103 InterlockedDecrement(&dll_ref);
105 return This->ref;
108 /**********************************************************************
109 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
111 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
112 ICreateDevEnum * iface,
113 REFCLSID clsidDeviceClass,
114 IEnumMoniker **ppEnumMoniker,
115 DWORD dwFlags)
117 WCHAR wszRegKey[MAX_PATH];
118 EnumMonikerImpl * pEnumMoniker;
119 HKEY hkey;
120 HKEY hbasekey;
121 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
123 TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
125 if (!ppEnumMoniker)
126 return E_POINTER;
128 *ppEnumMoniker = NULL;
130 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
131 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
132 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
134 hbasekey = HKEY_CURRENT_USER;
135 strcpyW(wszRegKey, wszActiveMovieKey);
137 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
138 return E_OUTOFMEMORY;
140 else
142 hbasekey = HKEY_CLASSES_ROOT;
143 strcpyW(wszRegKey, clsid_keyname);
144 strcatW(wszRegKey, wszRegSeparator);
146 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
147 return E_OUTOFMEMORY;
149 strcatW(wszRegKey, wszRegSeparator);
150 strcatW(wszRegKey, wszInstanceKeyName);
153 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
155 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
156 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
157 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
159 HRESULT hr = DEVENUM_CreateSpecialCategories();
160 if (FAILED(hr))
161 return hr;
162 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
164 ERR("Couldn't open registry key for special device: %s\n",
165 debugstr_guid(clsidDeviceClass));
166 return S_FALSE;
169 else
171 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
172 return S_FALSE;
176 pEnumMoniker = (EnumMonikerImpl *)CoTaskMemAlloc(sizeof(EnumMonikerImpl));
177 if (!pEnumMoniker)
178 return E_OUTOFMEMORY;
180 pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
181 pEnumMoniker->ref = 1;
182 pEnumMoniker->index = 0;
183 pEnumMoniker->hkey = hkey;
185 *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
187 return S_OK;
190 /**********************************************************************
191 * ICreateDevEnum_Vtbl
193 static ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
195 DEVENUM_ICreateDevEnum_QueryInterface,
196 DEVENUM_ICreateDevEnum_AddRef,
197 DEVENUM_ICreateDevEnum_Release,
198 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
201 /**********************************************************************
202 * static CreateDevEnum instance
204 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl, 0 };
206 /**********************************************************************
207 * DEVENUM_CreateAMCategoryKey (INTERNAL)
209 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
210 * Microsoft\ActiveMovie\devenum\{clsid}
212 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
214 WCHAR wszRegKey[MAX_PATH];
215 HRESULT res = S_OK;
216 HKEY hkeyDummy = NULL;
218 strcpyW(wszRegKey, wszActiveMovieKey);
220 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
221 res = E_INVALIDARG;
223 if (SUCCEEDED(res))
224 res = HRESULT_FROM_WIN32(
225 RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy));
227 if (hkeyDummy)
228 RegCloseKey(hkeyDummy);
230 if (FAILED(res))
231 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
233 return res;
236 /**********************************************************************
237 * DEVENUM_CreateSpecialCategories (INTERNAL)
239 * Creates the keys in the registry for the dynamic categories
241 static HRESULT DEVENUM_CreateSpecialCategories()
243 HRESULT res;
244 WCHAR szDSoundNameFormat[MAX_PATH + 1];
245 WCHAR szDSoundName[MAX_PATH + 1];
246 DWORD iDefaultDevice = -1;
247 UINT numDevs;
248 IFilterMapper2 * pMapper = NULL;
249 REGFILTER2 rf2;
250 REGFILTERPINS2 rfp2;
252 rf2.dwVersion = 2;
253 rf2.dwMerit = MERIT_PREFERRED;
254 rf2.u.s1.cPins2 = 1;
255 rf2.u.s1.rgPins2 = &rfp2;
256 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
257 rfp2.cInstances = 1;
258 rfp2.nMediums = 0;
259 rfp2.lpMedium = NULL;
260 rfp2.clsPinCategory = &IID_NULL;
262 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
264 ERR("Couldn't get string resource (GetLastError() is %ld)\n", GetLastError());
265 return HRESULT_FROM_WIN32(GetLastError());
268 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
269 &IID_IFilterMapper2, (void **) &pMapper);
271 * Fill in info for devices
273 if (SUCCEEDED(res))
275 UINT i;
276 WAVEOUTCAPSW wocaps;
277 WAVEINCAPSW wicaps;
278 MIDIOUTCAPSW mocaps;
279 REGPINTYPES * pTypes;
281 numDevs = waveOutGetNumDevs();
283 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
284 if (FAILED(res)) /* can't register any devices in this category */
285 numDevs = 0;
287 for (i = 0; i < numDevs; i++)
289 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
290 == MMSYSERR_NOERROR)
292 IMoniker * pMoniker = NULL;
294 rfp2.nMediaTypes = 1;
295 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
296 if (!pTypes)
298 IFilterMapper2_Release(pMapper);
299 return E_OUTOFMEMORY;
301 /* FIXME: Native devenum seems to register a lot more types for
302 * DSound than we do. Not sure what purpose they serve */
303 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
304 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
306 rfp2.lpMediaType = pTypes;
308 res = IFilterMapper2_RegisterFilter(pMapper,
309 &CLSID_AudioRender,
310 wocaps.szPname,
311 &pMoniker,
312 &CLSID_AudioRendererCategory,
313 wocaps.szPname,
314 &rf2);
316 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
318 if (pMoniker)
319 IMoniker_Release(pMoniker);
321 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
322 res = IFilterMapper2_RegisterFilter(pMapper,
323 &CLSID_DSoundRender,
324 szDSoundName,
325 &pMoniker,
326 &CLSID_AudioRendererCategory,
327 szDSoundName,
328 &rf2);
330 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
332 if (pMoniker)
333 IMoniker_Release(pMoniker);
335 if (i == iDefaultDevice)
337 FIXME("Default device\n");
340 CoTaskMemFree(pTypes);
344 numDevs = waveInGetNumDevs();
346 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
347 if (FAILED(res)) /* can't register any devices in this category */
348 numDevs = 0;
350 for (i = 0; i < numDevs; i++)
352 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
353 == MMSYSERR_NOERROR)
355 IMoniker * pMoniker = NULL;
357 rfp2.nMediaTypes = 1;
358 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
359 if (!pTypes)
361 IFilterMapper2_Release(pMapper);
362 return E_OUTOFMEMORY;
365 /* FIXME: Not sure if these are correct */
366 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
367 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
369 rfp2.lpMediaType = pTypes;
371 res = IFilterMapper2_RegisterFilter(pMapper,
372 &CLSID_AudioRecord,
373 wicaps.szPname,
374 &pMoniker,
375 &CLSID_AudioInputDeviceCategory,
376 wicaps.szPname,
377 &rf2);
379 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
381 if (pMoniker)
382 IMoniker_Release(pMoniker);
384 CoTaskMemFree(pTypes);
388 numDevs = midiOutGetNumDevs();
390 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
391 if (FAILED(res)) /* can't register any devices in this category */
392 numDevs = 0;
394 for (i = 0; i < numDevs; i++)
396 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
397 == MMSYSERR_NOERROR)
399 IMoniker * pMoniker = NULL;
401 rfp2.nMediaTypes = 1;
402 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
403 if (!pTypes)
405 IFilterMapper2_Release(pMapper);
406 return E_OUTOFMEMORY;
409 /* FIXME: Not sure if these are correct */
410 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
411 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
413 rfp2.lpMediaType = pTypes;
415 res = IFilterMapper2_RegisterFilter(pMapper,
416 &CLSID_AVIMIDIRender,
417 mocaps.szPname,
418 &pMoniker,
419 &CLSID_MidiRendererCategory,
420 mocaps.szPname,
421 &rf2);
423 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
424 /* Native version sets MidiOutId */
426 if (pMoniker)
427 IMoniker_Release(pMoniker);
429 if (i == iDefaultDevice)
431 FIXME("Default device\n");
434 CoTaskMemFree(pTypes);
439 if (pMapper)
440 IFilterMapper2_Release(pMapper);
441 return res;