include: Assorted spelling fixes.
[wine] / dlls / mmdevapi / devenum.c
1 /*
2  * Copyright 2009 Maarten Lankhorst
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define NONAMELESSUNION
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31
32 #include "initguid.h"
33 #include "ole2.h"
34 #include "mmdeviceapi.h"
35 #include "dshow.h"
36 #include "dsound.h"
37 #include "audioclient.h"
38 #include "endpointvolume.h"
39 #include "audiopolicy.h"
40
41 #include "mmdevapi.h"
42 #include "devpkey.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
45
46 static const WCHAR software_mmdevapi[] =
47     { 'S','o','f','t','w','a','r','e','\\',
48       'M','i','c','r','o','s','o','f','t','\\',
49       'W','i','n','d','o','w','s','\\',
50       'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51       'M','M','D','e','v','i','c','e','s','\\',
52       'A','u','d','i','o',0};
53 static const WCHAR reg_render[] =
54     { 'R','e','n','d','e','r',0 };
55 static const WCHAR reg_capture[] =
56     { 'C','a','p','t','u','r','e',0 };
57 static const WCHAR reg_devicestate[] =
58     { 'D','e','v','i','c','e','S','t','a','t','e',0 };
59 static const WCHAR reg_properties[] =
60     { 'P','r','o','p','e','r','t','i','e','s',0 };
61
62 static HKEY key_render;
63 static HKEY key_capture;
64
65 typedef struct MMDevPropStoreImpl
66 {
67     IPropertyStore IPropertyStore_iface;
68     LONG ref;
69     MMDevice *parent;
70     DWORD access;
71 } MMDevPropStore;
72
73 typedef struct MMDevEnumImpl
74 {
75     IMMDeviceEnumerator IMMDeviceEnumerator_iface;
76     LONG ref;
77 } MMDevEnumImpl;
78
79 static MMDevEnumImpl *MMDevEnumerator;
80 static MMDevice **MMDevice_head;
81 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
82 static DWORD MMDevice_count;
83 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
84 static const IMMDeviceCollectionVtbl MMDevColVtbl;
85 static const IMMDeviceVtbl MMDeviceVtbl;
86 static const IPropertyStoreVtbl MMDevPropVtbl;
87 static const IMMEndpointVtbl MMEndpointVtbl;
88
89 static IMMDevice info_device;
90
91 typedef struct MMDevColImpl
92 {
93     IMMDeviceCollection IMMDeviceCollection_iface;
94     LONG ref;
95     EDataFlow flow;
96     DWORD state;
97 } MMDevColImpl;
98
99 typedef struct IPropertyBagImpl {
100     IPropertyBag IPropertyBag_iface;
101     GUID devguid;
102 } IPropertyBagImpl;
103
104 static const IPropertyBagVtbl PB_Vtbl;
105
106 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
107
108 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
109 {
110     return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
111 }
112
113 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
114 {
115     return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
116 }
117
118 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
119 {
120     return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
121 }
122
123 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
124 {
125     return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
126 }
127
128 static const WCHAR propkey_formatW[] = {
129     '{','%','0','8','X','-','%','0','4','X','-',
130     '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
131     '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
132     '%','0','2','X','%','0','2','X','}',',','%','d',0 };
133
134 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
135 {
136     WCHAR buffer[39];
137     LONG ret;
138     HKEY key;
139     StringFromGUID2(guid, buffer, 39);
140     if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE, &key)) != ERROR_SUCCESS)
141     {
142         WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
143         return E_FAIL;
144     }
145     ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE, propkey);
146     RegCloseKey(key);
147     if (ret != ERROR_SUCCESS)
148     {
149         WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
150         return E_FAIL;
151     }
152     return S_OK;
153 }
154
155 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
156 {
157     WCHAR buffer[80];
158     const GUID *id = &key->fmtid;
159     DWORD type, size;
160     HRESULT hr = S_OK;
161     HKEY regkey;
162     LONG ret;
163
164     hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
165     if (FAILED(hr))
166         return hr;
167     wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
168                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
169                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
170     ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
171     if (ret != ERROR_SUCCESS)
172     {
173         WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
174         RegCloseKey(regkey);
175         PropVariantClear(pv);
176         return S_OK;
177     }
178
179     switch (type)
180     {
181         case REG_SZ:
182         {
183             pv->vt = VT_LPWSTR;
184             pv->u.pwszVal = CoTaskMemAlloc(size);
185             if (!pv->u.pwszVal)
186                 hr = E_OUTOFMEMORY;
187             else
188                 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
189             break;
190         }
191         case REG_DWORD:
192         {
193             pv->vt = VT_UI4;
194             RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
195             break;
196         }
197         case REG_BINARY:
198         {
199             pv->vt = VT_BLOB;
200             pv->u.blob.cbSize = size;
201             pv->u.blob.pBlobData = CoTaskMemAlloc(size);
202             if (!pv->u.blob.pBlobData)
203                 hr = E_OUTOFMEMORY;
204             else
205                 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
206             break;
207         }
208         default:
209             ERR("Unknown/unhandled type: %u\n", type);
210             PropVariantClear(pv);
211             break;
212     }
213     RegCloseKey(regkey);
214     return hr;
215 }
216
217 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
218 {
219     WCHAR buffer[80];
220     const GUID *id = &key->fmtid;
221     HRESULT hr;
222     HKEY regkey;
223     LONG ret;
224
225     hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
226     if (FAILED(hr))
227         return hr;
228     wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
229                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
230                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
231     switch (pv->vt)
232     {
233         case VT_UI4:
234         {
235             ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
236             break;
237         }
238         case VT_BLOB:
239         {
240             ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
241             TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
242
243             break;
244         }
245         case VT_LPWSTR:
246         {
247             ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
248             break;
249         }
250         default:
251             ret = 0;
252             FIXME("Unhandled type %u\n", pv->vt);
253             hr = E_INVALIDARG;
254             break;
255     }
256     RegCloseKey(regkey);
257     TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
258     return hr;
259 }
260
261 /* Creates or updates the state of a device
262  * If GUID is null, a random guid will be assigned
263  * and the device will be created
264  */
265 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
266 {
267     HKEY key, root;
268     MMDevice *cur = NULL;
269     WCHAR guidstr[39];
270     DWORD i;
271
272     static const PROPERTYKEY deviceinterface_key = {
273         {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
274     };
275
276     for (i = 0; i < MMDevice_count; ++i)
277     {
278         MMDevice *device = MMDevice_head[i];
279         if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
280             cur = device;
281             break;
282         }
283     }
284
285     if(!cur){
286         /* No device found, allocate new one */
287         cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
288         if (!cur)
289             return NULL;
290
291         cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
292         cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
293
294         InitializeCriticalSection(&cur->crst);
295         cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
296
297         if (!MMDevice_head)
298             MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
299         else
300             MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
301         MMDevice_head[MMDevice_count++] = cur;
302     }else if(cur->ref > 0)
303         WARN("Modifying an MMDevice with postitive reference count!\n");
304
305     HeapFree(GetProcessHeap(), 0, cur->drv_id);
306     cur->drv_id = name;
307
308     cur->flow = flow;
309     cur->state = state;
310     cur->devguid = *id;
311
312     StringFromGUID2(&cur->devguid, guidstr, sizeof(guidstr)/sizeof(*guidstr));
313
314     if (flow == eRender)
315         root = key_render;
316     else
317         root = key_capture;
318
319     if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &key, NULL) == ERROR_SUCCESS)
320     {
321         HKEY keyprop;
322         RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
323         if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &keyprop, NULL))
324         {
325             PROPVARIANT pv;
326
327             pv.vt = VT_LPWSTR;
328             pv.u.pwszVal = name;
329             MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
330             MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
331
332             pv.u.pwszVal = guidstr;
333             MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
334
335             RegCloseKey(keyprop);
336         }
337         RegCloseKey(key);
338     }
339
340     if (setdefault)
341     {
342         if (flow == eRender)
343             MMDevice_def_play = cur;
344         else
345             MMDevice_def_rec = cur;
346     }
347     return cur;
348 }
349
350 static HRESULT load_devices_from_reg(void)
351 {
352     DWORD i = 0;
353     HKEY root, cur;
354     LONG ret;
355     DWORD curflow;
356
357     ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &root, NULL);
358     if (ret == ERROR_SUCCESS)
359         ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_capture, NULL);
360     if (ret == ERROR_SUCCESS)
361         ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_render, NULL);
362     RegCloseKey(root);
363     cur = key_capture;
364     curflow = eCapture;
365     if (ret != ERROR_SUCCESS)
366     {
367         RegCloseKey(key_capture);
368         key_render = key_capture = NULL;
369         WARN("Couldn't create key: %u\n", ret);
370         return E_FAIL;
371     }
372
373     do {
374         WCHAR guidvalue[39];
375         GUID guid;
376         DWORD len;
377         PROPVARIANT pv = { VT_EMPTY };
378
379         len = sizeof(guidvalue)/sizeof(guidvalue[0]);
380         ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
381         if (ret == ERROR_NO_MORE_ITEMS)
382         {
383             if (cur == key_capture)
384             {
385                 cur = key_render;
386                 curflow = eRender;
387                 i = 0;
388                 continue;
389             }
390             break;
391         }
392         if (ret != ERROR_SUCCESS)
393             continue;
394         if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
395             && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
396             && pv.vt == VT_LPWSTR)
397         {
398             DWORD size_bytes = (strlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
399             WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
400             memcpy(name, pv.u.pwszVal, size_bytes);
401             MMDevice_Create(name, &guid, curflow,
402                     DEVICE_STATE_NOTPRESENT, FALSE);
403             CoTaskMemFree(pv.u.pwszVal);
404         }
405     } while (1);
406
407     return S_OK;
408 }
409
410 static HRESULT set_format(MMDevice *dev)
411 {
412     HRESULT hr;
413     IAudioClient *client;
414     WAVEFORMATEX *fmt;
415     PROPVARIANT pv = { VT_EMPTY };
416
417     hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
418     if(FAILED(hr))
419         return hr;
420
421     hr = IAudioClient_GetMixFormat(client, &fmt);
422     if(FAILED(hr)){
423         IAudioClient_Release(client);
424         return hr;
425     }
426
427     IAudioClient_Release(client);
428
429     pv.vt = VT_BLOB;
430     pv.u.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
431     pv.u.blob.pBlobData = (BYTE*)fmt;
432     MMDevice_SetPropValue(&dev->devguid, dev->flow,
433             &PKEY_AudioEngine_DeviceFormat, &pv);
434     MMDevice_SetPropValue(&dev->devguid, dev->flow,
435             &PKEY_AudioEngine_OEMFormat, &pv);
436
437     return S_OK;
438 }
439
440 static HRESULT load_driver_devices(EDataFlow flow)
441 {
442     WCHAR **ids;
443     GUID *guids;
444     UINT num, def, i;
445     HRESULT hr;
446
447     if(!drvs.pGetEndpointIDs)
448         return S_OK;
449
450     hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
451     if(FAILED(hr))
452         return hr;
453
454     for(i = 0; i < num; ++i){
455         MMDevice *dev;
456         dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
457                 def == i);
458         set_format(dev);
459     }
460
461     HeapFree(GetProcessHeap(), 0, guids);
462     HeapFree(GetProcessHeap(), 0, ids);
463
464     return S_OK;
465 }
466
467 static void MMDevice_Destroy(MMDevice *This)
468 {
469     DWORD i;
470     TRACE("Freeing %s\n", debugstr_w(This->drv_id));
471     /* Since this function is called at destruction time, reordering of the list is unimportant */
472     for (i = 0; i < MMDevice_count; ++i)
473     {
474         if (MMDevice_head[i] == This)
475         {
476             MMDevice_head[i] = MMDevice_head[--MMDevice_count];
477             break;
478         }
479     }
480     This->crst.DebugInfo->Spare[0] = 0;
481     DeleteCriticalSection(&This->crst);
482     HeapFree(GetProcessHeap(), 0, This->drv_id);
483     HeapFree(GetProcessHeap(), 0, This);
484 }
485
486 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
487 {
488     return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
489 }
490
491 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
492 {
493     MMDevice *This = impl_from_IMMDevice(iface);
494     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
495
496     if (!ppv)
497         return E_POINTER;
498     *ppv = NULL;
499     if (IsEqualIID(riid, &IID_IUnknown)
500         || IsEqualIID(riid, &IID_IMMDevice))
501         *ppv = This;
502     else if (IsEqualIID(riid, &IID_IMMEndpoint))
503         *ppv = &This->IMMEndpoint_iface;
504     if (*ppv)
505     {
506         IUnknown_AddRef((IUnknown*)*ppv);
507         return S_OK;
508     }
509     WARN("Unknown interface %s\n", debugstr_guid(riid));
510     return E_NOINTERFACE;
511 }
512
513 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
514 {
515     MMDevice *This = impl_from_IMMDevice(iface);
516     LONG ref;
517
518     ref = InterlockedIncrement(&This->ref);
519     TRACE("Refcount now %i\n", ref);
520     return ref;
521 }
522
523 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
524 {
525     MMDevice *This = impl_from_IMMDevice(iface);
526     LONG ref;
527
528     ref = InterlockedDecrement(&This->ref);
529     TRACE("Refcount now %i\n", ref);
530     return ref;
531 }
532
533 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
534 {
535     HRESULT hr = E_NOINTERFACE;
536     MMDevice *This = impl_from_IMMDevice(iface);
537
538     TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
539
540     if (!ppv)
541         return E_POINTER;
542
543     if (IsEqualIID(riid, &IID_IAudioClient)){
544         hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
545     }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
546         hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
547     else if (IsEqualIID(riid, &IID_IAudioSessionManager)
548              || IsEqualIID(riid, &IID_IAudioSessionManager2))
549     {
550         hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
551     }
552     else if (IsEqualIID(riid, &IID_IBaseFilter))
553     {
554         if (This->flow == eRender)
555             hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
556         else
557             ERR("Not supported for recording?\n");
558         if (SUCCEEDED(hr))
559         {
560             IPersistPropertyBag *ppb;
561             hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
562             if (SUCCEEDED(hr))
563             {
564                 /* ::Load cannot assume the interface stays alive after the function returns,
565                  * so just create the interface on the stack, saves a lot of complicated code */
566                 IPropertyBagImpl bag = { { &PB_Vtbl }, This->devguid };
567                 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
568                 IPersistPropertyBag_Release(ppb);
569                 if (FAILED(hr))
570                     IBaseFilter_Release((IBaseFilter*)*ppv);
571             }
572             else
573             {
574                 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
575                 hr = S_OK;
576             }
577         }
578     }
579     else if (IsEqualIID(riid, &IID_IDeviceTopology))
580     {
581         FIXME("IID_IDeviceTopology unsupported\n");
582     }
583     else if (IsEqualIID(riid, &IID_IDirectSound)
584              || IsEqualIID(riid, &IID_IDirectSound8))
585     {
586         if (This->flow == eRender)
587             hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
588         if (SUCCEEDED(hr))
589         {
590             hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
591             if (FAILED(hr))
592                 IDirectSound_Release((IDirectSound*)*ppv);
593         }
594     }
595     else if (IsEqualIID(riid, &IID_IDirectSoundCapture)
596              || IsEqualIID(riid, &IID_IDirectSoundCapture8))
597     {
598         if (This->flow == eCapture)
599             hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
600         if (SUCCEEDED(hr))
601         {
602             hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
603             if (FAILED(hr))
604                 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
605         }
606     }
607     else
608         ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
609
610     if (FAILED(hr))
611         *ppv = NULL;
612
613     TRACE("Returning %08x\n", hr);
614     return hr;
615 }
616
617 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
618 {
619     MMDevice *This = impl_from_IMMDevice(iface);
620     TRACE("(%p)->(%x,%p)\n", This, access, ppv);
621
622     if (!ppv)
623         return E_POINTER;
624     return MMDevPropStore_Create(This, access, ppv);
625 }
626
627 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
628 {
629     MMDevice *This = impl_from_IMMDevice(iface);
630     WCHAR *str;
631     GUID *id = &This->devguid;
632     static const WCHAR formatW[] = { '{','0','.','0','.','%','u','.','0','0','0','0','0','0','0','0','}','.',
633                                      '{','%','0','8','X','-','%','0','4','X','-',
634                                      '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
635                                      '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
636                                      '%','0','2','X','%','0','2','X','}',0 };
637
638     TRACE("(%p)->(%p)\n", This, itemid);
639     if (!itemid)
640         return E_POINTER;
641     *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
642     if (!str)
643         return E_OUTOFMEMORY;
644     wsprintfW( str, formatW, This->flow, id->Data1, id->Data2, id->Data3,
645                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
646                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
647     TRACE("returning %s\n", wine_dbgstr_w(str));
648     return S_OK;
649 }
650
651 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
652 {
653     MMDevice *This = impl_from_IMMDevice(iface);
654     TRACE("(%p)->(%p)\n", iface, state);
655
656     if (!state)
657         return E_POINTER;
658     *state = This->state;
659     return S_OK;
660 }
661
662 static const IMMDeviceVtbl MMDeviceVtbl =
663 {
664     MMDevice_QueryInterface,
665     MMDevice_AddRef,
666     MMDevice_Release,
667     MMDevice_Activate,
668     MMDevice_OpenPropertyStore,
669     MMDevice_GetId,
670     MMDevice_GetState
671 };
672
673 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
674 {
675     return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
676 }
677
678 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
679 {
680     MMDevice *This = impl_from_IMMEndpoint(iface);
681     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
682     return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
683 }
684
685 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
686 {
687     MMDevice *This = impl_from_IMMEndpoint(iface);
688     TRACE("(%p)\n", This);
689     return IMMDevice_AddRef(&This->IMMDevice_iface);
690 }
691
692 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
693 {
694     MMDevice *This = impl_from_IMMEndpoint(iface);
695     TRACE("(%p)\n", This);
696     return IMMDevice_Release(&This->IMMDevice_iface);
697 }
698
699 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
700 {
701     MMDevice *This = impl_from_IMMEndpoint(iface);
702     TRACE("(%p)->(%p)\n", This, flow);
703     if (!flow)
704         return E_POINTER;
705     *flow = This->flow;
706     return S_OK;
707 }
708
709 static const IMMEndpointVtbl MMEndpointVtbl =
710 {
711     MMEndpoint_QueryInterface,
712     MMEndpoint_AddRef,
713     MMEndpoint_Release,
714     MMEndpoint_GetDataFlow
715 };
716
717 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
718 {
719     MMDevColImpl *This;
720
721     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
722     *ppv = NULL;
723     if (!This)
724         return E_OUTOFMEMORY;
725     This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
726     This->ref = 1;
727     This->flow = flow;
728     This->state = state;
729     *ppv = &This->IMMDeviceCollection_iface;
730     return S_OK;
731 }
732
733 static void MMDevCol_Destroy(MMDevColImpl *This)
734 {
735     HeapFree(GetProcessHeap(), 0, This);
736 }
737
738 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
739 {
740     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
741     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
742
743     if (!ppv)
744         return E_POINTER;
745     if (IsEqualIID(riid, &IID_IUnknown)
746         || IsEqualIID(riid, &IID_IMMDeviceCollection))
747         *ppv = This;
748     else
749         *ppv = NULL;
750     if (!*ppv)
751         return E_NOINTERFACE;
752     IUnknown_AddRef((IUnknown*)*ppv);
753     return S_OK;
754 }
755
756 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
757 {
758     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
759     LONG ref = InterlockedIncrement(&This->ref);
760     TRACE("Refcount now %i\n", ref);
761     return ref;
762 }
763
764 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
765 {
766     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
767     LONG ref = InterlockedDecrement(&This->ref);
768     TRACE("Refcount now %i\n", ref);
769     if (!ref)
770         MMDevCol_Destroy(This);
771     return ref;
772 }
773
774 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
775 {
776     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
777     DWORD i;
778
779     TRACE("(%p)->(%p)\n", This, numdevs);
780     if (!numdevs)
781         return E_POINTER;
782
783     *numdevs = 0;
784     for (i = 0; i < MMDevice_count; ++i)
785     {
786         MMDevice *cur = MMDevice_head[i];
787         if ((cur->flow == This->flow || This->flow == eAll)
788             && (cur->state & This->state))
789             ++(*numdevs);
790     }
791     return S_OK;
792 }
793
794 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
795 {
796     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
797     DWORD i = 0, j = 0;
798
799     TRACE("(%p)->(%u, %p)\n", This, n, dev);
800     if (!dev)
801         return E_POINTER;
802
803     for (j = 0; j < MMDevice_count; ++j)
804     {
805         MMDevice *cur = MMDevice_head[j];
806         if ((cur->flow == This->flow || This->flow == eAll)
807             && (cur->state & This->state)
808             && i++ == n)
809         {
810             *dev = &cur->IMMDevice_iface;
811             IMMDevice_AddRef(*dev);
812             return S_OK;
813         }
814     }
815     WARN("Could not obtain item %u\n", n);
816     *dev = NULL;
817     return E_INVALIDARG;
818 }
819
820 static const IMMDeviceCollectionVtbl MMDevColVtbl =
821 {
822     MMDevCol_QueryInterface,
823     MMDevCol_AddRef,
824     MMDevCol_Release,
825     MMDevCol_GetCount,
826     MMDevCol_Item
827 };
828
829 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
830 {
831     MMDevEnumImpl *This = MMDevEnumerator;
832
833     if (!This)
834     {
835         This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
836         *ppv = NULL;
837         if (!This)
838             return E_OUTOFMEMORY;
839         This->ref = 1;
840         This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
841         MMDevEnumerator = This;
842
843         load_devices_from_reg();
844         load_driver_devices(eRender);
845         load_driver_devices(eCapture);
846     }
847     return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
848 }
849
850 void MMDevEnum_Free(void)
851 {
852     while (MMDevice_count)
853         MMDevice_Destroy(MMDevice_head[0]);
854     RegCloseKey(key_render);
855     RegCloseKey(key_capture);
856     key_render = key_capture = NULL;
857     HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
858     MMDevEnumerator = NULL;
859 }
860
861 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
862 {
863     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
864     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
865
866     if (!ppv)
867         return E_POINTER;
868     if (IsEqualIID(riid, &IID_IUnknown)
869         || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
870         *ppv = This;
871     else
872         *ppv = NULL;
873     if (!*ppv)
874         return E_NOINTERFACE;
875     IUnknown_AddRef((IUnknown*)*ppv);
876     return S_OK;
877 }
878
879 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
880 {
881     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
882     LONG ref = InterlockedIncrement(&This->ref);
883     TRACE("Refcount now %i\n", ref);
884     return ref;
885 }
886
887 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
888 {
889     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
890     LONG ref = InterlockedDecrement(&This->ref);
891     if (!ref)
892         MMDevEnum_Free();
893     TRACE("Refcount now %i\n", ref);
894     return ref;
895 }
896
897 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
898 {
899     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
900     TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
901     if (!devices)
902         return E_POINTER;
903     *devices = NULL;
904     if (flow >= EDataFlow_enum_count)
905         return E_INVALIDARG;
906     if (mask & ~DEVICE_STATEMASK_ALL)
907         return E_INVALIDARG;
908     return MMDevCol_Create(devices, flow, mask);
909 }
910
911 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
912 {
913     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
914     WCHAR reg_key[256];
915     HKEY key;
916     HRESULT hr;
917
918     static const WCHAR slashW[] = {'\\',0};
919     static const WCHAR reg_out_nameW[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
920     static const WCHAR reg_vout_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','O','u','t','p','u','t',0};
921     static const WCHAR reg_in_nameW[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
922     static const WCHAR reg_vin_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
923
924     TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
925
926     if (!device)
927         return E_POINTER;
928
929     if((flow != eRender && flow != eCapture) ||
930             (role != eConsole && role != eMultimedia && role != eCommunications)){
931         WARN("Unknown flow (%u) or role (%u)\n", flow, role);
932         return E_INVALIDARG;
933     }
934
935     *device = NULL;
936
937     if(!drvs.module_name[0])
938         return E_NOTFOUND;
939
940     lstrcpyW(reg_key, drv_keyW);
941     lstrcatW(reg_key, slashW);
942     lstrcatW(reg_key, drvs.module_name);
943
944     if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
945         const WCHAR *reg_x_name, *reg_vx_name;
946         WCHAR def_id[256];
947         DWORD size = sizeof(def_id), state;
948
949         if(flow == eRender){
950             reg_x_name = reg_out_nameW;
951             reg_vx_name = reg_vout_nameW;
952         }else{
953             reg_x_name = reg_in_nameW;
954             reg_vx_name = reg_vin_nameW;
955         }
956
957         if(role == eCommunications &&
958                 RegQueryValueExW(key, reg_vx_name, 0, NULL,
959                     (BYTE*)def_id, &size) == ERROR_SUCCESS){
960             hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
961             if(SUCCEEDED(hr)){
962                 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
963                         state == DEVICE_STATE_ACTIVE){
964                     RegCloseKey(key);
965                     return S_OK;
966                 }
967             }
968
969             TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
970         }
971
972         if(RegQueryValueExW(key, reg_x_name, 0, NULL,
973                     (BYTE*)def_id, &size) == ERROR_SUCCESS){
974             hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
975             if(SUCCEEDED(hr)){
976                 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
977                         state == DEVICE_STATE_ACTIVE){
978                     RegCloseKey(key);
979                     return S_OK;
980                 }
981             }
982
983             TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
984         }
985
986         RegCloseKey(key);
987     }
988
989     if (flow == eRender)
990         *device = &MMDevice_def_play->IMMDevice_iface;
991     else
992         *device = &MMDevice_def_rec->IMMDevice_iface;
993
994     if (!*device)
995         return E_NOTFOUND;
996     IMMDevice_AddRef(*device);
997     return S_OK;
998 }
999
1000 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1001 {
1002     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1003     DWORD i=0;
1004     IMMDevice *dev = NULL;
1005
1006     static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
1007         'i','n','f','o',' ','d','e','v','i','c','e',0};
1008
1009     TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1010
1011     if(!name || !device)
1012         return E_POINTER;
1013
1014     if(!lstrcmpW(name, wine_info_deviceW)){
1015         *device = &info_device;
1016         return S_OK;
1017     }
1018
1019     for (i = 0; i < MMDevice_count; ++i)
1020     {
1021         WCHAR *str;
1022         dev = &MMDevice_head[i]->IMMDevice_iface;
1023         IMMDevice_GetId(dev, &str);
1024
1025         if (str && !lstrcmpW(str, name))
1026         {
1027             CoTaskMemFree(str);
1028             IMMDevice_AddRef(dev);
1029             *device = dev;
1030             return S_OK;
1031         }
1032         CoTaskMemFree(str);
1033     }
1034     TRACE("Could not find device %s\n", debugstr_w(name));
1035     return E_INVALIDARG;
1036 }
1037
1038 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1039 {
1040     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1041     TRACE("(%p)->(%p)\n", This, client);
1042     FIXME("stub\n");
1043     return S_OK;
1044 }
1045
1046 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1047 {
1048     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1049     TRACE("(%p)->(%p)\n", This, client);
1050     FIXME("stub\n");
1051     return S_OK;
1052 }
1053
1054 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1055 {
1056     MMDevEnum_QueryInterface,
1057     MMDevEnum_AddRef,
1058     MMDevEnum_Release,
1059     MMDevEnum_EnumAudioEndpoints,
1060     MMDevEnum_GetDefaultAudioEndpoint,
1061     MMDevEnum_GetDevice,
1062     MMDevEnum_RegisterEndpointNotificationCallback,
1063     MMDevEnum_UnregisterEndpointNotificationCallback
1064 };
1065
1066 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1067 {
1068     MMDevPropStore *This;
1069     if (access != STGM_READ
1070         && access != STGM_WRITE
1071         && access != STGM_READWRITE)
1072     {
1073         WARN("Invalid access %08x\n", access);
1074         return E_INVALIDARG;
1075     }
1076     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1077     *ppv = &This->IPropertyStore_iface;
1078     if (!This)
1079         return E_OUTOFMEMORY;
1080     This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1081     This->ref = 1;
1082     This->parent = parent;
1083     This->access = access;
1084     return S_OK;
1085 }
1086
1087 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1088 {
1089     HeapFree(GetProcessHeap(), 0, This);
1090 }
1091
1092 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1093 {
1094     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1095     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1096
1097     if (!ppv)
1098         return E_POINTER;
1099     if (IsEqualIID(riid, &IID_IUnknown)
1100         || IsEqualIID(riid, &IID_IPropertyStore))
1101         *ppv = This;
1102     else
1103         *ppv = NULL;
1104     if (!*ppv)
1105         return E_NOINTERFACE;
1106     IUnknown_AddRef((IUnknown*)*ppv);
1107     return S_OK;
1108 }
1109
1110 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1111 {
1112     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1113     LONG ref = InterlockedIncrement(&This->ref);
1114     TRACE("Refcount now %i\n", ref);
1115     return ref;
1116 }
1117
1118 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1119 {
1120     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1121     LONG ref = InterlockedDecrement(&This->ref);
1122     TRACE("Refcount now %i\n", ref);
1123     if (!ref)
1124         MMDevPropStore_Destroy(This);
1125     return ref;
1126 }
1127
1128 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1129 {
1130     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1131     WCHAR buffer[50];
1132     DWORD i = 0;
1133     HKEY propkey;
1134     HRESULT hr;
1135
1136     TRACE("(%p)->(%p)\n", iface, nprops);
1137     if (!nprops)
1138         return E_POINTER;
1139     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1140     if (FAILED(hr))
1141         return hr;
1142     *nprops = 0;
1143     do {
1144         DWORD len = sizeof(buffer)/sizeof(*buffer);
1145         if (RegEnumKeyExW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1146             break;
1147         i++;
1148     } while (0);
1149     RegCloseKey(propkey);
1150     TRACE("Returning %i\n", i);
1151     *nprops = i;
1152     return S_OK;
1153 }
1154
1155 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1156 {
1157     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1158     WCHAR buffer[50];
1159     DWORD len = sizeof(buffer)/sizeof(*buffer);
1160     HRESULT hr;
1161     HKEY propkey;
1162
1163     TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1164     if (!key)
1165         return E_POINTER;
1166
1167     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1168     if (FAILED(hr))
1169         return hr;
1170
1171     if (RegEnumKeyExW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1172         || len <= 40)
1173     {
1174         WARN("GetAt %u failed\n", prop);
1175         return E_INVALIDARG;
1176     }
1177     RegCloseKey(propkey);
1178     buffer[39] = 0;
1179     CLSIDFromString(buffer, &key->fmtid);
1180     key->pid = atoiW(&buffer[40]);
1181     return S_OK;
1182 }
1183
1184 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1185 {
1186     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1187     TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1188
1189     if (!key || !pv)
1190         return E_POINTER;
1191     if (This->access != STGM_READ
1192         && This->access != STGM_READWRITE)
1193         return STG_E_ACCESSDENIED;
1194
1195     /* Special case */
1196     if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1197     {
1198         pv->vt = VT_LPWSTR;
1199         pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1200         if (!pv->u.pwszVal)
1201             return E_OUTOFMEMORY;
1202         StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1203         return S_OK;
1204     }
1205
1206     return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1207 }
1208
1209 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1210 {
1211     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1212     TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1213
1214     if (!key || !pv)
1215         return E_POINTER;
1216
1217     if (This->access != STGM_WRITE
1218         && This->access != STGM_READWRITE)
1219         return STG_E_ACCESSDENIED;
1220     return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1221 }
1222
1223 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1224 {
1225     FIXME("stub\n");
1226     return E_NOTIMPL;
1227 }
1228
1229 static const IPropertyStoreVtbl MMDevPropVtbl =
1230 {
1231     MMDevPropStore_QueryInterface,
1232     MMDevPropStore_AddRef,
1233     MMDevPropStore_Release,
1234     MMDevPropStore_GetCount,
1235     MMDevPropStore_GetAt,
1236     MMDevPropStore_GetValue,
1237     MMDevPropStore_SetValue,
1238     MMDevPropStore_Commit
1239 };
1240
1241
1242 /* Property bag for IBaseFilter activation */
1243 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1244 {
1245     ERR("Should not be called\n");
1246     *ppv = NULL;
1247     return E_NOINTERFACE;
1248 }
1249
1250 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1251 {
1252     ERR("Should not be called\n");
1253     return 2;
1254 }
1255
1256 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1257 {
1258     ERR("Should not be called\n");
1259     return 1;
1260 }
1261
1262 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1263 {
1264     static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1265     IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1266     TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1267     if (!lstrcmpW(name, dsguid))
1268     {
1269         WCHAR guidstr[39];
1270         StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1271         var->n1.n2.vt = VT_BSTR;
1272         var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1273         return S_OK;
1274     }
1275     ERR("Unknown property '%s' queried\n", debugstr_w(name));
1276     return E_FAIL;
1277 }
1278
1279 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1280 {
1281     ERR("Should not be called\n");
1282     return E_FAIL;
1283 }
1284
1285 static const IPropertyBagVtbl PB_Vtbl =
1286 {
1287     PB_QueryInterface,
1288     PB_AddRef,
1289     PB_Release,
1290     PB_Read,
1291     PB_Write
1292 };
1293
1294 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1295 {
1296     return 2;
1297 }
1298
1299 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1300 {
1301     return 1;
1302 }
1303
1304 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1305         REFPROPERTYKEY key, PROPVARIANT *pv)
1306 {
1307     TRACE("(static)->(\"%s,%u\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1308
1309     if (!key || !pv)
1310         return E_POINTER;
1311
1312     if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1313     {
1314         INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1315         pv->vt = VT_LPWSTR;
1316         pv->u.pwszVal = CoTaskMemAlloc(size);
1317         if (!pv->u.pwszVal)
1318             return E_OUTOFMEMORY;
1319         memcpy(pv->u.pwszVal, drvs.module_name, size);
1320         return S_OK;
1321     }
1322
1323     return E_INVALIDARG;
1324 }
1325
1326 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1327 {
1328     NULL,
1329     info_device_ps_AddRef,
1330     info_device_ps_Release,
1331     NULL,
1332     NULL,
1333     info_device_ps_GetValue,
1334     NULL,
1335     NULL
1336 };
1337
1338 static IPropertyStore info_device_ps = {
1339     &info_device_ps_Vtbl
1340 };
1341
1342 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1343 {
1344     return 2;
1345 }
1346
1347 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1348 {
1349     return 1;
1350 }
1351
1352 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1353         DWORD access, IPropertyStore **ppv)
1354 {
1355     TRACE("(static)->(%x, %p)\n", access, ppv);
1356     *ppv = &info_device_ps;
1357     return S_OK;
1358 }
1359
1360 static const IMMDeviceVtbl info_device_Vtbl =
1361 {
1362     NULL,
1363     info_device_AddRef,
1364     info_device_Release,
1365     NULL,
1366     info_device_OpenPropertyStore,
1367     NULL,
1368     NULL
1369 };
1370
1371 static IMMDevice info_device = {
1372     &info_device_Vtbl
1373 };