- use Interlocked* functions in AddRef and Release.
[wine/wine64.git] / dlls / devenum / createdevenum.c
blob67a84696a40e8d79fc66446162b48d4853eb1214
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 HINSTANCE DEVENUM_hInstance;
38 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
40 static const WCHAR wszRegSeparator[] = {'\\', 0 };
41 static const WCHAR wszActiveMovieKey[] = {'S','o','f','t','w','a','r','e','\\',
42 'M','i','c','r','o','s','o','f','t','\\',
43 'A','c','t','i','v','e','M','o','v','i','e','\\',
44 'd','e','v','e','n','u','m','\\',0};
46 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
47 static HRESULT DEVENUM_CreateSpecialCategories(void);
49 /**********************************************************************
50 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
52 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
53 ICreateDevEnum * iface,
54 REFIID riid,
55 LPVOID *ppvObj)
57 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
59 if (ppvObj == NULL) return E_POINTER;
61 if (IsEqualGUID(riid, &IID_IUnknown) ||
62 IsEqualGUID(riid, &IID_ICreateDevEnum))
64 *ppvObj = (LPVOID)iface;
65 DEVENUM_ICreateDevEnum_AddRef(iface);
66 return S_OK;
69 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
70 return E_NOINTERFACE;
73 /**********************************************************************
74 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
76 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
78 TRACE("\n");
80 DEVENUM_LockModule();
82 return 2; /* non-heap based object */
85 /**********************************************************************
86 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
88 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
90 TRACE("\n");
92 DEVENUM_UnlockModule();
94 return 1; /* non-heap based object */
97 /**********************************************************************
98 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
100 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
101 ICreateDevEnum * iface,
102 REFCLSID clsidDeviceClass,
103 IEnumMoniker **ppEnumMoniker,
104 DWORD dwFlags)
106 WCHAR wszRegKey[MAX_PATH];
107 HKEY hkey;
108 HKEY hbasekey;
109 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
111 TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
113 if (!ppEnumMoniker)
114 return E_POINTER;
116 *ppEnumMoniker = NULL;
118 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
119 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
120 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
122 hbasekey = HKEY_CURRENT_USER;
123 strcpyW(wszRegKey, wszActiveMovieKey);
125 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
126 return E_OUTOFMEMORY;
128 else
130 hbasekey = HKEY_CLASSES_ROOT;
131 strcpyW(wszRegKey, clsid_keyname);
132 strcatW(wszRegKey, wszRegSeparator);
134 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
135 return E_OUTOFMEMORY;
137 strcatW(wszRegKey, wszRegSeparator);
138 strcatW(wszRegKey, wszInstanceKeyName);
141 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
143 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
144 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
145 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
147 HRESULT hr = DEVENUM_CreateSpecialCategories();
148 if (FAILED(hr))
149 return hr;
150 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
152 ERR("Couldn't open registry key for special device: %s\n",
153 debugstr_guid(clsidDeviceClass));
154 return S_FALSE;
157 else
159 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
160 return S_FALSE;
164 return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
167 /**********************************************************************
168 * ICreateDevEnum_Vtbl
170 static ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
172 DEVENUM_ICreateDevEnum_QueryInterface,
173 DEVENUM_ICreateDevEnum_AddRef,
174 DEVENUM_ICreateDevEnum_Release,
175 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
178 /**********************************************************************
179 * static CreateDevEnum instance
181 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
183 /**********************************************************************
184 * DEVENUM_CreateAMCategoryKey (INTERNAL)
186 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
187 * Microsoft\ActiveMovie\devenum\{clsid}
189 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
191 WCHAR wszRegKey[MAX_PATH];
192 HRESULT res = S_OK;
193 HKEY hkeyDummy = NULL;
195 strcpyW(wszRegKey, wszActiveMovieKey);
197 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
198 res = E_INVALIDARG;
200 if (SUCCEEDED(res))
201 res = HRESULT_FROM_WIN32(
202 RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy));
204 if (hkeyDummy)
205 RegCloseKey(hkeyDummy);
207 if (FAILED(res))
208 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
210 return res;
213 /**********************************************************************
214 * DEVENUM_CreateSpecialCategories (INTERNAL)
216 * Creates the keys in the registry for the dynamic categories
218 static HRESULT DEVENUM_CreateSpecialCategories()
220 HRESULT res;
221 WCHAR szDSoundNameFormat[MAX_PATH + 1];
222 WCHAR szDSoundName[MAX_PATH + 1];
223 DWORD iDefaultDevice = -1;
224 UINT numDevs;
225 IFilterMapper2 * pMapper = NULL;
226 REGFILTER2 rf2;
227 REGFILTERPINS2 rfp2;
229 rf2.dwVersion = 2;
230 rf2.dwMerit = MERIT_PREFERRED;
231 rf2.u.s1.cPins2 = 1;
232 rf2.u.s1.rgPins2 = &rfp2;
233 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
234 rfp2.cInstances = 1;
235 rfp2.nMediums = 0;
236 rfp2.lpMedium = NULL;
237 rfp2.clsPinCategory = &IID_NULL;
239 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
241 ERR("Couldn't get string resource (GetLastError() is %ld)\n", GetLastError());
242 return HRESULT_FROM_WIN32(GetLastError());
245 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
246 &IID_IFilterMapper2, (void **) &pMapper);
248 * Fill in info for devices
250 if (SUCCEEDED(res))
252 UINT i;
253 WAVEOUTCAPSW wocaps;
254 WAVEINCAPSW wicaps;
255 MIDIOUTCAPSW mocaps;
256 REGPINTYPES * pTypes;
258 numDevs = waveOutGetNumDevs();
260 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
261 if (FAILED(res)) /* can't register any devices in this category */
262 numDevs = 0;
264 for (i = 0; i < numDevs; i++)
266 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
267 == MMSYSERR_NOERROR)
269 IMoniker * pMoniker = NULL;
271 rfp2.nMediaTypes = 1;
272 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
273 if (!pTypes)
275 IFilterMapper2_Release(pMapper);
276 return E_OUTOFMEMORY;
278 /* FIXME: Native devenum seems to register a lot more types for
279 * DSound than we do. Not sure what purpose they serve */
280 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
281 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
283 rfp2.lpMediaType = pTypes;
285 res = IFilterMapper2_RegisterFilter(pMapper,
286 &CLSID_AudioRender,
287 wocaps.szPname,
288 &pMoniker,
289 &CLSID_AudioRendererCategory,
290 wocaps.szPname,
291 &rf2);
293 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
295 if (pMoniker)
296 IMoniker_Release(pMoniker);
298 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
299 res = IFilterMapper2_RegisterFilter(pMapper,
300 &CLSID_DSoundRender,
301 szDSoundName,
302 &pMoniker,
303 &CLSID_AudioRendererCategory,
304 szDSoundName,
305 &rf2);
307 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
309 if (pMoniker)
310 IMoniker_Release(pMoniker);
312 if (i == iDefaultDevice)
314 FIXME("Default device\n");
317 CoTaskMemFree(pTypes);
321 numDevs = waveInGetNumDevs();
323 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
324 if (FAILED(res)) /* can't register any devices in this category */
325 numDevs = 0;
327 for (i = 0; i < numDevs; i++)
329 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
330 == MMSYSERR_NOERROR)
332 IMoniker * pMoniker = NULL;
334 rfp2.nMediaTypes = 1;
335 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
336 if (!pTypes)
338 IFilterMapper2_Release(pMapper);
339 return E_OUTOFMEMORY;
342 /* FIXME: Not sure if these are correct */
343 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
344 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
346 rfp2.lpMediaType = pTypes;
348 res = IFilterMapper2_RegisterFilter(pMapper,
349 &CLSID_AudioRecord,
350 wicaps.szPname,
351 &pMoniker,
352 &CLSID_AudioInputDeviceCategory,
353 wicaps.szPname,
354 &rf2);
356 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
358 if (pMoniker)
359 IMoniker_Release(pMoniker);
361 CoTaskMemFree(pTypes);
365 numDevs = midiOutGetNumDevs();
367 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
368 if (FAILED(res)) /* can't register any devices in this category */
369 numDevs = 0;
371 for (i = 0; i < numDevs; i++)
373 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
374 == MMSYSERR_NOERROR)
376 IMoniker * pMoniker = NULL;
378 rfp2.nMediaTypes = 1;
379 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
380 if (!pTypes)
382 IFilterMapper2_Release(pMapper);
383 return E_OUTOFMEMORY;
386 /* FIXME: Not sure if these are correct */
387 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
388 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
390 rfp2.lpMediaType = pTypes;
392 res = IFilterMapper2_RegisterFilter(pMapper,
393 &CLSID_AVIMIDIRender,
394 mocaps.szPname,
395 &pMoniker,
396 &CLSID_MidiRendererCategory,
397 mocaps.szPname,
398 &rf2);
400 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
401 /* Native version sets MidiOutId */
403 if (pMoniker)
404 IMoniker_Release(pMoniker);
406 if (i == iDefaultDevice)
408 FIXME("Default device\n");
411 CoTaskMemFree(pTypes);
416 if (pMapper)
417 IFilterMapper2_Release(pMapper);
418 return res;