GetModuleFileName[AW] doesn't terminate the string if the buffer is
[wine] / dlls / devenum / createdevenum.c
1 /*
2  *      ICreateDevEnum implementation for DEVENUM.dll
3  *
4  * Copyright (C) 2002 Robert Shearman
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
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
24  */
25
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
28
29 #include "devenum_private.h"
30
31 #include "wine/debug.h"
32 #include "mmddk.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
35
36 extern ICOM_VTABLE(IEnumMoniker) IEnumMoniker_Vtbl;
37
38 extern HINSTANCE DEVENUM_hInstance;
39
40 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
41
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};
47
48 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
49 static HRESULT DEVENUM_CreateSpecialCategories();
50
51 /**********************************************************************
52  * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
53  */
54 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
55     ICreateDevEnum * iface,
56     REFIID riid,
57     LPVOID *ppvObj)
58 {
59     ICOM_THIS(CreateDevEnumImpl, iface);
60     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
61
62     if (This == NULL || ppvObj == NULL) return E_POINTER;
63
64     if (IsEqualGUID(riid, &IID_IUnknown) ||
65         IsEqualGUID(riid, &IID_ICreateDevEnum))
66     {
67         *ppvObj = (LPVOID)iface;
68         DEVENUM_ICreateDevEnum_AddRef(iface);
69         return S_OK;
70     }
71
72     FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
73     return E_NOINTERFACE;
74 }
75
76 /**********************************************************************
77  * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
78  */
79 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
80 {
81     ICOM_THIS(CreateDevEnumImpl, iface);
82     TRACE("\n");
83
84     if (This == NULL) return E_POINTER;
85
86     if (InterlockedIncrement(&This->ref) == 1) {
87         InterlockedIncrement(&dll_ref);
88     }
89     return This->ref;
90 }
91
92 /**********************************************************************
93  * DEVENUM_ICreateDevEnum_Release (also IUnknown)
94  */
95 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
96 {
97     ICOM_THIS(CreateDevEnumImpl, iface);
98     TRACE("\n");
99
100     if (This == NULL) return E_POINTER;
101
102     if (InterlockedDecrement(&This->ref) == 0) {
103         InterlockedDecrement(&dll_ref);
104     }
105     return This->ref;
106 }
107
108 /**********************************************************************
109  * DEVENUM_ICreateDevEnum_CreateClassEnumerator
110  */
111 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
112     ICreateDevEnum * iface,
113     REFCLSID clsidDeviceClass,
114     IEnumMoniker **ppEnumMoniker,
115     DWORD dwFlags)
116 {
117     WCHAR wszRegKey[MAX_PATH];
118     EnumMonikerImpl * pEnumMoniker;
119     HKEY hkey;
120     HKEY hbasekey;
121     ICOM_THIS(CreateDevEnumImpl, iface);
122
123     TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
124
125     if (!ppEnumMoniker)
126         return E_POINTER;
127
128     *ppEnumMoniker = NULL;
129
130     if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
131         IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
132         IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
133     {
134         hbasekey = HKEY_CURRENT_USER;
135         strcpyW(wszRegKey, wszActiveMovieKey);
136
137         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
138             return E_OUTOFMEMORY;
139     }
140     else
141     {
142         hbasekey = HKEY_CLASSES_ROOT;
143         strcpyW(wszRegKey, clsid_keyname);
144         strcatW(wszRegKey, wszRegSeparator);
145
146         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
147             return E_OUTOFMEMORY;
148
149         strcatW(wszRegKey, wszRegSeparator);
150         strcatW(wszRegKey, wszInstanceKeyName);
151     }
152
153     if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
154     {
155         if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
156             IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
157             IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
158         {
159              HRESULT hr = DEVENUM_CreateSpecialCategories();
160              if (FAILED(hr))
161                  return hr;
162              if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
163              {
164                  ERR("Couldn't open registry key for special device: %s\n",
165                      debugstr_guid(clsidDeviceClass));
166                  return S_FALSE;
167              }
168         }
169         else
170         {
171             FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
172             return S_FALSE;
173         }
174     }
175
176     pEnumMoniker = (EnumMonikerImpl *)CoTaskMemAlloc(sizeof(EnumMonikerImpl));
177     if (!pEnumMoniker)
178         return E_OUTOFMEMORY;
179
180     pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
181     pEnumMoniker->ref = 1;
182     pEnumMoniker->index = 0;
183     pEnumMoniker->hkey = hkey;
184
185     *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
186
187     return S_OK;
188 }
189
190 /**********************************************************************
191  * ICreateDevEnum_Vtbl
192  */
193 static ICOM_VTABLE(ICreateDevEnum) ICreateDevEnum_Vtbl =
194 {
195     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
196     DEVENUM_ICreateDevEnum_QueryInterface,
197     DEVENUM_ICreateDevEnum_AddRef,
198     DEVENUM_ICreateDevEnum_Release,
199     DEVENUM_ICreateDevEnum_CreateClassEnumerator,
200 };
201
202 /**********************************************************************
203  * static CreateDevEnum instance
204  */
205 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl, 0 };
206
207 /**********************************************************************
208  * DEVENUM_CreateAMCategoryKey (INTERNAL)
209  *
210  * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
211  * Microsoft\ActiveMovie\devenum\{clsid}
212  */
213 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
214 {
215     WCHAR wszRegKey[MAX_PATH];
216     HRESULT res = S_OK;
217     HKEY hkeyDummy = NULL;
218
219     strcpyW(wszRegKey, wszActiveMovieKey);
220
221     if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
222         res = E_INVALIDARG;
223
224     if (SUCCEEDED(res))
225         res = HRESULT_FROM_WIN32(
226             RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy));
227
228     if (hkeyDummy)
229         RegCloseKey(hkeyDummy);
230
231     if (FAILED(res))
232         ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
233
234     return res;
235 }
236
237 /**********************************************************************
238  * DEVENUM_CreateSpecialCategories (INTERNAL)
239  *
240  * Creates the keys in the registry for the dynamic categories
241  */
242 static HRESULT DEVENUM_CreateSpecialCategories()
243 {
244     HRESULT res;
245     WCHAR szDSoundNameFormat[MAX_PATH + 1];
246     WCHAR szDSoundName[MAX_PATH + 1];
247     DWORD iDefaultDevice = -1;
248     UINT numDevs;
249     IFilterMapper2 * pMapper = NULL;
250     REGFILTER2 rf2;
251     REGFILTERPINS2 rfp2;
252
253     rf2.dwVersion = 2;
254     rf2.dwMerit = MERIT_PREFERRED;
255     rf2.u.s1.cPins2 = 1;
256     rf2.u.s1.rgPins2 = &rfp2;
257     rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
258     rfp2.cInstances = 1;
259     rfp2.nMediums = 0;
260     rfp2.lpMedium = NULL;
261     rfp2.clsPinCategory = &IID_NULL;
262
263     if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
264     {
265         ERR("Couldn't get string resource (GetLastError() is %ld)\n", GetLastError());
266         return HRESULT_FROM_WIN32(GetLastError());
267     }
268
269     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
270                            &IID_IFilterMapper2, (void **) &pMapper);
271     /*
272      * Fill in info for devices
273      */
274     if (SUCCEEDED(res))
275     {
276         UINT i;
277         WAVEOUTCAPSW wocaps;
278         WAVEINCAPSW wicaps;
279         MIDIOUTCAPSW mocaps;
280         REGPINTYPES * pTypes;
281
282         numDevs = waveOutGetNumDevs();
283
284         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
285         if (FAILED(res)) /* can't register any devices in this category */
286             numDevs = 0;
287
288         for (i = 0; i < numDevs; i++)
289         {
290             if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
291                 == MMSYSERR_NOERROR)
292             {
293                 IMoniker * pMoniker = NULL;
294
295                 rfp2.nMediaTypes = 1;
296                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
297                 if (!pTypes)
298                 {
299                     IFilterMapper2_Release(pMapper);
300                     return E_OUTOFMEMORY;
301                 }
302                 /* FIXME: Native devenum seems to register a lot more types for
303                  * DSound than we do. Not sure what purpose they serve */
304                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
305                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
306
307                 rfp2.lpMediaType = pTypes;
308
309                 res = IFilterMapper2_RegisterFilter(pMapper,
310                                               &CLSID_AudioRender,
311                                               wocaps.szPname,
312                                               &pMoniker,
313                                               &CLSID_AudioRendererCategory,
314                                               wocaps.szPname,
315                                               &rf2);
316
317                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
318
319                 if (pMoniker)
320                     IMoniker_Release(pMoniker);
321
322                 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
323                 res = IFilterMapper2_RegisterFilter(pMapper,
324                                               &CLSID_DSoundRender,
325                                               szDSoundName,
326                                               &pMoniker,
327                                               &CLSID_AudioRendererCategory,
328                                               szDSoundName,
329                                               &rf2);
330
331                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
332
333                 if (pMoniker)
334                     IMoniker_Release(pMoniker);
335
336                 if (i == iDefaultDevice)
337                 {
338                     FIXME("Default device\n");
339                 }
340
341                 CoTaskMemFree(pTypes);
342             }
343         }
344
345         numDevs = waveInGetNumDevs();
346
347         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
348         if (FAILED(res)) /* can't register any devices in this category */
349             numDevs = 0;
350
351         for (i = 0; i < numDevs; i++)
352         {
353             if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
354                 == MMSYSERR_NOERROR)
355             {
356                 IMoniker * pMoniker = NULL;
357
358                 rfp2.nMediaTypes = 1;
359                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
360                 if (!pTypes)
361                 {
362                     IFilterMapper2_Release(pMapper);
363                     return E_OUTOFMEMORY;
364                 }
365
366                 /* FIXME: Not sure if these are correct */
367                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
368                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
369
370                 rfp2.lpMediaType = pTypes;
371
372                 res = IFilterMapper2_RegisterFilter(pMapper,
373                                               &CLSID_AudioRecord,
374                                               wicaps.szPname,
375                                               &pMoniker,
376                                               &CLSID_AudioInputDeviceCategory,
377                                               wicaps.szPname,
378                                               &rf2);
379
380                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
381
382                 if (pMoniker)
383                     IMoniker_Release(pMoniker);
384
385                 CoTaskMemFree(pTypes);
386             }
387         }
388
389         numDevs = midiOutGetNumDevs();
390
391         res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
392         if (FAILED(res)) /* can't register any devices in this category */
393             numDevs = 0;
394
395         for (i = 0; i < numDevs; i++)
396         {
397             if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
398                 == MMSYSERR_NOERROR)
399             {
400                 IMoniker * pMoniker = NULL;
401
402                 rfp2.nMediaTypes = 1;
403                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
404                 if (!pTypes)
405                 {
406                     IFilterMapper2_Release(pMapper);
407                     return E_OUTOFMEMORY;
408                 }
409
410                 /* FIXME: Not sure if these are correct */
411                 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
412                 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
413
414                 rfp2.lpMediaType = pTypes;
415
416                 res = IFilterMapper2_RegisterFilter(pMapper,
417                                               &CLSID_AVIMIDIRender,
418                                               mocaps.szPname,
419                                               &pMoniker,
420                                               &CLSID_MidiRendererCategory,
421                                               mocaps.szPname,
422                                               &rf2);
423
424                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
425                 /* Native version sets MidiOutId */
426
427                 if (pMoniker)
428                     IMoniker_Release(pMoniker);
429
430                 if (i == iDefaultDevice)
431                 {
432                     FIXME("Default device\n");
433                 }
434
435                 CoTaskMemFree(pTypes);
436             }
437         }
438     }
439
440     if (pMapper)
441         IFilterMapper2_Release(pMapper);
442     return res;
443 }