shdocvw/tests: Make sure to use return value (LLVM/Clang).
[wine] / dlls / msacm32 / internal.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 1998  Patrik Stridvall
7  *                1999  Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "winreg.h"
33 #include "mmsystem.h"
34 #include "mmreg.h"
35 #include "msacm.h"
36 #include "msacmdrv.h"
37 #include "wineacm.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
42
43 /**********************************************************************/
44
45 HANDLE MSACM_hHeap = NULL;
46 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
47 static PWINE_ACMDRIVERID MSACM_pLastACMDriverID;
48
49 static DWORD MSACM_suspendBroadcastCount = 0;
50 static BOOL MSACM_pendingBroadcast = FALSE;
51 static PWINE_ACMNOTIFYWND MSACM_pFirstACMNotifyWnd = NULL;
52 static PWINE_ACMNOTIFYWND MSACM_pLastACMNotifyWnd = NULL;
53
54 static void MSACM_ReorderDriversByPriority(void);
55
56 /***********************************************************************
57  *           MSACM_RegisterDriverFromRegistry()
58  */
59 PWINE_ACMDRIVERID MSACM_RegisterDriverFromRegistry(LPCWSTR pszRegEntry)
60 {
61     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
62     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
63                                    'M','i','c','r','o','s','o','f','t','\\',
64                                    'W','i','n','d','o','w','s',' ','N','T','\\',
65                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
66                                    'D','r','i','v','e','r','s','3','2','\0'};
67     WCHAR buf[2048];
68     DWORD bufLen, lRet;
69     HKEY hKey;
70     PWINE_ACMDRIVERID padid = NULL;
71     
72     /* The requested registry entry must have the format msacm.XXXXX in order to
73        be recognized in any future sessions of msacm
74      */
75     if (0 == strncmpiW(pszRegEntry, msacmW, sizeof(msacmW)/sizeof(WCHAR))) {
76         lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
77         if (lRet != ERROR_SUCCESS) {
78             WARN("unable to open registry key - 0x%08x\n", lRet);
79         } else {
80             bufLen = sizeof(buf);
81             lRet = RegQueryValueExW(hKey, pszRegEntry, NULL, NULL, (LPBYTE)buf, &bufLen);
82             if (lRet != ERROR_SUCCESS) {
83                 WARN("unable to query requested subkey %s - 0x%08x\n", debugstr_w(pszRegEntry), lRet);
84             } else {
85                 MSACM_RegisterDriver(pszRegEntry, buf, 0);
86             }
87             RegCloseKey( hKey );
88         }
89     }
90     return padid;
91 }
92
93 #if 0
94 /***********************************************************************
95  *           MSACM_DumpCache
96  */
97 static  void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
98 {
99     unsigned    i;
100
101     TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
102           padid->cFilterTags, padid->cFormatTags, padid->fdwSupport);
103     for (i = 0; i < padid->cache->cFormatTags; i++) {
104         TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
105               padid->aFormatTag[i].dwFormatTag, padid->aFormatTag[i].cbwfx);
106     }
107 }
108 #endif
109
110 /***********************************************************************
111  *           MSACM_FindFormatTagInCache                 [internal]
112  *
113  *      Returns TRUE is the format tag fmtTag is present in the cache.
114  *      If so, idx is set to its index.
115  */
116 BOOL MSACM_FindFormatTagInCache(const WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
117 {
118     unsigned    i;
119
120     for (i = 0; i < padid->cFormatTags; i++) {
121         if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
122             if (idx) *idx = i;
123             return TRUE;
124         }
125     }
126     return FALSE;
127 }
128
129 /***********************************************************************
130  *           MSACM_FillCache
131  */
132 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
133 {
134     HACMDRIVER                  had = 0;
135     unsigned int                        ntag;
136     ACMDRIVERDETAILSW           add;
137     ACMFORMATTAGDETAILSW        aftd;
138
139     if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
140         return FALSE;
141
142     padid->aFormatTag = NULL;
143     add.cbStruct = sizeof(add);
144     if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add,  0))
145         goto errCleanUp;
146
147     if (add.cFormatTags > 0) {
148         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY,
149                                       add.cFormatTags * sizeof(padid->aFormatTag[0]));
150         if (!padid->aFormatTag) goto errCleanUp;
151     }
152
153     padid->cFormatTags = add.cFormatTags;
154     padid->cFilterTags = add.cFilterTags;
155     padid->fdwSupport  = add.fdwSupport;
156
157     aftd.cbStruct = sizeof(aftd);
158
159     for (ntag = 0; ntag < add.cFormatTags; ntag++) {
160         aftd.dwFormatTagIndex = ntag;
161         if (MSACM_Message(had, ACMDM_FORMATTAG_DETAILS, (LPARAM)&aftd, ACM_FORMATTAGDETAILSF_INDEX)) {
162             TRACE("IIOs (%s)\n", debugstr_w(padid->pszDriverAlias));
163             goto errCleanUp;
164         }
165         padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
166         padid->aFormatTag[ntag].cbwfx = aftd.cbFormatSize;
167     }
168
169     acmDriverClose(had, 0);
170
171     return TRUE;
172
173 errCleanUp:
174     if (had) acmDriverClose(had, 0);
175     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
176     padid->aFormatTag = NULL;
177     return FALSE;
178 }
179
180 /***********************************************************************
181  *           MSACM_GetRegistryKey
182  */
183 static  LPWSTR  MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
184 {
185     static const WCHAR  baseKey[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186                                      'A','u','d','i','o','C','o','m','p','r','e','s','s','i','o','n','M','a','n','a','g','e','r','\\',
187                                      'D','r','i','v','e','r','C','a','c','h','e','\\','\0'};
188     LPWSTR      ret;
189     int         len;
190
191     if (!padid->pszDriverAlias) {
192         ERR("No alias needed for registry entry\n");
193         return NULL;
194     }
195     len = strlenW(baseKey);
196     ret = HeapAlloc(MSACM_hHeap, 0, (len + strlenW(padid->pszDriverAlias) + 1) * sizeof(WCHAR));
197     if (!ret) return NULL;
198
199     strcpyW(ret, baseKey);
200     strcpyW(ret + len, padid->pszDriverAlias);
201     CharLowerW(ret + len);
202     return ret;
203 }
204
205 /***********************************************************************
206  *           MSACM_ReadCache
207  */
208 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
209 {
210     LPWSTR      key = MSACM_GetRegistryKey(padid);
211     HKEY        hKey;
212     DWORD       type, size;
213
214     if (!key) return FALSE;
215
216     padid->aFormatTag = NULL;
217
218     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
219         goto errCleanUp;
220
221     size = sizeof(padid->cFormatTags);
222     if (RegQueryValueExA(hKey, "cFormatTags", 0, &type, (void*)&padid->cFormatTags, &size))
223         goto errCleanUp;
224     size = sizeof(padid->cFilterTags);
225     if (RegQueryValueExA(hKey, "cFilterTags", 0, &type, (void*)&padid->cFilterTags, &size))
226         goto errCleanUp;
227     size = sizeof(padid->fdwSupport);
228     if (RegQueryValueExA(hKey, "fdwSupport", 0, &type, (void*)&padid->fdwSupport, &size))
229         goto errCleanUp;
230
231     if (padid->cFormatTags > 0) {
232         size = padid->cFormatTags * sizeof(padid->aFormatTag[0]);
233         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY, size);
234         if (!padid->aFormatTag) goto errCleanUp;
235         if (RegQueryValueExA(hKey, "aFormatTagCache", 0, &type, (void*)padid->aFormatTag, &size))
236             goto errCleanUp;
237     }
238     HeapFree(MSACM_hHeap, 0, key);
239     return TRUE;
240
241  errCleanUp:
242     HeapFree(MSACM_hHeap, 0, key);
243     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
244     padid->aFormatTag = NULL;
245     RegCloseKey(hKey);
246     return FALSE;
247 }
248
249 /***********************************************************************
250  *           MSACM_WriteCache
251  */
252 static  BOOL MSACM_WriteCache(const WINE_ACMDRIVERID *padid)
253 {
254     LPWSTR      key = MSACM_GetRegistryKey(padid);
255     HKEY        hKey;
256
257     if (!key) return FALSE;
258
259     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
260         goto errCleanUp;
261
262     if (RegSetValueExA(hKey, "cFormatTags", 0, REG_DWORD, (const void*)&padid->cFormatTags, sizeof(DWORD)))
263         goto errCleanUp;
264     if (RegSetValueExA(hKey, "cFilterTags", 0, REG_DWORD, (const void*)&padid->cFilterTags, sizeof(DWORD)))
265         goto errCleanUp;
266     if (RegSetValueExA(hKey, "fdwSupport", 0, REG_DWORD, (const void*)&padid->fdwSupport, sizeof(DWORD)))
267         goto errCleanUp;
268     if (RegSetValueExA(hKey, "aFormatTagCache", 0, REG_BINARY,
269                        (void*)padid->aFormatTag,
270                        padid->cFormatTags * sizeof(padid->aFormatTag[0])))
271         goto errCleanUp;
272     HeapFree(MSACM_hHeap, 0, key);
273     return TRUE;
274
275  errCleanUp:
276     HeapFree(MSACM_hHeap, 0, key);
277     return FALSE;
278 }
279
280 /***********************************************************************
281  *           MSACM_RegisterDriver()
282  */
283 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPCWSTR pszDriverAlias, LPCWSTR pszFileName,
284                                        PWINE_ACMLOCALDRIVER pLocalDriver)
285 {
286     PWINE_ACMDRIVERID   padid;
287
288     TRACE("(%s, %s, %p)\n", 
289           debugstr_w(pszDriverAlias), debugstr_w(pszFileName), pLocalDriver);
290
291     padid = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMDRIVERID));
292     if (!padid)
293         return NULL;
294     padid->obj.dwType = WINE_ACMOBJ_DRIVERID;
295     padid->obj.pACMDriverID = padid;
296     padid->pszDriverAlias = NULL;
297     if (pszDriverAlias)
298     {
299         padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszDriverAlias)+1) * sizeof(WCHAR) );
300         if (!padid->pszDriverAlias) {
301             HeapFree(MSACM_hHeap, 0, padid);
302             return NULL;
303         }
304         strcpyW( padid->pszDriverAlias, pszDriverAlias );
305     }
306     padid->pszFileName = NULL;
307     if (pszFileName)
308     {
309         padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszFileName)+1) * sizeof(WCHAR) );
310         if (!padid->pszFileName) {
311             HeapFree(MSACM_hHeap, 0, padid->pszDriverAlias);
312             HeapFree(MSACM_hHeap, 0, padid);
313             return NULL;
314         }
315         strcpyW( padid->pszFileName, pszFileName );
316     }
317     padid->pLocalDriver = pLocalDriver;
318
319     padid->pACMDriverList = NULL;
320     
321     if (pLocalDriver) {
322         padid->pPrevACMDriverID = NULL;
323         padid->pNextACMDriverID = MSACM_pFirstACMDriverID;
324         if (MSACM_pFirstACMDriverID)
325             MSACM_pFirstACMDriverID->pPrevACMDriverID = padid;
326         MSACM_pFirstACMDriverID = padid;
327         if (!MSACM_pLastACMDriverID)
328             MSACM_pLastACMDriverID = padid;
329     } else {
330         padid->pNextACMDriverID = NULL;
331         padid->pPrevACMDriverID = MSACM_pLastACMDriverID;
332         if (MSACM_pLastACMDriverID)
333             MSACM_pLastACMDriverID->pNextACMDriverID = padid;
334         MSACM_pLastACMDriverID = padid;
335         if (!MSACM_pFirstACMDriverID)
336             MSACM_pFirstACMDriverID = padid;
337     }
338     /* disable the driver if we cannot load the cache */
339     if ((!padid->pszDriverAlias || !MSACM_ReadCache(padid)) && !MSACM_FillCache(padid)) {
340         WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName));
341         MSACM_UnregisterDriver(padid);
342         return NULL;
343     }
344
345     if (pLocalDriver) padid->fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_LOCAL;
346     return padid;
347 }
348
349 /***********************************************************************
350  *           MSACM_RegisterAllDrivers()
351  */
352 void MSACM_RegisterAllDrivers(void)
353 {
354     static const WCHAR msacm32[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
355     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
356     static const WCHAR drv32[] = {'d','r','i','v','e','r','s','3','2','\0'};
357     static const WCHAR sys[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
358     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
359                                    'M','i','c','r','o','s','o','f','t','\\',
360                                    'W','i','n','d','o','w','s',' ','N','T','\\',
361                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
362                                    'D','r','i','v','e','r','s','3','2','\0'};
363     DWORD i, cnt = 0, bufLen, lRet;
364     WCHAR buf[2048], *name, *s;
365     FILETIME lastWrite;
366     HKEY hKey;
367
368     /* FIXME: What if the user edits system.ini while the program is running?
369      * Does Windows handle that?  */
370     if (MSACM_pFirstACMDriverID) return;
371
372     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
373     if (lRet == ERROR_SUCCESS) {
374         RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
375         for (i = 0; i < cnt; i++) {
376             bufLen = sizeof(buf) / sizeof(buf[0]);
377             lRet = RegEnumKeyExW(hKey, i, buf, &bufLen, 0, 0, 0, &lastWrite);
378             if (lRet != ERROR_SUCCESS) continue;
379             if (strncmpiW(buf, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
380             if (!(name = strchrW(buf, '='))) continue;
381             *name = 0;
382             MSACM_RegisterDriver(buf, name + 1, 0);
383         }
384         RegCloseKey( hKey );
385     }
386
387     if (GetPrivateProfileSectionW(drv32, buf, sizeof(buf)/sizeof(buf[0]), sys))
388     {
389         for(s = buf; *s;  s += strlenW(s) + 1)
390         {
391             if (strncmpiW(s, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
392             if (!(name = strchrW(s, '='))) continue;
393             *name = 0;
394             MSACM_RegisterDriver(s, name + 1, 0);
395             *name = '=';
396         }
397     }
398     MSACM_ReorderDriversByPriority();
399     MSACM_RegisterDriver(msacm32, msacm32, 0);
400 }
401
402 /***********************************************************************
403  *           MSACM_RegisterNotificationWindow()
404  */
405 PWINE_ACMNOTIFYWND MSACM_RegisterNotificationWindow(HWND hNotifyWnd, DWORD dwNotifyMsg)
406 {
407     PWINE_ACMNOTIFYWND  panwnd;
408
409     TRACE("(%p,0x%08x)\n", hNotifyWnd, dwNotifyMsg);
410
411     panwnd = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMNOTIFYWND));
412     panwnd->obj.dwType = WINE_ACMOBJ_NOTIFYWND;
413     panwnd->obj.pACMDriverID = 0;
414     panwnd->hNotifyWnd = hNotifyWnd;
415     panwnd->dwNotifyMsg = dwNotifyMsg;
416     panwnd->fdwSupport = 0;
417     
418     panwnd->pNextACMNotifyWnd = NULL;
419     panwnd->pPrevACMNotifyWnd = MSACM_pLastACMNotifyWnd;
420     if (MSACM_pLastACMNotifyWnd)
421         MSACM_pLastACMNotifyWnd->pNextACMNotifyWnd = panwnd;
422     MSACM_pLastACMNotifyWnd = panwnd;
423     if (!MSACM_pFirstACMNotifyWnd)
424         MSACM_pFirstACMNotifyWnd = panwnd;
425
426     return panwnd;
427 }
428
429 /***********************************************************************
430  *           MSACM_BroadcastNotification()
431  */
432 void MSACM_BroadcastNotification(void)
433 {
434     if (MSACM_suspendBroadcastCount <= 0) {
435         PWINE_ACMNOTIFYWND panwnd;
436
437         for (panwnd = MSACM_pFirstACMNotifyWnd; panwnd; panwnd = panwnd->pNextACMNotifyWnd) 
438         if (!(panwnd->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED))
439             SendMessageW(panwnd->hNotifyWnd, panwnd->dwNotifyMsg, 0, 0);
440     } else {
441         MSACM_pendingBroadcast = TRUE;
442     }
443 }
444
445 /***********************************************************************
446  *           MSACM_DisableNotifications()
447  */
448 void MSACM_DisableNotifications(void)
449 {
450     MSACM_suspendBroadcastCount++;
451 }
452
453 /***********************************************************************
454  *           MSACM_EnableNotifications()
455  */
456 void MSACM_EnableNotifications(void)
457 {
458     if (MSACM_suspendBroadcastCount > 0) {
459         MSACM_suspendBroadcastCount--;
460         if (MSACM_suspendBroadcastCount == 0 && MSACM_pendingBroadcast) {
461             MSACM_pendingBroadcast = FALSE;
462             MSACM_BroadcastNotification();
463         }
464     }
465 }
466
467 /***********************************************************************
468  *           MSACM_UnRegisterNotificationWindow()
469  */
470 PWINE_ACMNOTIFYWND MSACM_UnRegisterNotificationWindow(const WINE_ACMNOTIFYWND *panwnd)
471 {
472     PWINE_ACMNOTIFYWND p;
473
474     for (p = MSACM_pFirstACMNotifyWnd; p; p = p->pNextACMNotifyWnd) {
475         if (p == panwnd) {
476             PWINE_ACMNOTIFYWND pNext = p->pNextACMNotifyWnd;
477
478             if (p->pPrevACMNotifyWnd) p->pPrevACMNotifyWnd->pNextACMNotifyWnd = p->pNextACMNotifyWnd;
479             if (p->pNextACMNotifyWnd) p->pNextACMNotifyWnd->pPrevACMNotifyWnd = p->pPrevACMNotifyWnd;
480             if (MSACM_pFirstACMNotifyWnd == p) MSACM_pFirstACMNotifyWnd = p->pNextACMNotifyWnd;
481             if (MSACM_pLastACMNotifyWnd == p) MSACM_pLastACMNotifyWnd = p->pPrevACMNotifyWnd;
482             HeapFree(MSACM_hHeap, 0, p);
483             
484             return pNext;
485         }
486     }
487     return NULL;
488 }
489
490 /***********************************************************************
491  *           MSACM_RePositionDriver()
492  */
493 void MSACM_RePositionDriver(PWINE_ACMDRIVERID padid, DWORD dwPriority)
494 {
495     PWINE_ACMDRIVERID pTargetPosition = NULL;
496                 
497     /* Remove selected driver from linked list */
498     if (MSACM_pFirstACMDriverID == padid) {
499         MSACM_pFirstACMDriverID = padid->pNextACMDriverID;
500     }
501     if (MSACM_pLastACMDriverID == padid) {
502         MSACM_pLastACMDriverID = padid->pPrevACMDriverID;
503     }
504     if (padid->pPrevACMDriverID != NULL) {
505         padid->pPrevACMDriverID->pNextACMDriverID = padid->pNextACMDriverID;
506     }
507     if (padid->pNextACMDriverID != NULL) {
508         padid->pNextACMDriverID->pPrevACMDriverID = padid->pPrevACMDriverID;
509     }
510     
511     /* Look up position where selected driver should be */
512     if (dwPriority == 1) {
513         pTargetPosition = padid->pPrevACMDriverID;
514         while (pTargetPosition->pPrevACMDriverID != NULL &&
515             !(pTargetPosition->pPrevACMDriverID->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL)) {
516             pTargetPosition = pTargetPosition->pPrevACMDriverID;
517         }
518     } else if (dwPriority == -1) {
519         pTargetPosition = padid->pNextACMDriverID;
520         while (pTargetPosition->pNextACMDriverID != NULL) {
521             pTargetPosition = pTargetPosition->pNextACMDriverID;
522         }
523     }
524     
525     /* Place selected driver in selected position */
526     padid->pPrevACMDriverID = pTargetPosition->pPrevACMDriverID;
527     padid->pNextACMDriverID = pTargetPosition;
528     if (padid->pPrevACMDriverID != NULL) {
529         padid->pPrevACMDriverID->pNextACMDriverID = padid;
530     } else {
531         MSACM_pFirstACMDriverID = padid;
532     }
533     if (padid->pNextACMDriverID != NULL) {
534         padid->pNextACMDriverID->pPrevACMDriverID = padid;
535     } else {
536         MSACM_pLastACMDriverID = padid;
537     }
538 }
539
540 /***********************************************************************
541  *           MSACM_ReorderDriversByPriority()
542  * Reorders all drivers based on the priority list indicated by the registry key:
543  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
544  */
545 static void MSACM_ReorderDriversByPriority(void)
546 {
547     PWINE_ACMDRIVERID   padid;
548     unsigned int iNumDrivers;
549     PWINE_ACMDRIVERID * driverList = NULL;
550     HKEY hPriorityKey = NULL;
551     
552     TRACE("\n");
553     
554     /* Count drivers && alloc corresponding memory for list */
555     iNumDrivers = 0;
556     for (padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) iNumDrivers++;
557     if (iNumDrivers > 1)
558     {
559         LONG lError;
560         static const WCHAR basePriorityKey[] = {
561             'S','o','f','t','w','a','r','e','\\',
562             'M','i','c','r','o','s','o','f','t','\\',
563             'M','u','l','t','i','m','e','d','i','a','\\',
564             'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
565             'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
566         };
567         unsigned int i;
568         LONG lBufferLength;
569         WCHAR szBuffer[256];
570         
571         driverList = HeapAlloc(MSACM_hHeap, 0, iNumDrivers * sizeof(PWINE_ACMDRIVERID));
572         if (!driverList)
573         {
574             ERR("out of memory\n");
575             goto errCleanUp;
576         }
577
578         lError = RegOpenKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
579         if (lError != ERROR_SUCCESS) {
580             TRACE("RegOpenKeyW failed, possibly key does not exist yet\n");
581             hPriorityKey = NULL;
582             goto errCleanUp;
583         } 
584             
585         /* Copy drivers into list to simplify linked list modification */
586         for (i = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID, i++)
587         {
588             driverList[i] = padid;
589         }
590
591         /* Query each of the priorities in turn. Alias key is in lowercase. 
592             The general form of the priority record is the following:
593             "PriorityN" --> "1, msacm.driveralias"
594             where N is an integer, and the value is a string of the driver
595             alias, prefixed by "1, " for an enabled driver, or "0, " for a
596             disabled driver.
597             */
598         for (i = 0; i < iNumDrivers; i++)
599         {
600             static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
601             WCHAR szSubKey[17];
602             unsigned int iTargetPosition;
603             unsigned int iCurrentPosition;
604             WCHAR * pAlias;
605             static const WCHAR sPrefix[] = {'m','s','a','c','m','.','\0'};
606             
607             /* Build expected entry name */
608             snprintfW(szSubKey, 17, priorityTmpl, i + 1);
609             lBufferLength = sizeof(szBuffer);
610             lError = RegQueryValueExW(hPriorityKey, szSubKey, NULL, NULL, (LPBYTE)szBuffer, (LPDWORD)&lBufferLength);
611             if (lError != ERROR_SUCCESS) continue;
612
613             /* Recovered driver alias should be at this position */
614             iTargetPosition = i;
615             
616             /* Locate driver alias in driver list */
617             pAlias = strstrW(szBuffer, sPrefix);
618             if (pAlias == NULL) continue;
619             
620             for (iCurrentPosition = 0; iCurrentPosition < iNumDrivers; iCurrentPosition++) {
621                 if (strcmpiW(driverList[iCurrentPosition]->pszDriverAlias, pAlias) == 0) 
622                     break;
623             }
624             if (iCurrentPosition < iNumDrivers && iTargetPosition != iCurrentPosition) {
625                 padid = driverList[iTargetPosition];
626                 driverList[iTargetPosition] = driverList[iCurrentPosition];
627                 driverList[iCurrentPosition] = padid;
628
629                 /* Locate enabled status */
630                 if (szBuffer[0] == '1') {
631                     driverList[iTargetPosition]->fdwSupport &= ~ACMDRIVERDETAILS_SUPPORTF_DISABLED;
632                 } else if (szBuffer[0] == '0') {
633                     driverList[iTargetPosition]->fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_DISABLED;
634                 }
635             }
636         }
637         
638         /* Re-assign pointers so that linked list traverses the ordered array */
639         for (i = 0; i < iNumDrivers; i++) {
640             driverList[i]->pPrevACMDriverID = (i > 0) ? driverList[i - 1] : NULL;
641             driverList[i]->pNextACMDriverID = (i < iNumDrivers - 1) ? driverList[i + 1] : NULL;
642         }
643         MSACM_pFirstACMDriverID = driverList[0];
644         MSACM_pLastACMDriverID = driverList[iNumDrivers - 1];
645     }
646     
647 errCleanUp:
648     if (hPriorityKey != NULL) RegCloseKey(hPriorityKey);
649     HeapFree(MSACM_hHeap, 0, driverList);
650 }
651
652 /***********************************************************************
653  *           MSACM_WriteCurrentPriorities()
654  * Writes out current order of driver priorities to registry key:
655  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
656  */
657 void MSACM_WriteCurrentPriorities(void)
658 {
659     LONG lError;
660     HKEY hPriorityKey;
661     static const WCHAR basePriorityKey[] = {
662         'S','o','f','t','w','a','r','e','\\',
663         'M','i','c','r','o','s','o','f','t','\\',
664         'M','u','l','t','i','m','e','d','i','a','\\',
665         'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
666         'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
667     };
668     PWINE_ACMDRIVERID padid;
669     DWORD dwPriorityCounter;
670     static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
671     static const WCHAR valueTmpl[] = {'%','c',',',' ','%','s','\0'};
672     static const WCHAR converterAlias[] = {'I','n','t','e','r','n','a','l',' ','P','C','M',' ','C','o','n','v','e','r','t','e','r','\0'};
673     WCHAR szSubKey[17];
674     WCHAR szBuffer[256];
675
676     /* Delete ACM priority key and create it anew */
677     lError = RegDeleteKeyW(HKEY_CURRENT_USER, basePriorityKey);
678     if (lError != ERROR_SUCCESS && lError != ERROR_FILE_NOT_FOUND) {
679         ERR("unable to remove current key %s (0x%08x) - priority changes won't persist past application end.\n",
680             debugstr_w(basePriorityKey), lError);
681         return;
682     }
683     lError = RegCreateKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
684     if (lError != ERROR_SUCCESS) {
685         ERR("unable to create key %s (0x%08x) - priority changes won't persist past application end.\n",
686             debugstr_w(basePriorityKey), lError);
687         return;
688     }
689     
690     /* Write current list of priorities */
691     for (dwPriorityCounter = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) {        
692         if (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL) continue;
693         if (padid->pszDriverAlias == NULL) continue;    /* internal PCM converter is last */
694
695         /* Build required value name */
696         dwPriorityCounter++;
697         snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
698         
699         /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
700         snprintfW(szBuffer, 256, valueTmpl, (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ? '0' : '1', padid->pszDriverAlias);
701         strlwrW(szBuffer);
702         
703         lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
704         if (lError != ERROR_SUCCESS) {
705             ERR("unable to write value for %s under key %s (0x%08x)\n",
706                 debugstr_w(padid->pszDriverAlias), debugstr_w(basePriorityKey), lError);
707         }
708     }
709     
710     /* Build required value name */
711     dwPriorityCounter++;
712     snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
713         
714     /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
715     snprintfW(szBuffer, 256, valueTmpl, '1', converterAlias);
716         
717     lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
718     if (lError != ERROR_SUCCESS) {
719         ERR("unable to write value for %s under key %s (0x%08x)\n",
720             debugstr_w(converterAlias), debugstr_w(basePriorityKey), lError);
721     }
722     RegCloseKey(hPriorityKey);
723 }
724
725 static PWINE_ACMLOCALDRIVER MSACM_pFirstACMLocalDriver;
726 static PWINE_ACMLOCALDRIVER MSACM_pLastACMLocalDriver;
727
728 static PWINE_ACMLOCALDRIVER MSACM_UnregisterLocalDriver(PWINE_ACMLOCALDRIVER paldrv)
729 {
730     PWINE_ACMLOCALDRIVER pNextACMLocalDriver;
731
732     if (paldrv->pACMInstList) {
733         ERR("local driver instances still present after closing all drivers - memory leak\n");
734         return NULL;
735     }
736
737     if (paldrv == MSACM_pFirstACMLocalDriver)
738         MSACM_pFirstACMLocalDriver = paldrv->pNextACMLocalDrv;
739     if (paldrv == MSACM_pLastACMLocalDriver)
740         MSACM_pLastACMLocalDriver = paldrv->pPrevACMLocalDrv;
741
742     if (paldrv->pPrevACMLocalDrv)
743         paldrv->pPrevACMLocalDrv->pNextACMLocalDrv = paldrv->pNextACMLocalDrv;
744     if (paldrv->pNextACMLocalDrv)
745         paldrv->pNextACMLocalDrv->pPrevACMLocalDrv = paldrv->pPrevACMLocalDrv;
746
747     pNextACMLocalDriver = paldrv->pNextACMLocalDrv;
748
749     HeapFree(MSACM_hHeap, 0, paldrv);
750
751     return pNextACMLocalDriver;
752 }
753
754 /***********************************************************************
755  *           MSACM_UnregisterDriver()
756  */
757 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
758 {
759     PWINE_ACMDRIVERID pNextACMDriverID;
760
761     while (p->pACMDriverList)
762         acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
763
764     HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
765     HeapFree(MSACM_hHeap, 0, p->pszFileName);
766     HeapFree(MSACM_hHeap, 0, p->aFormatTag);
767
768     if (p == MSACM_pFirstACMDriverID)
769         MSACM_pFirstACMDriverID = p->pNextACMDriverID;
770     if (p == MSACM_pLastACMDriverID)
771         MSACM_pLastACMDriverID = p->pPrevACMDriverID;
772
773     if (p->pPrevACMDriverID)
774         p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
775     if (p->pNextACMDriverID)
776         p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
777
778     pNextACMDriverID = p->pNextACMDriverID;
779
780     if (p->pLocalDriver) MSACM_UnregisterLocalDriver(p->pLocalDriver);
781     HeapFree(MSACM_hHeap, 0, p);
782
783     return pNextACMDriverID;
784 }
785
786 /***********************************************************************
787  *           MSACM_UnregisterAllDrivers()
788  */
789 void MSACM_UnregisterAllDrivers(void)
790 {
791     PWINE_ACMNOTIFYWND panwnd = MSACM_pFirstACMNotifyWnd;
792     PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
793
794     while (p) {
795         MSACM_WriteCache(p);
796         p = MSACM_UnregisterDriver(p);
797     }
798     
799     while (panwnd) {
800         panwnd = MSACM_UnRegisterNotificationWindow(panwnd);
801     }
802 }
803
804 /***********************************************************************
805  *           MSACM_GetObj()
806  */
807 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
808 {
809     PWINE_ACMOBJ        pao = (PWINE_ACMOBJ)hObj;
810
811     if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
812         ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
813         return NULL;
814     return pao;
815 }
816
817 /***********************************************************************
818  *           MSACM_GetDriverID()
819  */
820 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
821 {
822     return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
823 }
824
825 /***********************************************************************
826  *           MSACM_GetDriver()
827  */
828 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
829 {
830     return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
831 }
832
833 /***********************************************************************
834  *           MSACM_GetNotifyWnd()
835  */
836 PWINE_ACMNOTIFYWND MSACM_GetNotifyWnd(HACMDRIVERID hDriver)
837 {
838     return (PWINE_ACMNOTIFYWND)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_NOTIFYWND);
839 }
840
841 /***********************************************************************
842  *           MSACM_GetLocalDriver()
843  */
844 /* 
845 PWINE_ACMLOCALDRIVER MSACM_GetLocalDriver(HACMDRIVER hDriver)
846 {
847     return (PWINE_ACMLOCALDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_LOCALDRIVER);
848 }
849 */
850 #define MSACM_DRIVER_SendMessage(PDRVRINST, msg, lParam1, lParam2) \
851         (PDRVRINST)->pLocalDriver->lpDrvProc((PDRVRINST)->dwDriverID, (HDRVR)(PDRVRINST), msg, lParam1, lParam2)
852
853 /***********************************************************************
854  *           MSACM_Message()
855  */
856 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
857 {
858     PWINE_ACMDRIVER     pad = MSACM_GetDriver(had);
859
860     if (!pad) return MMSYSERR_INVALHANDLE;
861     if (pad->hDrvr) return SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2);
862     if (pad->pLocalDrvrInst) return MSACM_DRIVER_SendMessage(pad->pLocalDrvrInst, uMsg, lParam1, lParam2);
863
864     return MMSYSERR_INVALHANDLE;
865 }
866
867 PWINE_ACMLOCALDRIVER MSACM_RegisterLocalDriver(HMODULE hModule, DRIVERPROC lpDriverProc)
868 {
869     PWINE_ACMLOCALDRIVER paldrv;
870
871     TRACE("(%p, %p)\n", hModule, lpDriverProc);
872     if (!hModule || !lpDriverProc) return NULL;
873     
874     /* look up previous instance of local driver module */
875     for (paldrv = MSACM_pFirstACMLocalDriver; paldrv; paldrv = paldrv->pNextACMLocalDrv)
876     {
877         if (paldrv->hModule == hModule && paldrv->lpDrvProc == lpDriverProc) return paldrv;
878     }
879
880     paldrv = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMLOCALDRIVER));
881     paldrv->obj.dwType = WINE_ACMOBJ_LOCALDRIVER;
882     paldrv->obj.pACMDriverID = 0;
883     paldrv->hModule = hModule;
884     paldrv->lpDrvProc = lpDriverProc;
885     paldrv->pACMInstList = NULL;
886
887     paldrv->pNextACMLocalDrv = NULL;
888     paldrv->pPrevACMLocalDrv = MSACM_pLastACMLocalDriver;
889     if (MSACM_pLastACMLocalDriver)
890         MSACM_pLastACMLocalDriver->pNextACMLocalDrv = paldrv;
891     MSACM_pLastACMLocalDriver = paldrv;
892     if (!MSACM_pFirstACMLocalDriver)
893         MSACM_pFirstACMLocalDriver = paldrv;
894
895     return paldrv;
896 }
897
898 /**************************************************************************
899  *                      MSACM_GetNumberOfModuleRefs             [internal]
900  *
901  * Returns the number of open drivers which share the same module.
902  * Inspired from implementation in dlls/winmm/driver.c
903  */
904 static unsigned MSACM_GetNumberOfModuleRefs(HMODULE hModule, DRIVERPROC lpDrvProc, WINE_ACMLOCALDRIVERINST ** found)
905 {
906     PWINE_ACMLOCALDRIVER lpDrv;
907     unsigned            count = 0;
908
909     if (found) *found = NULL;
910     for (lpDrv = MSACM_pFirstACMLocalDriver; lpDrv; lpDrv = lpDrv->pNextACMLocalDrv)
911     {
912         if (lpDrv->hModule == hModule && lpDrv->lpDrvProc == lpDrvProc)
913         {
914             PWINE_ACMLOCALDRIVERINST pInst = lpDrv->pACMInstList;
915         
916             while (pInst) {
917                 if (found && !*found) *found = pInst;
918                 count++;
919                 pInst = pInst->pNextACMInst;
920             }
921         }
922     }
923     return count;
924 }
925
926 /**************************************************************************
927  *                              MSACM_RemoveFromList            [internal]
928  *
929  * Generates all the logic to handle driver closure / deletion
930  * Removes a driver struct to the list of open drivers.
931  */
932 static  BOOL    MSACM_RemoveFromList(PWINE_ACMLOCALDRIVERINST lpDrv)
933 {
934     PWINE_ACMLOCALDRIVER pDriverBase = lpDrv->pLocalDriver;
935     PWINE_ACMLOCALDRIVERINST pPrevInst;
936
937     /* last of this driver in list ? */
938     if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, NULL) == 1) {
939         MSACM_DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
940         MSACM_DRIVER_SendMessage(lpDrv, DRV_FREE,    0L, 0L);
941     }
942     
943     pPrevInst = NULL;
944     if (pDriverBase->pACMInstList != lpDrv) {
945         pPrevInst = pDriverBase->pACMInstList;
946         while (pPrevInst && pPrevInst->pNextACMInst != lpDrv)
947             pPrevInst = pPrevInst->pNextACMInst;
948         if (!pPrevInst) {
949             ERR("requested to remove invalid instance %p\n", pPrevInst);
950             return FALSE;
951         }
952     }
953     if (!pPrevInst) {
954         /* first driver instance on list */
955         pDriverBase->pACMInstList = lpDrv->pNextACMInst;
956     } else {
957         pPrevInst->pNextACMInst = lpDrv->pNextACMInst;
958     }
959     return TRUE;
960 }
961
962 /**************************************************************************
963  *                              MSACM_AddToList         [internal]
964  *
965  * Adds a driver struct to the list of open drivers.
966  * Generates all the logic to handle driver creation / open.
967  */
968 static  BOOL    MSACM_AddToList(PWINE_ACMLOCALDRIVERINST lpNewDrv, LPARAM lParam2)
969 {
970     PWINE_ACMLOCALDRIVER pDriverBase = lpNewDrv->pLocalDriver;
971
972     /* first of this driver in list ? */
973     if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, NULL) == 0) {
974         if (MSACM_DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
975             FIXME("DRV_LOAD failed on driver %p\n", lpNewDrv);
976             return FALSE;
977         }
978         /* returned value is not checked */
979         MSACM_DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
980     }
981
982     lpNewDrv->pNextACMInst = NULL;
983     if (pDriverBase->pACMInstList == NULL) {
984         pDriverBase->pACMInstList = lpNewDrv;
985     } else {
986         PWINE_ACMLOCALDRIVERINST lpDrvInst = pDriverBase->pACMInstList;
987     
988         while (lpDrvInst->pNextACMInst != NULL)
989             lpDrvInst = lpDrvInst->pNextACMInst;
990
991         lpDrvInst->pNextACMInst = lpNewDrv;
992     }
993
994     /* Now just open a new instance of a driver on this module */
995     lpNewDrv->dwDriverID = MSACM_DRIVER_SendMessage(lpNewDrv, DRV_OPEN, 0, lParam2);
996
997     if (lpNewDrv->dwDriverID == 0) {
998         FIXME("DRV_OPEN failed on driver %p\n", lpNewDrv);
999         MSACM_RemoveFromList(lpNewDrv);
1000         return FALSE;
1001     }
1002     return TRUE;
1003 }
1004
1005 PWINE_ACMLOCALDRIVERINST MSACM_OpenLocalDriver(PWINE_ACMLOCALDRIVER paldrv, LPARAM lParam2)
1006 {
1007     PWINE_ACMLOCALDRIVERINST pDrvInst;
1008     
1009     pDrvInst = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMLOCALDRIVERINST));
1010     pDrvInst->pLocalDriver = paldrv;
1011     pDrvInst->dwDriverID = 0;
1012     pDrvInst->pNextACMInst = NULL;
1013     pDrvInst->bSession = FALSE;
1014     
1015     /* Win32 installable drivers must support a two phase opening scheme:
1016      * + first open with NULL as lParam2 (session instance),
1017      * + then do a second open with the real non null lParam2)
1018      */
1019     if (MSACM_GetNumberOfModuleRefs(paldrv->hModule, paldrv->lpDrvProc, NULL) == 0 && lParam2)
1020     {
1021         PWINE_ACMLOCALDRIVERINST   ret;
1022
1023         if (!MSACM_AddToList(pDrvInst, 0L))
1024         {
1025             ERR("load0 failed\n");
1026             goto exit;
1027         }
1028         ret = MSACM_OpenLocalDriver(paldrv, lParam2);
1029         if (!ret)
1030         {
1031             MSACM_CloseLocalDriver(pDrvInst);
1032             ERR("load1 failed\n");
1033             goto exit;
1034         }
1035         pDrvInst->bSession = TRUE;
1036         return ret;
1037     }
1038     
1039     if (!MSACM_AddToList(pDrvInst, lParam2))
1040     {
1041         ERR("load failed\n");
1042         goto exit;
1043     }
1044
1045     TRACE("=> %p\n", pDrvInst);
1046     return pDrvInst;
1047 exit:
1048     HeapFree(MSACM_hHeap, 0, pDrvInst);
1049     return NULL;
1050 }
1051
1052 LRESULT MSACM_CloseLocalDriver(PWINE_ACMLOCALDRIVERINST paldrv)
1053 {
1054     if (MSACM_RemoveFromList(paldrv)) {
1055         PWINE_ACMLOCALDRIVERINST lpDrv0;
1056         PWINE_ACMLOCALDRIVER pDriverBase = paldrv->pLocalDriver;
1057     
1058         MSACM_DRIVER_SendMessage(paldrv, DRV_CLOSE, 0, 0);
1059         paldrv->dwDriverID = 0;
1060     
1061         if (paldrv->bSession)
1062             ERR("should not directly close session instance (%p)\n", paldrv);
1063
1064         /* if driver has an opened session instance, we have to close it too */
1065         if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, &lpDrv0) == 1 &&
1066                 lpDrv0->bSession)
1067         {
1068             MSACM_DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
1069             lpDrv0->dwDriverID = 0;
1070             MSACM_RemoveFromList(lpDrv0);
1071             HeapFree(GetProcessHeap(), 0, lpDrv0);
1072         }
1073
1074         HeapFree(MSACM_hHeap, 0, paldrv);
1075         return TRUE;
1076     }
1077     ERR("unable to close driver instance\n");
1078     return FALSE;
1079 }