winmm: Have xyzGetDevCaps return MMSYSERR_BADDEVICEID, not INVALHANDLE.
[wine] / dlls / mmdevapi / main.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 CINTERFACE
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/debug.h"
28
29 #include "initguid.h"
30 #include "ole2.h"
31 #include "mmdeviceapi.h"
32
33 #include "mmdevapi.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
36
37 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
38 {
39     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
40
41     switch (fdwReason)
42     {
43         case DLL_PROCESS_ATTACH:
44             DisableThreadLibraryCalls(hinstDLL);
45             break;
46         case DLL_PROCESS_DETACH:
47             MMDevEnum_Free();
48             break;
49     }
50
51     return TRUE;
52 }
53
54 HRESULT WINAPI DllCanUnloadNow(void)
55 {
56     return S_FALSE;
57 }
58
59 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
60
61 typedef struct {
62     const IClassFactoryVtbl *lpVtbl;
63     REFCLSID rclsid;
64     FnCreateInstance pfnCreateInstance;
65 } IClassFactoryImpl;
66
67 static HRESULT WINAPI
68 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
69 {
70     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
71     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
72     if (ppobj == NULL)
73         return E_POINTER;
74     if (IsEqualIID(riid, &IID_IUnknown) ||
75         IsEqualIID(riid, &IID_IClassFactory))
76     {
77         *ppobj = iface;
78         IUnknown_AddRef(iface);
79         return S_OK;
80     }
81     *ppobj = NULL;
82     return E_NOINTERFACE;
83 }
84
85 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
86 {
87     return 2;
88 }
89
90 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
91 {
92     /* static class, won't be freed */
93     return 1;
94 }
95
96 static HRESULT WINAPI MMCF_CreateInstance(
97     LPCLASSFACTORY iface,
98     LPUNKNOWN pOuter,
99     REFIID riid,
100     LPVOID *ppobj)
101 {
102     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
103     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
104
105     if (pOuter)
106         return CLASS_E_NOAGGREGATION;
107
108     if (ppobj == NULL) {
109         WARN("invalid parameter\n");
110         return E_POINTER;
111     }
112     *ppobj = NULL;
113     return This->pfnCreateInstance(riid, ppobj);
114 }
115
116 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
117 {
118     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
119     FIXME("(%p, %d) stub!\n", This, dolock);
120     return S_OK;
121 }
122
123 static const IClassFactoryVtbl MMCF_Vtbl = {
124     MMCF_QueryInterface,
125     MMCF_AddRef,
126     MMCF_Release,
127     MMCF_CreateInstance,
128     MMCF_LockServer
129 };
130
131 static IClassFactoryImpl MMDEVAPI_CF[] = {
132     { &MMCF_Vtbl, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
133 };
134
135 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
136 {
137     int i = 0;
138     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
139
140     if (ppv == NULL) {
141         WARN("invalid parameter\n");
142         return E_INVALIDARG;
143     }
144
145     *ppv = NULL;
146
147     if (!IsEqualIID(riid, &IID_IClassFactory) &&
148         !IsEqualIID(riid, &IID_IUnknown)) {
149         WARN("no interface for %s\n", debugstr_guid(riid));
150         return E_NOINTERFACE;
151     }
152
153     for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
154     {
155         if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
156             IUnknown_AddRef((IClassFactory*) &MMDEVAPI_CF[i]);
157             *ppv = &MMDEVAPI_CF[i];
158             return S_OK;
159         }
160         i++;
161     }
162
163     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
164          debugstr_guid(riid), ppv);
165     return CLASS_E_CLASSNOTAVAILABLE;
166 }