advapi32: Implement filter matching for CredEnumerate.
[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 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
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(buf, 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     padid->obj.dwType = WINE_ACMOBJ_DRIVERID;
293     padid->obj.pACMDriverID = padid;
294     padid->pszDriverAlias = NULL;
295     if (pszDriverAlias)
296     {
297         padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszDriverAlias)+1) * sizeof(WCHAR) );
298         strcpyW( padid->pszDriverAlias, pszDriverAlias );
299     }
300     padid->pszFileName = NULL;
301     if (pszFileName)
302     {
303         padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszFileName)+1) * sizeof(WCHAR) );
304         strcpyW( padid->pszFileName, pszFileName );
305     }
306     padid->pLocalDriver = pLocalDriver;
307
308     padid->pACMDriverList = NULL;
309     
310     if (pLocalDriver) {
311         padid->pPrevACMDriverID = NULL;
312         padid->pNextACMDriverID = MSACM_pFirstACMDriverID;
313         if (MSACM_pFirstACMDriverID)
314             MSACM_pFirstACMDriverID->pPrevACMDriverID = padid;
315         MSACM_pFirstACMDriverID = padid;
316         if (!MSACM_pLastACMDriverID)
317             MSACM_pLastACMDriverID = padid;
318     } else {
319         padid->pNextACMDriverID = NULL;
320         padid->pPrevACMDriverID = MSACM_pLastACMDriverID;
321         if (MSACM_pLastACMDriverID)
322             MSACM_pLastACMDriverID->pNextACMDriverID = padid;
323         MSACM_pLastACMDriverID = padid;
324         if (!MSACM_pFirstACMDriverID)
325             MSACM_pFirstACMDriverID = padid;
326     }
327     /* disable the driver if we cannot load the cache */
328     if ((!padid->pszDriverAlias || !MSACM_ReadCache(padid)) && !MSACM_FillCache(padid)) {
329         WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName));
330         MSACM_UnregisterDriver(padid);
331         return NULL;
332     }
333
334     if (pLocalDriver) padid->fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_LOCAL;
335     return padid;
336 }
337
338 /***********************************************************************
339  *           MSACM_RegisterAllDrivers()
340  */
341 void MSACM_RegisterAllDrivers(void)
342 {
343     static const WCHAR msacm32[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
344     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
345     static const WCHAR drv32[] = {'d','r','i','v','e','r','s','3','2','\0'};
346     static const WCHAR sys[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
347     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
348                                    'M','i','c','r','o','s','o','f','t','\\',
349                                    'W','i','n','d','o','w','s',' ','N','T','\\',
350                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
351                                    'D','r','i','v','e','r','s','3','2','\0'};
352     DWORD i, cnt = 0, bufLen, lRet;
353     WCHAR buf[2048], *name, *s;
354     FILETIME lastWrite;
355     HKEY hKey;
356
357     /* FIXME: What if the user edits system.ini while the program is running?
358      * Does Windows handle that?  */
359     if (MSACM_pFirstACMDriverID) return;
360
361     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
362     if (lRet == ERROR_SUCCESS) {
363         RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
364         for (i = 0; i < cnt; i++) {
365             bufLen = sizeof(buf) / sizeof(buf[0]);
366             lRet = RegEnumKeyExW(hKey, i, buf, &bufLen, 0, 0, 0, &lastWrite);
367             if (lRet != ERROR_SUCCESS) continue;
368             if (strncmpiW(buf, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
369             if (!(name = strchrW(buf, '='))) continue;
370             *name = 0;
371             MSACM_RegisterDriver(buf, name + 1, 0);
372         }
373         RegCloseKey( hKey );
374     }
375
376     if (GetPrivateProfileSectionW(drv32, buf, sizeof(buf)/sizeof(buf[0]), sys))
377     {
378         for(s = buf; *s;  s += strlenW(s) + 1)
379         {
380             if (strncmpiW(s, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
381             if (!(name = strchrW(s, '='))) continue;
382             *name = 0;
383             MSACM_RegisterDriver(s, name + 1, 0);
384             *name = '=';
385         }
386     }
387     MSACM_ReorderDriversByPriority();
388     MSACM_RegisterDriver(msacm32, msacm32, 0);
389 }
390
391 /***********************************************************************
392  *           MSACM_RegisterNotificationWindow()
393  */
394 PWINE_ACMNOTIFYWND MSACM_RegisterNotificationWindow(HWND hNotifyWnd, DWORD dwNotifyMsg)
395 {
396     PWINE_ACMNOTIFYWND  panwnd;
397
398     TRACE("(%p,0x%08x)\n", hNotifyWnd, dwNotifyMsg);
399
400     panwnd = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMNOTIFYWND));
401     panwnd->obj.dwType = WINE_ACMOBJ_NOTIFYWND;
402     panwnd->obj.pACMDriverID = 0;
403     panwnd->hNotifyWnd = hNotifyWnd;
404     panwnd->dwNotifyMsg = dwNotifyMsg;
405     panwnd->fdwSupport = 0;
406     
407     panwnd->pNextACMNotifyWnd = NULL;
408     panwnd->pPrevACMNotifyWnd = MSACM_pLastACMNotifyWnd;
409     if (MSACM_pLastACMNotifyWnd)
410         MSACM_pLastACMNotifyWnd->pNextACMNotifyWnd = panwnd;
411     MSACM_pLastACMNotifyWnd = panwnd;
412     if (!MSACM_pFirstACMNotifyWnd)
413         MSACM_pFirstACMNotifyWnd = panwnd;
414
415     return panwnd;
416 }
417
418 /***********************************************************************
419  *           MSACM_BroadcastNotification()
420  */
421 void MSACM_BroadcastNotification(void)
422 {
423     if (MSACM_suspendBroadcastCount <= 0) {
424         PWINE_ACMNOTIFYWND panwnd;
425
426         for (panwnd = MSACM_pFirstACMNotifyWnd; panwnd; panwnd = panwnd->pNextACMNotifyWnd) 
427         if (!(panwnd->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED))
428             SendMessageW(panwnd->hNotifyWnd, panwnd->dwNotifyMsg, 0, 0);
429     } else {
430         MSACM_pendingBroadcast = TRUE;
431     }
432 }
433
434 /***********************************************************************
435  *           MSACM_DisableNotifications()
436  */
437 void MSACM_DisableNotifications(void)
438 {
439     MSACM_suspendBroadcastCount++;
440 }
441
442 /***********************************************************************
443  *           MSACM_EnableNotifications()
444  */
445 void MSACM_EnableNotifications(void)
446 {
447     if (MSACM_suspendBroadcastCount > 0) {
448         MSACM_suspendBroadcastCount--;
449         if (MSACM_suspendBroadcastCount == 0 && MSACM_pendingBroadcast) {
450             MSACM_pendingBroadcast = FALSE;
451             MSACM_BroadcastNotification();
452         }
453     }
454 }
455
456 /***********************************************************************
457  *           MSACM_UnRegisterNotificationWindow()
458  */
459 PWINE_ACMNOTIFYWND MSACM_UnRegisterNotificationWindow(const WINE_ACMNOTIFYWND *panwnd)
460 {
461     PWINE_ACMNOTIFYWND p;
462
463     for (p = MSACM_pFirstACMNotifyWnd; p; p = p->pNextACMNotifyWnd) {
464         if (p == panwnd) {
465             PWINE_ACMNOTIFYWND pNext = p->pNextACMNotifyWnd;
466
467             if (p->pPrevACMNotifyWnd) p->pPrevACMNotifyWnd->pNextACMNotifyWnd = p->pNextACMNotifyWnd;
468             if (p->pNextACMNotifyWnd) p->pNextACMNotifyWnd->pPrevACMNotifyWnd = p->pPrevACMNotifyWnd;
469             if (MSACM_pFirstACMNotifyWnd == p) MSACM_pFirstACMNotifyWnd = p->pNextACMNotifyWnd;
470             if (MSACM_pLastACMNotifyWnd == p) MSACM_pLastACMNotifyWnd = p->pPrevACMNotifyWnd;
471             HeapFree(MSACM_hHeap, 0, p);
472             
473             return pNext;
474         }
475     }
476     return NULL;
477 }
478
479 /***********************************************************************
480  *           MSACM_RePositionDriver()
481  */
482 void MSACM_RePositionDriver(PWINE_ACMDRIVERID padid, DWORD dwPriority)
483 {
484     PWINE_ACMDRIVERID pTargetPosition = NULL;
485                 
486     /* Remove selected driver from linked list */
487     if (MSACM_pFirstACMDriverID == padid) {
488         MSACM_pFirstACMDriverID = padid->pNextACMDriverID;
489     }
490     if (MSACM_pLastACMDriverID == padid) {
491         MSACM_pLastACMDriverID = padid->pPrevACMDriverID;
492     }
493     if (padid->pPrevACMDriverID != NULL) {
494         padid->pPrevACMDriverID->pNextACMDriverID = padid->pNextACMDriverID;
495     }
496     if (padid->pNextACMDriverID != NULL) {
497         padid->pNextACMDriverID->pPrevACMDriverID = padid->pPrevACMDriverID;
498     }
499     
500     /* Look up position where selected driver should be */
501     if (dwPriority == 1) {
502         pTargetPosition = padid->pPrevACMDriverID;
503         while (pTargetPosition->pPrevACMDriverID != NULL &&
504             !(pTargetPosition->pPrevACMDriverID->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL)) {
505             pTargetPosition = pTargetPosition->pPrevACMDriverID;
506         }
507     } else if (dwPriority == -1) {
508         pTargetPosition = padid->pNextACMDriverID;
509         while (pTargetPosition->pNextACMDriverID != NULL) {
510             pTargetPosition = pTargetPosition->pNextACMDriverID;
511         }
512     }
513     
514     /* Place selected driver in selected position */
515     padid->pPrevACMDriverID = pTargetPosition->pPrevACMDriverID;
516     padid->pNextACMDriverID = pTargetPosition;
517     if (padid->pPrevACMDriverID != NULL) {
518         padid->pPrevACMDriverID->pNextACMDriverID = padid;
519     } else {
520         MSACM_pFirstACMDriverID = padid;
521     }
522     if (padid->pNextACMDriverID != NULL) {
523         padid->pNextACMDriverID->pPrevACMDriverID = padid;
524     } else {
525         MSACM_pLastACMDriverID = padid;
526     }
527 }
528
529 /***********************************************************************
530  *           MSACM_ReorderDriversByPriority()
531  * Reorders all drivers based on the priority list indicated by the registry key:
532  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
533  */
534 static void MSACM_ReorderDriversByPriority(void)
535 {
536     PWINE_ACMDRIVERID   padid;
537     unsigned int iNumDrivers;
538     PWINE_ACMDRIVERID * driverList = NULL;
539     HKEY hPriorityKey = NULL;
540     
541     TRACE("\n");
542     
543     /* Count drivers && alloc corresponding memory for list */
544     iNumDrivers = 0;
545     for (padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) iNumDrivers++;
546     if (iNumDrivers > 1)
547     {
548         LONG lError;
549         static const WCHAR basePriorityKey[] = {
550             'S','o','f','t','w','a','r','e','\\',
551             'M','i','c','r','o','s','o','f','t','\\',
552             'M','u','l','t','i','m','e','d','i','a','\\',
553             'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
554             'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
555         };
556         unsigned int i;
557         LONG lBufferLength;
558         WCHAR szBuffer[256];
559         
560         driverList = HeapAlloc(MSACM_hHeap, 0, iNumDrivers * sizeof(PWINE_ACMDRIVERID));
561         if (!driverList)
562         {
563             ERR("out of memory\n");
564             goto errCleanUp;
565         }
566
567         lError = RegOpenKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
568         if (lError != ERROR_SUCCESS) {
569             TRACE("RegOpenKeyW failed, possibly key does not exist yet\n");
570             hPriorityKey = NULL;
571             goto errCleanUp;
572         } 
573             
574         /* Copy drivers into list to simplify linked list modification */
575         for (i = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID, i++)
576         {
577             driverList[i] = padid;
578         }
579
580         /* Query each of the priorities in turn. Alias key is in lowercase. 
581             The general form of the priority record is the following:
582             "PriorityN" --> "1, msacm.driveralias"
583             where N is an integer, and the value is a string of the driver
584             alias, prefixed by "1, " for an enabled driver, or "0, " for a
585             disabled driver.
586             */
587         for (i = 0; i < iNumDrivers; i++)
588         {
589             static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
590             WCHAR szSubKey[17];
591             unsigned int iTargetPosition;
592             unsigned int iCurrentPosition;
593             WCHAR * pAlias;
594             static const WCHAR sPrefix[] = {'m','s','a','c','m','.','\0'};
595             
596             /* Build expected entry name */
597             snprintfW(szSubKey, 17, priorityTmpl, i + 1);
598             lBufferLength = sizeof(szBuffer);
599             lError = RegQueryValueExW(hPriorityKey, szSubKey, NULL, NULL, (LPBYTE)szBuffer, (LPDWORD)&lBufferLength);
600             if (lError != ERROR_SUCCESS) continue;
601
602             /* Recovered driver alias should be at this position */
603             iTargetPosition = i;
604             
605             /* Locate driver alias in driver list */
606             pAlias = strstrW(szBuffer, sPrefix);
607             if (pAlias == NULL) continue;
608             
609             for (iCurrentPosition = 0; iCurrentPosition < iNumDrivers; iCurrentPosition++) {
610                 if (strcmpiW(driverList[iCurrentPosition]->pszDriverAlias, pAlias) == 0) 
611                     break;
612             }
613             if (iCurrentPosition < iNumDrivers && iTargetPosition != iCurrentPosition) {
614                 padid = driverList[iTargetPosition];
615                 driverList[iTargetPosition] = driverList[iCurrentPosition];
616                 driverList[iCurrentPosition] = padid;
617
618                 /* Locate enabled status */
619                 if (szBuffer[0] == '1') {
620                     driverList[iTargetPosition]->fdwSupport &= ~ACMDRIVERDETAILS_SUPPORTF_DISABLED;
621                 } else if (szBuffer[0] == '0') {
622                     driverList[iTargetPosition]->fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_DISABLED;
623                 }
624             }
625         }
626         
627         /* Re-assign pointers so that linked list traverses the ordered array */
628         for (i = 0; i < iNumDrivers; i++) {
629             driverList[i]->pPrevACMDriverID = (i > 0) ? driverList[i - 1] : NULL;
630             driverList[i]->pNextACMDriverID = (i < iNumDrivers - 1) ? driverList[i + 1] : NULL;
631         }
632         MSACM_pFirstACMDriverID = driverList[0];
633         MSACM_pLastACMDriverID = driverList[iNumDrivers - 1];
634     }
635     
636 errCleanUp:
637     if (hPriorityKey != NULL) RegCloseKey(hPriorityKey);
638     HeapFree(MSACM_hHeap, 0, driverList);
639 }
640
641 /***********************************************************************
642  *           MSACM_WriteCurrentPriorities()
643  * Writes out current order of driver priorities to registry key:
644  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
645  */
646 void MSACM_WriteCurrentPriorities(void)
647 {
648     LONG lError;
649     HKEY hPriorityKey;
650     static const WCHAR basePriorityKey[] = {
651         'S','o','f','t','w','a','r','e','\\',
652         'M','i','c','r','o','s','o','f','t','\\',
653         'M','u','l','t','i','m','e','d','i','a','\\',
654         'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
655         'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
656     };
657     PWINE_ACMDRIVERID padid;
658     DWORD dwPriorityCounter;
659     static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
660     static const WCHAR valueTmpl[] = {'%','c',',',' ','%','s','\0'};
661     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'};
662     WCHAR szSubKey[17];
663     WCHAR szBuffer[256];
664
665     /* Delete ACM priority key and create it anew */
666     lError = RegDeleteKeyW(HKEY_CURRENT_USER, basePriorityKey);
667     if (lError != ERROR_SUCCESS && lError != ERROR_FILE_NOT_FOUND) {
668         ERR("unable to remove current key %s (0x%08x) - priority changes won't persist past application end.\n",
669             debugstr_w(basePriorityKey), lError);
670         return;
671     }
672     lError = RegCreateKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
673     if (lError != ERROR_SUCCESS) {
674         ERR("unable to create key %s (0x%08x) - priority changes won't persist past application end.\n",
675             debugstr_w(basePriorityKey), lError);
676         return;
677     }
678     
679     /* Write current list of priorities */
680     for (dwPriorityCounter = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) {        
681         if (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL) continue;
682         if (padid->pszDriverAlias == NULL) continue;    /* internal PCM converter is last */
683
684         /* Build required value name */
685         dwPriorityCounter++;
686         snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
687         
688         /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
689         snprintfW(szBuffer, 256, valueTmpl, (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ? '0' : '1', padid->pszDriverAlias);
690         strlwrW(szBuffer);
691         
692         lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
693         if (lError != ERROR_SUCCESS) {
694             ERR("unable to write value for %s under key %s (0x%08x)\n",
695                 debugstr_w(padid->pszDriverAlias), debugstr_w(basePriorityKey), lError);
696         }
697     }
698     
699     /* Build required value name */
700     dwPriorityCounter++;
701     snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
702         
703     /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
704     snprintfW(szBuffer, 256, valueTmpl, '1', converterAlias);
705         
706     lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
707     if (lError != ERROR_SUCCESS) {
708         ERR("unable to write value for %s under key %s (0x%08x)\n",
709             debugstr_w(converterAlias), debugstr_w(basePriorityKey), lError);
710     }
711     RegCloseKey(hPriorityKey);
712 }
713
714 /***********************************************************************
715  *           MSACM_UnregisterDriver()
716  */
717 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
718 {
719     PWINE_ACMDRIVERID pNextACMDriverID;
720
721     while (p->pACMDriverList)
722         acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
723
724     HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
725     HeapFree(MSACM_hHeap, 0, p->pszFileName);
726     HeapFree(MSACM_hHeap, 0, p->aFormatTag);
727
728     if (p == MSACM_pFirstACMDriverID)
729         MSACM_pFirstACMDriverID = p->pNextACMDriverID;
730     if (p == MSACM_pLastACMDriverID)
731         MSACM_pLastACMDriverID = p->pPrevACMDriverID;
732
733     if (p->pPrevACMDriverID)
734         p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
735     if (p->pNextACMDriverID)
736         p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
737
738     pNextACMDriverID = p->pNextACMDriverID;
739
740     if (p->pLocalDriver) MSACM_UnregisterLocalDriver(p->pLocalDriver);
741     HeapFree(MSACM_hHeap, 0, p);
742
743     return pNextACMDriverID;
744 }
745
746 /***********************************************************************
747  *           MSACM_UnregisterAllDrivers()
748  */
749 void MSACM_UnregisterAllDrivers(void)
750 {
751     PWINE_ACMNOTIFYWND panwnd = MSACM_pFirstACMNotifyWnd;
752     PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
753
754     while (p) {
755         MSACM_WriteCache(p);
756         p = MSACM_UnregisterDriver(p);
757     }
758     
759     while (panwnd) {
760         panwnd = MSACM_UnRegisterNotificationWindow(panwnd);
761     }
762 }
763
764 /***********************************************************************
765  *           MSACM_GetObj()
766  */
767 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
768 {
769     PWINE_ACMOBJ        pao = (PWINE_ACMOBJ)hObj;
770
771     if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
772         ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
773         return NULL;
774     return pao;
775 }
776
777 /***********************************************************************
778  *           MSACM_GetDriverID()
779  */
780 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
781 {
782     return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
783 }
784
785 /***********************************************************************
786  *           MSACM_GetDriver()
787  */
788 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
789 {
790     return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
791 }
792
793 /***********************************************************************
794  *           MSACM_GetNotifyWnd()
795  */
796 PWINE_ACMNOTIFYWND MSACM_GetNotifyWnd(HACMDRIVERID hDriver)
797 {
798     return (PWINE_ACMNOTIFYWND)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_NOTIFYWND);
799 }
800
801 /***********************************************************************
802  *           MSACM_GetLocalDriver()
803  */
804 /* 
805 PWINE_ACMLOCALDRIVER MSACM_GetLocalDriver(HACMDRIVER hDriver)
806 {
807     return (PWINE_ACMLOCALDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_LOCALDRIVER);
808 }
809 */
810 #define MSACM_DRIVER_SendMessage(PDRVRINST, msg, lParam1, lParam2) \
811         (PDRVRINST)->pLocalDriver->lpDrvProc((PDRVRINST)->dwDriverID, (HDRVR)(PDRVRINST), msg, lParam1, lParam2)
812
813 /***********************************************************************
814  *           MSACM_Message()
815  */
816 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
817 {
818     PWINE_ACMDRIVER     pad = MSACM_GetDriver(had);
819
820     if (!pad) return MMSYSERR_INVALHANDLE;
821     if (pad->hDrvr) return SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2);
822     if (pad->pLocalDrvrInst) return MSACM_DRIVER_SendMessage(pad->pLocalDrvrInst, uMsg, lParam1, lParam2);
823
824     return MMSYSERR_INVALHANDLE;
825 }
826
827 static PWINE_ACMLOCALDRIVER MSACM_pFirstACMLocalDriver;
828 static PWINE_ACMLOCALDRIVER MSACM_pLastACMLocalDriver;
829
830 PWINE_ACMLOCALDRIVER MSACM_RegisterLocalDriver(HMODULE hModule, DRIVERPROC lpDriverProc)
831 {
832     PWINE_ACMLOCALDRIVER paldrv;
833
834     TRACE("(%p, %p)\n", hModule, lpDriverProc);
835     if (!hModule || !lpDriverProc) return NULL;
836     
837     /* look up previous instance of local driver module */
838     for (paldrv = MSACM_pFirstACMLocalDriver; paldrv; paldrv = paldrv->pNextACMLocalDrv)
839     {
840         if (paldrv->hModule == hModule && paldrv->lpDrvProc == lpDriverProc) return paldrv;
841     }
842
843     paldrv = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMLOCALDRIVER));
844     paldrv->obj.dwType = WINE_ACMOBJ_LOCALDRIVER;
845     paldrv->obj.pACMDriverID = 0;
846     paldrv->hModule = hModule;
847     paldrv->lpDrvProc = lpDriverProc;
848     paldrv->pACMInstList = NULL;
849
850     paldrv->pNextACMLocalDrv = NULL;
851     paldrv->pPrevACMLocalDrv = MSACM_pLastACMLocalDriver;
852     if (MSACM_pLastACMLocalDriver)
853         MSACM_pLastACMLocalDriver->pNextACMLocalDrv = paldrv;
854     MSACM_pLastACMLocalDriver = paldrv;
855     if (!MSACM_pFirstACMLocalDriver)
856         MSACM_pFirstACMLocalDriver = paldrv;
857
858     return paldrv;
859 }
860
861 /**************************************************************************
862  *                      MSACM_GetNumberOfModuleRefs             [internal]
863  *
864  * Returns the number of open drivers which share the same module.
865  * Inspired from implementation in dlls/winmm/driver.c
866  */
867 static unsigned MSACM_GetNumberOfModuleRefs(HMODULE hModule, DRIVERPROC lpDrvProc, WINE_ACMLOCALDRIVERINST ** found)
868 {
869     PWINE_ACMLOCALDRIVER lpDrv;
870     unsigned            count = 0;
871
872     if (found) *found = NULL;
873     for (lpDrv = MSACM_pFirstACMLocalDriver; lpDrv; lpDrv = lpDrv->pNextACMLocalDrv)
874     {
875         if (lpDrv->hModule == hModule && lpDrv->lpDrvProc == lpDrvProc)
876         {
877             PWINE_ACMLOCALDRIVERINST pInst = lpDrv->pACMInstList;
878         
879             while (pInst) {
880                 if (found && !*found) *found = pInst;
881                 count++;
882                 pInst = pInst->pNextACMInst;
883             }
884         }
885     }
886     return count;
887 }
888
889 /**************************************************************************
890  *                              MSACM_RemoveFromList            [internal]
891  *
892  * Generates all the logic to handle driver closure / deletion
893  * Removes a driver struct to the list of open drivers.
894  */
895 static  BOOL    MSACM_RemoveFromList(PWINE_ACMLOCALDRIVERINST lpDrv)
896 {
897     PWINE_ACMLOCALDRIVER pDriverBase = lpDrv->pLocalDriver;
898     PWINE_ACMLOCALDRIVERINST pPrevInst;
899
900     /* last of this driver in list ? */
901     if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, NULL) == 1) {
902         MSACM_DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
903         MSACM_DRIVER_SendMessage(lpDrv, DRV_FREE,    0L, 0L);
904     }
905     
906     pPrevInst = NULL;
907     if (pDriverBase->pACMInstList != lpDrv) {
908         pPrevInst = pDriverBase->pACMInstList;
909         while (pPrevInst && pPrevInst->pNextACMInst != lpDrv)
910             pPrevInst = pPrevInst->pNextACMInst;
911         if (!pPrevInst) {
912             ERR("requested to remove invalid instance %p\n", pPrevInst);
913             return FALSE;
914         }
915     }
916     if (!pPrevInst) {
917         /* first driver instance on list */
918         pDriverBase->pACMInstList = lpDrv->pNextACMInst;
919     } else {
920         pPrevInst->pNextACMInst = lpDrv->pNextACMInst;
921     }
922     return TRUE;
923 }
924
925 /**************************************************************************
926  *                              MSACM_AddToList         [internal]
927  *
928  * Adds a driver struct to the list of open drivers.
929  * Generates all the logic to handle driver creation / open.
930  */
931 static  BOOL    MSACM_AddToList(PWINE_ACMLOCALDRIVERINST lpNewDrv, LPARAM lParam2)
932 {
933     PWINE_ACMLOCALDRIVER pDriverBase = lpNewDrv->pLocalDriver;
934
935     /* first of this driver in list ? */
936     if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, NULL) == 0) {
937         if (MSACM_DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
938             FIXME("DRV_LOAD failed on driver %p\n", lpNewDrv);
939             return FALSE;
940         }
941         /* returned value is not checked */
942         MSACM_DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
943     }
944
945     lpNewDrv->pNextACMInst = NULL;
946     if (pDriverBase->pACMInstList == NULL) {
947         pDriverBase->pACMInstList = lpNewDrv;
948     } else {
949         PWINE_ACMLOCALDRIVERINST lpDrvInst = pDriverBase->pACMInstList;
950     
951         while (lpDrvInst->pNextACMInst != NULL)
952             lpDrvInst = lpDrvInst->pNextACMInst;
953
954         lpDrvInst->pNextACMInst = lpNewDrv;
955     }
956
957     /* Now just open a new instance of a driver on this module */
958     lpNewDrv->dwDriverID = MSACM_DRIVER_SendMessage(lpNewDrv, DRV_OPEN, 0, lParam2);
959
960     if (lpNewDrv->dwDriverID == 0) {
961         FIXME("DRV_OPEN failed on driver %p\n", lpNewDrv);
962         MSACM_RemoveFromList(lpNewDrv);
963         return FALSE;
964     }
965     return TRUE;
966 }
967
968 PWINE_ACMLOCALDRIVERINST MSACM_OpenLocalDriver(PWINE_ACMLOCALDRIVER paldrv, LPARAM lParam2)
969 {
970     PWINE_ACMLOCALDRIVERINST pDrvInst;
971     
972     pDrvInst = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMLOCALDRIVERINST));
973     pDrvInst->pLocalDriver = paldrv;
974     pDrvInst->dwDriverID = 0;
975     pDrvInst->pNextACMInst = NULL;
976     pDrvInst->bSession = FALSE;
977     
978     /* Win32 installable drivers must support a two phase opening scheme:
979      * + first open with NULL as lParam2 (session instance),
980      * + then do a second open with the real non null lParam2)
981      */
982     if (MSACM_GetNumberOfModuleRefs(paldrv->hModule, paldrv->lpDrvProc, NULL) == 0 && lParam2)
983     {
984         PWINE_ACMLOCALDRIVERINST   ret;
985
986         if (!MSACM_AddToList(pDrvInst, 0L))
987         {
988             ERR("load0 failed\n");
989             goto exit;
990         }
991         ret = MSACM_OpenLocalDriver(paldrv, lParam2);
992         if (!ret)
993         {
994             MSACM_CloseLocalDriver(pDrvInst);
995             ERR("load1 failed\n");
996             goto exit;
997         }
998         pDrvInst->bSession = TRUE;
999         return ret;
1000     }
1001     
1002     if (!MSACM_AddToList(pDrvInst, lParam2))
1003     {
1004         ERR("load failed\n");
1005         goto exit;
1006     }
1007
1008     TRACE("=> %p\n", pDrvInst);
1009     return pDrvInst;
1010 exit:
1011     HeapFree(MSACM_hHeap, 0, pDrvInst);
1012     return NULL;
1013 }
1014
1015 LRESULT MSACM_CloseLocalDriver(PWINE_ACMLOCALDRIVERINST paldrv)
1016 {
1017     if (MSACM_RemoveFromList(paldrv)) {
1018         PWINE_ACMLOCALDRIVERINST lpDrv0;
1019         PWINE_ACMLOCALDRIVER pDriverBase = paldrv->pLocalDriver;
1020     
1021         MSACM_DRIVER_SendMessage(paldrv, DRV_CLOSE, 0, 0);
1022         paldrv->dwDriverID = 0;
1023     
1024         if (paldrv->bSession)
1025             ERR("should not directly close session instance (%p)\n", paldrv);
1026
1027         /* if driver has an opened session instance, we have to close it too */
1028         if (MSACM_GetNumberOfModuleRefs(pDriverBase->hModule, pDriverBase->lpDrvProc, &lpDrv0) == 1 &&
1029                 lpDrv0->bSession)
1030         {
1031             MSACM_DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
1032             lpDrv0->dwDriverID = 0;
1033             MSACM_RemoveFromList(lpDrv0);
1034             HeapFree(GetProcessHeap(), 0, lpDrv0);
1035         }
1036
1037         HeapFree(MSACM_hHeap, 0, paldrv);
1038         return TRUE;
1039     }
1040     ERR("unable to close driver instance\n");
1041     return FALSE;
1042 }
1043
1044 PWINE_ACMLOCALDRIVER MSACM_UnregisterLocalDriver(PWINE_ACMLOCALDRIVER paldrv)
1045 {
1046     PWINE_ACMLOCALDRIVER pNextACMLocalDriver;
1047
1048     if (paldrv->pACMInstList) {
1049         ERR("local driver instances still present after closing all drivers - memory leak\n");
1050         return NULL;
1051     }
1052
1053     if (paldrv == MSACM_pFirstACMLocalDriver)
1054         MSACM_pFirstACMLocalDriver = paldrv->pNextACMLocalDrv;
1055     if (paldrv == MSACM_pLastACMLocalDriver)
1056         MSACM_pLastACMLocalDriver = paldrv->pPrevACMLocalDrv;
1057
1058     if (paldrv->pPrevACMLocalDrv)
1059         paldrv->pPrevACMLocalDrv->pNextACMLocalDrv = paldrv->pNextACMLocalDrv;
1060     if (paldrv->pNextACMLocalDrv)
1061         paldrv->pNextACMLocalDrv->pPrevACMLocalDrv = paldrv->pPrevACMLocalDrv;
1062
1063     pNextACMLocalDriver = paldrv->pNextACMLocalDrv;
1064
1065     HeapFree(MSACM_hHeap, 0, paldrv);
1066
1067     return pNextACMLocalDriver;
1068 }