ole32: Fix memory leaks in the storage test.
[wine] / dlls / winmm / mci.c
1 /*
2  * MCI internal functions
3  *
4  * Copyright 1998/1999 Eric Pouech
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /* TODO:
22  * - implement WINMM (32bit) multitasking and use it in all MCI drivers
23  *   instead of the home grown one 
24  * - 16bit mmTaskXXX functions are currently broken because the 16
25  *   loader does not support binary command lines => provide Wine's
26  *   own mmtask.tsk not using binary command line.
27  * - correctly handle the MCI_ALL_DEVICE_ID in functions.
28  * - finish mapping 16 <=> 32 of MCI structures and commands
29  * - implement auto-open feature (ie, when a string command is issued
30  *   for a not yet opened device, MCI automatically opens it) 
31  * - use a default registry setting to replace the [mci] section in
32  *   configuration file (layout of info in registry should be compatible
33  *   with all Windows' version - which use different layouts of course)
34  * - implement automatic open
35  *      + only works on string interface, on regular devices (don't work on all
36  *        nor custom devices)
37  * - command table handling isn't thread safe
38  */
39
40 /* to be cross checked:
41  * - heapalloc for *sizeof(WCHAR) when needed
42  * - size of string in WCHAR or bytes? (#chars for MCI_INFO, #bytes for MCI_SYSINFO)
43  */
44
45 #include "config.h"
46 #include "wine/port.h"
47
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "mmsystem.h"
57 #include "winuser.h"
58 #include "winnls.h"
59 #include "winreg.h"
60 #include "wownt32.h"
61
62 #include "digitalv.h"
63 #include "winemm.h"
64
65 #include "wine/debug.h"
66 #include "wine/unicode.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(mci);
69
70 /* First MCI valid device ID (0 means error) */
71 #define MCI_MAGIC 0x0001
72
73 /* MCI settings */
74 static const WCHAR wszHklmMci  [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
75 static const WCHAR wszNull     [] = {0};
76 static const WCHAR wszAll      [] = {'A','L','L',0};
77 static const WCHAR wszMci      [] = {'M','C','I',0};
78 static const WCHAR wszOpen     [] = {'o','p','e','n',0};
79 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
80
81 static WINE_MCIDRIVER *MciDrivers;
82
83 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
84 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
85
86 /* dup a string and uppercase it */
87 static inline LPWSTR str_dup_upper( LPCWSTR str )
88 {
89     INT len = (strlenW(str) + 1) * sizeof(WCHAR);
90     LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
91     if (p)
92     {
93         memcpy( p, str, len );
94         CharUpperW( p );
95     }
96     return p;
97 }
98
99 /**************************************************************************
100  *                              MCI_GetDriver                   [internal]
101  */
102 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
103 {
104     LPWINE_MCIDRIVER    wmd = 0;
105
106     EnterCriticalSection(&WINMM_cs);
107     for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
108         if (wmd->wDeviceID == wDevID)
109             break;
110     }
111     LeaveCriticalSection(&WINMM_cs);
112     return wmd;
113 }
114
115 /**************************************************************************
116  *                              MCI_GetDriverFromString         [internal]
117  */
118 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
119 {
120     LPWINE_MCIDRIVER    wmd;
121     UINT                ret = 0;
122
123     if (!lpstrName)
124         return 0;
125
126     if (!strcmpiW(lpstrName, wszAll))
127         return MCI_ALL_DEVICE_ID;
128
129     EnterCriticalSection(&WINMM_cs);
130     for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
131         if (wmd->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
132             ret = wmd->wDeviceID;
133             break;
134         }
135         if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
136             ret = wmd->wDeviceID;
137             break;
138         }
139         if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
140             ret = wmd->wDeviceID;
141             break;
142         }
143     }
144     LeaveCriticalSection(&WINMM_cs);
145
146     return ret;
147 }
148
149 /**************************************************************************
150  *                      MCI_MessageToString                     [internal]
151  */
152 const char* MCI_MessageToString(UINT wMsg)
153 {
154 #define CASE(s) case (s): return #s
155
156     switch (wMsg) {
157         CASE(DRV_LOAD);
158         CASE(DRV_ENABLE);
159         CASE(DRV_OPEN);
160         CASE(DRV_CLOSE);
161         CASE(DRV_DISABLE);
162         CASE(DRV_FREE);
163         CASE(DRV_CONFIGURE);
164         CASE(DRV_QUERYCONFIGURE);
165         CASE(DRV_INSTALL);
166         CASE(DRV_REMOVE);
167         CASE(DRV_EXITSESSION);
168         CASE(DRV_EXITAPPLICATION);
169         CASE(DRV_POWER);
170         CASE(MCI_BREAK);
171         CASE(MCI_CLOSE);
172         CASE(MCI_CLOSE_DRIVER);
173         CASE(MCI_COPY);
174         CASE(MCI_CUE);
175         CASE(MCI_CUT);
176         CASE(MCI_DELETE);
177         CASE(MCI_ESCAPE);
178         CASE(MCI_FREEZE);
179         CASE(MCI_PAUSE);
180         CASE(MCI_PLAY);
181         CASE(MCI_GETDEVCAPS);
182         CASE(MCI_INFO);
183         CASE(MCI_LOAD);
184         CASE(MCI_OPEN);
185         CASE(MCI_OPEN_DRIVER);
186         CASE(MCI_PASTE);
187         CASE(MCI_PUT);
188         CASE(MCI_REALIZE);
189         CASE(MCI_RECORD);
190         CASE(MCI_RESUME);
191         CASE(MCI_SAVE);
192         CASE(MCI_SEEK);
193         CASE(MCI_SET);
194         CASE(MCI_SPIN);
195         CASE(MCI_STATUS);
196         CASE(MCI_STEP);
197         CASE(MCI_STOP);
198         CASE(MCI_SYSINFO);
199         CASE(MCI_UNFREEZE);
200         CASE(MCI_UPDATE);
201         CASE(MCI_WHERE);
202         CASE(MCI_WINDOW);
203         /* constants for digital video */
204         CASE(MCI_CAPTURE);
205         CASE(MCI_MONITOR);
206         CASE(MCI_RESERVE);
207         CASE(MCI_SETAUDIO);
208         CASE(MCI_SIGNAL);
209         CASE(MCI_SETVIDEO);
210         CASE(MCI_QUALITY);
211         CASE(MCI_LIST);
212         CASE(MCI_UNDO);
213         CASE(MCI_CONFIGURE);
214         CASE(MCI_RESTORE);
215 #undef CASE
216     default:
217         return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
218     }
219 }
220
221 LPWSTR MCI_strdupAtoW( LPCSTR str )
222 {
223     LPWSTR ret;
224     INT len;
225
226     if (!str) return NULL;
227     len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
228     ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
229     if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
230     return ret;
231 }
232
233 LPSTR MCI_strdupWtoA( LPCWSTR str )
234 {
235     LPSTR ret;
236     INT len;
237
238     if (!str) return NULL;
239     len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
240     ret = HeapAlloc( GetProcessHeap(), 0, len );
241     if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
242     return ret;
243 }
244
245 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
246 {
247     if (msg < DRV_RESERVED) return 0;
248
249     switch (msg)
250     {
251     case MCI_CLOSE:
252     case MCI_CONFIGURE:
253     case MCI_PLAY:
254     case MCI_SEEK:
255     case MCI_STOP:
256     case MCI_PAUSE:
257     case MCI_GETDEVCAPS:
258     case MCI_SPIN:
259     case MCI_SET:
260     case MCI_STEP:
261     case MCI_RECORD:
262     case MCI_BREAK:
263     case MCI_SOUND:
264     case MCI_STATUS:
265     case MCI_CUE:
266     case MCI_REALIZE:
267     case MCI_PUT:
268     case MCI_WHERE:
269     case MCI_FREEZE:
270     case MCI_UNFREEZE:
271     case MCI_CUT:
272     case MCI_COPY:
273     case MCI_PASTE:
274     case MCI_UPDATE:
275     case MCI_RESUME:
276     case MCI_DELETE:
277     case MCI_MONITOR:
278     case MCI_SETAUDIO:
279     case MCI_SIGNAL:
280     case MCI_SETVIDEO:
281     case MCI_LIST:
282         return 0;
283
284     case MCI_OPEN:
285         {
286             MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
287             MCI_OPEN_PARMSW *mci_openW;
288             DWORD_PTR *ptr;
289
290             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
291             if (!ptr) return -1;
292
293             *ptr++ = *dwParam2; /* save the previous pointer */
294             *dwParam2 = (DWORD_PTR)ptr;
295             mci_openW = (MCI_OPEN_PARMSW *)ptr;
296
297             if (dwParam1 & MCI_NOTIFY)
298                 mci_openW->dwCallback = mci_openA->dwCallback;
299
300             if (dwParam1 & MCI_OPEN_TYPE)
301             {
302                 if (dwParam1 & MCI_OPEN_TYPE_ID)
303                     mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
304                 else
305                     mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
306             }
307             if (dwParam1 & MCI_OPEN_ELEMENT)
308             {
309                 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
310                     mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
311                 else
312                     mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
313             }
314             if (dwParam1 & MCI_OPEN_ALIAS)
315                 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
316             /* FIXME: this is only needed for specific types of MCI devices, and
317              * may cause a segfault if the two DWORD:s don't exist at the end of 
318              * mci_openA
319              */
320             memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
321         }
322         return 1;
323
324     case MCI_WINDOW:
325         if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
326         {
327             MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
328             MCI_ANIM_WINDOW_PARMSW *mci_windowW;
329
330             mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
331             if (!mci_windowW) return -1;
332
333             *dwParam2 = (DWORD_PTR)mci_windowW;
334
335             mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
336
337             if (dwParam1 & MCI_NOTIFY)
338                 mci_windowW->dwCallback = mci_windowA->dwCallback;
339             if (dwParam1 & MCI_ANIM_WINDOW_HWND)
340                 mci_windowW->hWnd = mci_windowA->hWnd;
341             if (dwParam1 & MCI_ANIM_WINDOW_STATE)
342                 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
343
344             return 1;
345         }
346         return 0;
347
348     case MCI_SYSINFO:
349         {
350             MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
351             MCI_SYSINFO_PARMSW *mci_sysinfoW;
352             DWORD_PTR *ptr;
353
354             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
355             if (!ptr) return -1;
356
357             *ptr++ = *dwParam2; /* save the previous pointer */
358             *dwParam2 = (DWORD_PTR)ptr;
359             mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
360
361             if (dwParam1 & MCI_NOTIFY)
362                 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
363
364             mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
365             mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
366             mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
367             mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
368             return 1;
369         }
370     case MCI_INFO:
371         {
372             MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
373             MCI_INFO_PARMSW *mci_infoW;
374             DWORD_PTR *ptr;
375
376             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
377             if (!ptr) return -1;
378
379             *ptr++ = *dwParam2; /* save the previous pointer */
380             *dwParam2 = (DWORD_PTR)ptr;
381             mci_infoW = (MCI_INFO_PARMSW *)ptr;
382
383             if (dwParam1 & MCI_NOTIFY)
384                 mci_infoW->dwCallback = mci_infoA->dwCallback;
385
386             mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
387             mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
388             return 1;
389         }
390     case MCI_SAVE:
391         {
392             MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
393             MCI_SAVE_PARMSW *mci_saveW;
394
395             mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
396             if (!mci_saveW) return -1;
397
398             *dwParam2 = (DWORD_PTR)mci_saveW;
399             if (dwParam1 & MCI_NOTIFY)
400                 mci_saveW->dwCallback = mci_saveA->dwCallback;
401             mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
402             return 1;
403         }
404     case MCI_LOAD:
405         {
406             MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
407             MCI_LOAD_PARMSW *mci_loadW;
408
409             mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
410             if (!mci_loadW) return -1;
411
412             *dwParam2 = (DWORD_PTR)mci_loadW;
413             if (dwParam1 & MCI_NOTIFY)
414                 mci_loadW->dwCallback = mci_loadA->dwCallback;
415             mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
416             return 1;
417         }
418
419     case MCI_ESCAPE:
420         {
421             MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
422             MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
423
424             mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
425             if (!mci_vd_escapeW) return -1;
426
427             *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
428             if (dwParam1 & MCI_NOTIFY)
429                 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
430             mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
431             return 1;
432         }
433     default:
434         FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
435         return -1;
436     }
437 }
438
439 static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
440                               DWORD result)
441 {
442     switch (msg)
443     {
444     case MCI_OPEN:
445         {
446             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
447             MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
448             MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);
449
450             mci_openA->wDeviceID = mci_openW->wDeviceID;
451
452             if (dwParam1 & MCI_OPEN_TYPE)
453             {
454                 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
455                     HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
456             }
457             if (dwParam1 & MCI_OPEN_ELEMENT)
458             {
459                 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
460                     HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
461             }
462             if (dwParam1 & MCI_OPEN_ALIAS)
463                 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
464             HeapFree(GetProcessHeap(), 0, ptr);
465         }
466         break;
467     case MCI_WINDOW:
468         if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
469         {
470             MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
471
472             HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
473             HeapFree(GetProcessHeap(), 0, mci_windowW);
474         }
475         break;
476
477     case MCI_SYSINFO:
478         {
479             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
480             MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
481             MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);
482
483             if (!result)
484             {
485                 mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
486                 mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
487                 if (dwParam1 & MCI_SYSINFO_QUANTITY)
488                     *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
489                 else
490                     WideCharToMultiByte(CP_ACP, 0,
491                                         mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
492                                         mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
493                                         NULL, NULL);
494             }
495
496             HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
497             HeapFree(GetProcessHeap(), 0, ptr);
498         }
499         break;
500     case MCI_INFO:
501         {
502             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
503             MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
504             MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);
505
506             if (!result)
507             {
508                 WideCharToMultiByte(CP_ACP, 0,
509                                     mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
510                                     mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
511                                     NULL, NULL);
512             }
513
514             HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
515             HeapFree(GetProcessHeap(), 0, ptr);
516         }
517         break;
518     case MCI_SAVE:
519         {
520             MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;
521
522             HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
523             HeapFree(GetProcessHeap(), 0, mci_saveW);
524         }
525         break;
526     case MCI_LOAD:
527         {
528             MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;
529
530             HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
531             HeapFree(GetProcessHeap(), 0, mci_loadW);
532         }
533         break;
534     case MCI_ESCAPE:
535         {
536             MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
537
538             HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
539             HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
540         }
541         break;
542
543     default:
544         FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
545         break;
546     }
547
548     return result;
549 }
550
551 /**************************************************************************
552  *                              MCI_GetDevTypeFromFileName      [internal]
553  */
554 static  DWORD   MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
555 {
556     LPCWSTR     tmp;
557     HKEY        hKey;
558     static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
559                                  'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
560                                  'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
561     if ((tmp = strrchrW(fileName, '.'))) {
562         if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
563                            0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
564             DWORD dwLen = len;
565             LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen ); 
566             RegCloseKey( hKey );
567             if (lRet == ERROR_SUCCESS) return 0;
568         }
569         TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
570     }
571     return MCIERR_EXTENSION_NOT_FOUND;
572 }
573
574 #define MAX_MCICMDTABLE                 20
575 #define MCI_COMMAND_TABLE_NOT_LOADED    0xFFFE
576
577 typedef struct tagWINE_MCICMDTABLE {
578     UINT                uDevType;
579     HGLOBAL             hMem;
580     const BYTE*         lpTable;
581     UINT                nVerbs;         /* number of verbs in command table */
582     LPCWSTR*            aVerbs;         /* array of verbs to speed up the verb look up process */
583 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
584
585 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
586
587 /**************************************************************************
588  *                              MCI_IsCommandTableValid         [internal]
589  */
590 static  BOOL            MCI_IsCommandTableValid(UINT uTbl)
591 {
592     const BYTE* lmem;
593     LPCWSTR     str;
594     DWORD       flg;
595     WORD        eid;
596     int         idx = 0;
597     BOOL        inCst = FALSE;
598
599     TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
600           uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
601
602     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
603         return FALSE;
604
605     lmem = S_MciCmdTable[uTbl].lpTable;
606     do {
607         str = (LPCWSTR)lmem;
608         lmem += (strlenW(str) + 1) * sizeof(WCHAR);
609         flg = *(const DWORD*)lmem;
610         eid = *(const WORD*)(lmem + sizeof(DWORD));
611         lmem += sizeof(DWORD) + sizeof(WORD);
612         idx ++;
613         /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
614         switch (eid) {
615         case MCI_COMMAND_HEAD:          if (!*str || !flg) return FALSE; idx = 0;               break;  /* check unicity of str in table */
616         case MCI_STRING:                if (inCst) return FALSE;                                break;
617         case MCI_INTEGER:               if (!*str) return FALSE;                                break;
618         case MCI_END_COMMAND:           if (*str || flg || idx == 0) return FALSE; idx = 0;     break;
619         case MCI_RETURN:                if (*str || idx != 1) return FALSE;                     break;
620         case MCI_FLAG:                  if (!*str) return FALSE;                                break;
621         case MCI_END_COMMAND_LIST:      if (*str || flg) return FALSE;  idx = 0;                break;
622         case MCI_RECT:                  if (!*str || inCst) return FALSE;                       break;
623         case MCI_CONSTANT:              if (inCst) return FALSE; inCst = TRUE;                  break;
624         case MCI_END_CONSTANT:          if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
625         default:                        return FALSE;
626         }
627     } while (eid != MCI_END_COMMAND_LIST);
628     return TRUE;
629 }
630
631 /**************************************************************************
632  *                              MCI_DumpCommandTable            [internal]
633  */
634 static  BOOL            MCI_DumpCommandTable(UINT uTbl)
635 {
636     const BYTE* lmem;
637     LPCWSTR     str;
638     DWORD       flg;
639     WORD        eid;
640
641     if (!MCI_IsCommandTableValid(uTbl)) {
642         ERR("Ooops: %d is not valid\n", uTbl);
643         return FALSE;
644     }
645
646     lmem = S_MciCmdTable[uTbl].lpTable;
647     do {
648         do {
649             str = (LPCWSTR)lmem;
650             lmem += (strlenW(str) + 1) * sizeof(WCHAR);
651             flg = *(const DWORD*)lmem;
652             eid = *(const WORD*)(lmem + sizeof(DWORD));
653             /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
654             lmem += sizeof(DWORD) + sizeof(WORD);
655         } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
656         /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
657     } while (eid != MCI_END_COMMAND_LIST);
658     return TRUE;
659 }
660
661
662 /**************************************************************************
663  *                              MCI_GetCommandTable             [internal]
664  */
665 static  UINT            MCI_GetCommandTable(UINT uDevType)
666 {
667     UINT        uTbl;
668     WCHAR       buf[32];
669     LPCWSTR     str = NULL;
670
671     /* first look up existing for existing devType */
672     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
673         if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
674             return uTbl;
675     }
676
677     /* well try to load id */
678     if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
679         if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
680             str = buf;
681         }
682     } else if (uDevType == 0) {
683         static const WCHAR wszCore[] = {'C','O','R','E',0};
684         str = wszCore;
685     }
686     uTbl = MCI_NO_COMMAND_TABLE;
687     if (str) {
688         HRSRC   hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
689         HANDLE  hMem = 0;
690
691         if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
692         if (hMem) {
693             uTbl = MCI_SetCommandTable(hMem, uDevType);
694         } else {
695             WARN("No command table found in resource %p[%s]\n",
696                  hWinMM32Instance, debugstr_w(str));
697         }
698     }
699     TRACE("=> %d\n", uTbl);
700     return uTbl;
701 }
702
703 /**************************************************************************
704  *                              MCI_SetCommandTable             [internal]
705  */
706 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
707 {
708     int                 uTbl;
709     static      BOOL    bInitDone = FALSE;
710
711     /* <HACK>
712      * The CORE command table must be loaded first, so that MCI_GetCommandTable()
713      * can be called with 0 as a uDevType to retrieve it.
714      * </HACK>
715      */
716     if (!bInitDone) {
717         bInitDone = TRUE;
718         MCI_GetCommandTable(0);
719     }
720     TRACE("(%p, %u)\n", hMem, uDevType);
721     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
722         if (!S_MciCmdTable[uTbl].lpTable) {
723             const BYTE* lmem;
724             LPCWSTR     str;
725             WORD        eid;
726             WORD        count;
727
728             S_MciCmdTable[uTbl].uDevType = uDevType;
729             S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
730             S_MciCmdTable[uTbl].hMem = hMem;
731
732             if (TRACE_ON(mci)) {
733                 MCI_DumpCommandTable(uTbl);
734             }
735
736             /* create the verbs table */
737             /* get # of entries */
738             lmem = S_MciCmdTable[uTbl].lpTable;
739             count = 0;
740             do {
741                 str = (LPCWSTR)lmem;
742                 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
743                 eid = *(const WORD*)(lmem + sizeof(DWORD));
744                 lmem += sizeof(DWORD) + sizeof(WORD);
745                 if (eid == MCI_COMMAND_HEAD)
746                     count++;
747             } while (eid != MCI_END_COMMAND_LIST);
748
749             S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
750             S_MciCmdTable[uTbl].nVerbs = count;
751
752             lmem = S_MciCmdTable[uTbl].lpTable;
753             count = 0;
754             do {
755                 str = (LPCWSTR)lmem;
756                 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
757                 eid = *(const WORD*)(lmem + sizeof(DWORD));
758                 lmem += sizeof(DWORD) + sizeof(WORD);
759                 if (eid == MCI_COMMAND_HEAD)
760                     S_MciCmdTable[uTbl].aVerbs[count++] = str;
761             } while (eid != MCI_END_COMMAND_LIST);
762             /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
763             return uTbl;
764         }
765     }
766
767     return MCI_NO_COMMAND_TABLE;
768 }
769
770 /**************************************************************************
771  *                              MCI_UnLoadMciDriver             [internal]
772  */
773 static  BOOL    MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
774 {
775     LPWINE_MCIDRIVER*           tmp;
776
777     if (!wmd)
778         return TRUE;
779
780     CloseDriver(wmd->hDriver, 0, 0);
781
782     if (wmd->dwPrivate != 0)
783         WARN("Unloading mci driver with non nul dwPrivate field\n");
784
785     EnterCriticalSection(&WINMM_cs);
786     for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
787         if (*tmp == wmd) {
788             *tmp = wmd->lpNext;
789             break;
790         }
791     }
792     LeaveCriticalSection(&WINMM_cs);
793
794     HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
795     HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
796     HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
797
798     HeapFree(GetProcessHeap(), 0, wmd);
799     return TRUE;
800 }
801
802 /**************************************************************************
803  *                              MCI_OpenMciDriver               [internal]
804  */
805 static  BOOL    MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
806 {
807     WCHAR       libName[128];
808
809     if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
810         return FALSE;
811
812     /* First load driver */
813     wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
814     return wmd->hDriver != NULL;
815 }
816
817 /**************************************************************************
818  *                              MCI_LoadMciDriver               [internal]
819  */
820 static  DWORD   MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
821 {
822     LPWSTR                      strDevTyp = str_dup_upper(_strDevTyp);
823     LPWINE_MCIDRIVER            wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
824     MCI_OPEN_DRIVER_PARMSW      modp;
825     DWORD                       dwRet = 0;
826
827     if (!wmd || !strDevTyp) {
828         dwRet = MCIERR_OUT_OF_MEMORY;
829         goto errCleanUp;
830     }
831
832     wmd->lpfnYieldProc = MCI_DefYieldProc;
833     wmd->dwYieldData = VK_CANCEL;
834     wmd->CreatorThread = GetCurrentThreadId();
835
836     EnterCriticalSection(&WINMM_cs);
837     /* wmd must be inserted in list before sending opening the driver, because it
838      * may want to lookup at wDevID
839      */
840     wmd->lpNext = MciDrivers;
841     MciDrivers = wmd;
842
843     for (modp.wDeviceID = MCI_MAGIC;
844          MCI_GetDriver(modp.wDeviceID) != 0;
845          modp.wDeviceID++);
846
847     wmd->wDeviceID = modp.wDeviceID;
848
849     LeaveCriticalSection(&WINMM_cs);
850
851     TRACE("wDevID=%04X\n", modp.wDeviceID);
852
853     modp.lpstrParams = NULL;
854
855     if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
856         /* silence warning if all is used... some bogus program use commands like
857          * 'open all'...
858          */
859         if (strcmpiW(strDevTyp, wszAll) == 0) {
860             dwRet = MCIERR_CANNOT_USE_ALL;
861         } else {
862             FIXME("Couldn't load driver for type %s.\n",
863                   debugstr_w(strDevTyp));
864             dwRet = MCIERR_DEVICE_NOT_INSTALLED;
865         }
866         goto errCleanUp;
867     }
868
869     /* FIXME: should also check that module's description is of the form
870      * MODULENAME:[MCI] comment
871      */
872
873     /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
874     wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
875     wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
876
877     TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
878           wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
879
880     wmd->lpstrDeviceType = strDevTyp;
881     wmd->wType = modp.wType;
882
883     TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
884           modp.wDeviceID, modp.wType, modp.wDeviceID);
885     *lpwmd = wmd;
886     return 0;
887 errCleanUp:
888     MCI_UnLoadMciDriver(wmd);
889     HeapFree(GetProcessHeap(), 0, strDevTyp);
890     *lpwmd = 0;
891     return dwRet;
892 }
893
894 /**************************************************************************
895  *                      MCI_SendCommandFrom32                   [internal]
896  */
897 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
898 {
899     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
900     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
901
902     if (wmd) {
903         dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
904     }
905     return dwRet;
906 }
907
908 /**************************************************************************
909  *                      MCI_FinishOpen                          [internal]
910  */
911 static  DWORD   MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
912                                DWORD dwParam)
913 {
914     if (dwParam & MCI_OPEN_ELEMENT)
915     {
916         wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
917         strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
918     }
919     if (dwParam & MCI_OPEN_ALIAS)
920     {
921         wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
922         strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
923     }
924     lpParms->wDeviceID = wmd->wDeviceID;
925
926     return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
927                                  (DWORD_PTR)lpParms);
928 }
929
930 /**************************************************************************
931  *                              MCI_FindCommand         [internal]
932  */
933 static  LPCWSTR         MCI_FindCommand(UINT uTbl, LPCWSTR verb)
934 {
935     UINT        idx;
936
937     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
938         return NULL;
939
940     /* another improvement would be to have the aVerbs array sorted,
941      * so that we could use a dichotomic search on it, rather than this dumb
942      * array look up
943      */
944     for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
945         if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
946             return S_MciCmdTable[uTbl].aVerbs[idx];
947     }
948
949     return NULL;
950 }
951
952 /**************************************************************************
953  *                              MCI_GetReturnType               [internal]
954  */
955 static  DWORD           MCI_GetReturnType(LPCWSTR lpCmd)
956 {
957     lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
958     if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
959         return *(const DWORD*)(lpCmd + 1);
960     }
961     return 0L;
962 }
963
964 /**************************************************************************
965  *                              MCI_GetMessage                  [internal]
966  */
967 static  WORD            MCI_GetMessage(LPCWSTR lpCmd)
968 {
969     return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
970 }
971
972 /**************************************************************************
973  *                              MCI_GetDWord                    [internal]
974  */
975 static  BOOL            MCI_GetDWord(LPDWORD data, LPWSTR* ptr)
976 {
977     DWORD       val;
978     LPWSTR      ret;
979
980     val = strtoulW(*ptr, &ret, 0);
981
982     switch (*ret) {
983     case '\0':  break;
984     case ' ':   ret++; break;
985     default:    return FALSE;
986     }
987
988     *data |= val;
989     *ptr = ret;
990     return TRUE;
991 }
992
993 /**************************************************************************
994  *                              MCI_GetString           [internal]
995  */
996 static  DWORD   MCI_GetString(LPWSTR* str, LPWSTR* args)
997 {
998     LPWSTR      ptr = *args;
999
1000     /* see if we have a quoted string */
1001     if (*ptr == '"') {
1002         ptr = strchrW(*str = ptr + 1, '"');
1003         if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1004         /* FIXME: shall we escape \" from string ?? */
1005         if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1006         *ptr++ = '\0'; /* remove trailing " */
1007         if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1008     } else {
1009         ptr = strchrW(ptr, ' ');
1010
1011         if (ptr) {
1012             *ptr++ = '\0';
1013         } else {
1014             ptr = *args + strlenW(*args);
1015         }
1016         *str = *args;
1017     }
1018
1019     *args = ptr;
1020     return 0;
1021 }
1022
1023 #define MCI_DATA_SIZE   16
1024
1025 /**************************************************************************
1026  *                              MCI_ParseOptArgs                [internal]
1027  */
1028 static  DWORD   MCI_ParseOptArgs(LPDWORD data, int _offset, LPCWSTR lpCmd,
1029                                  LPWSTR args, LPDWORD dwFlags)
1030 {
1031     int         len, offset;
1032     const char* lmem;
1033     LPCWSTR     str;
1034     DWORD       dwRet, flg, cflg = 0;
1035     WORD        eid;
1036     BOOL        inCst, found;
1037
1038     /* loop on arguments */
1039     while (*args) {
1040         lmem = (const char*)lpCmd;
1041         found = inCst = FALSE;
1042         offset = _offset;
1043
1044         /* skip any leading white space(s) */
1045         while (*args == ' ') args++;
1046         TRACE("args=%s offset=%d\n", debugstr_w(args), offset);
1047
1048         do { /* loop on options for command table for the requested verb */
1049             str = (LPCWSTR)lmem;
1050             lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1051             flg = *(const DWORD*)lmem;
1052             eid = *(const WORD*)(lmem + sizeof(DWORD));
1053             lmem += sizeof(DWORD) + sizeof(WORD);
1054             /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1055
1056             switch (eid) {
1057             case MCI_CONSTANT:
1058                 inCst = TRUE;   cflg = flg;     break;
1059             case MCI_END_CONSTANT:
1060                 /* there may be additional integral values after flag in constant */
1061                 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1062                     *dwFlags |= cflg;
1063                 }
1064                 inCst = FALSE;  cflg = 0;
1065                 break;
1066             }
1067
1068             if (strncmpiW(args, str, len) == 0 &&
1069                 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1070                 /* store good values into data[] */
1071                 args += len;
1072                 while (*args == ' ') args++;
1073                 found = TRUE;
1074
1075                 switch (eid) {
1076                 case MCI_COMMAND_HEAD:
1077                 case MCI_RETURN:
1078                 case MCI_END_COMMAND:
1079                 case MCI_END_COMMAND_LIST:
1080                 case MCI_CONSTANT:      /* done above */
1081                 case MCI_END_CONSTANT:  /* done above */
1082                     break;
1083                 case MCI_FLAG:
1084                     *dwFlags |= flg;
1085                     break;
1086                 case MCI_INTEGER:
1087                     if (inCst) {
1088                         data[offset] |= flg;
1089                         *dwFlags |= cflg;
1090                         inCst = FALSE;
1091                     } else {
1092                         *dwFlags |= flg;
1093                         if (!MCI_GetDWord(&(data[offset]), &args)) {
1094                             return MCIERR_BAD_INTEGER;
1095                         }
1096                     }
1097                     break;
1098                 case MCI_RECT:
1099                     /* store rect in data (offset..offset+3) */
1100                     *dwFlags |= flg;
1101                     if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1102                         !MCI_GetDWord(&(data[offset+1]), &args) ||
1103                         !MCI_GetDWord(&(data[offset+2]), &args) ||
1104                         !MCI_GetDWord(&(data[offset+3]), &args)) {
1105                         ERR("Bad rect %s\n", debugstr_w(args));
1106                         return MCIERR_BAD_INTEGER;
1107                     }
1108                     break;
1109                 case MCI_STRING:
1110                     *dwFlags |= flg;
1111                     if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1112                         return dwRet;
1113                     break;
1114                 default:        ERR("oops\n");
1115                 }
1116                 /* exit inside while loop, except if just entered in constant area definition */
1117                 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1118             } else {
1119                 /* have offset incremented if needed */
1120                 switch (eid) {
1121                 case MCI_COMMAND_HEAD:
1122                 case MCI_RETURN:
1123                 case MCI_END_COMMAND:
1124                 case MCI_END_COMMAND_LIST:
1125                 case MCI_CONSTANT:
1126                 case MCI_FLAG:                  break;
1127                 case MCI_INTEGER:               if (!inCst) offset++;   break;
1128                 case MCI_END_CONSTANT:
1129                 case MCI_STRING:                offset++; break;
1130                 case MCI_RECT:                  offset += 4; break;
1131                 default:                        ERR("oops\n");
1132                 }
1133             }
1134         } while (eid != MCI_END_COMMAND);
1135         if (!found) {
1136             WARN("Optarg %s not found\n", debugstr_w(args));
1137             return MCIERR_UNRECOGNIZED_COMMAND;
1138         }
1139         if (offset == MCI_DATA_SIZE) {
1140             ERR("Internal data[] buffer overflow\n");
1141             return MCIERR_PARSER_INTERNAL;
1142         }
1143     }
1144     return 0;
1145 }
1146
1147 /**************************************************************************
1148  *                              MCI_HandleReturnValues  [internal]
1149  */
1150 static  DWORD   MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType, 
1151                                        LPDWORD data, LPWSTR lpstrRet, UINT uRetLen)
1152 {
1153     static const WCHAR wszLd  [] = {'%','l','d',0};
1154     static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
1155     static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1156     static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1157
1158     if (lpstrRet) {
1159         switch (retType) {
1160         case 0: /* nothing to return */
1161             break;
1162         case MCI_INTEGER:
1163             switch (dwRet & 0xFFFF0000ul) {
1164             case 0:
1165             case MCI_INTEGER_RETURNED:
1166                 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1167                 break;
1168             case MCI_RESOURCE_RETURNED:
1169                 /* return string which ID is HIWORD(data[1]),
1170                  * string is loaded from mmsystem.dll */
1171                 LoadStringW(hWinMM32Instance, HIWORD(data[1]), lpstrRet, uRetLen);
1172                 break;
1173             case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1174                 /* return string which ID is HIWORD(data[1]),
1175                  * string is loaded from driver */
1176                 /* FIXME: this is wrong for a 16 bit handle */
1177                 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1178                             HIWORD(data[1]), lpstrRet, uRetLen);
1179                 break;
1180             case MCI_COLONIZED3_RETURN:
1181                 snprintfW(lpstrRet, uRetLen, wszCol3,
1182                           LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1183                           LOBYTE(HIWORD(data[1])));
1184                 break;
1185             case MCI_COLONIZED4_RETURN:
1186                 snprintfW(lpstrRet, uRetLen, wszCol4,
1187                           LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1188                           LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1189                 break;
1190             default:    ERR("Ooops (%04X)\n", HIWORD(dwRet));
1191             }
1192             break;
1193         case MCI_STRING:
1194             switch (dwRet & 0xFFFF0000ul) {
1195             case 0:
1196                 /* nothing to do data[1] == lpstrRet */
1197                 break;
1198             case MCI_INTEGER_RETURNED:
1199                 data[1] = *(LPDWORD)lpstrRet;
1200                 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1201                 break;
1202             default:
1203                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1204                 break;
1205             }
1206             break;
1207         case MCI_RECT:
1208             if (dwRet & 0xFFFF0000ul)
1209                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1210             snprintfW(lpstrRet, uRetLen, wszLd4,
1211                       data[1], data[2], data[3], data[4]);
1212             break;
1213         default:                ERR("oops\n");
1214         }
1215     }
1216     return LOWORD(dwRet);
1217 }
1218
1219 /**************************************************************************
1220  *                              mciSendStringW          [WINMM.@]
1221  */
1222 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1223                             UINT uRetLen, HWND hwndCallback)
1224 {
1225     LPWSTR              verb, dev, args;
1226     LPWINE_MCIDRIVER    wmd = 0;
1227     DWORD               dwFlags = 0, dwRet = 0;
1228     int                 offset = 0;
1229     DWORD               data[MCI_DATA_SIZE];
1230     DWORD               retType;
1231     LPCWSTR             lpCmd = 0;
1232     static const WCHAR  wszNew[] = {'n','e','w',0};
1233     static const WCHAR  wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1234     static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1235
1236     TRACE("(%s, %p, %d, %p)\n", 
1237           debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1238
1239     /* format is <command> <device> <optargs> */
1240     if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1241         return MCIERR_OUT_OF_MEMORY;
1242     strcpyW( verb, lpstrCommand );
1243     CharLowerW(verb);
1244
1245     memset(data, 0, sizeof(data));
1246
1247     if (!(args = strchrW(verb, ' '))) {
1248         dwRet = MCIERR_MISSING_DEVICE_NAME;
1249         goto errCleanUp;
1250     }
1251     *args++ = '\0';
1252     if ((dwRet = MCI_GetString(&dev, &args))) {
1253         goto errCleanUp;
1254     }
1255
1256     /* Determine devType from open */
1257     if (!strcmpW(verb, wszOpen)) {
1258         LPWSTR  devType, tmp;
1259         WCHAR   buf[128];
1260
1261         /* case dev == 'new' has to be handled */
1262         if (!strcmpW(dev, wszNew)) {
1263             dev = 0;
1264             if ((devType = strstrW(args, wszTypeS)) != NULL) {
1265                 devType += 5;
1266                 tmp = strchrW(devType, ' ');
1267                 if (tmp) *tmp = '\0';
1268                 devType = str_dup_upper(devType);
1269                 if (tmp) *tmp = ' ';
1270                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1271             } else {
1272                 WARN("open new requires device type\n");
1273                 dwRet = MCIERR_MISSING_DEVICE_NAME;
1274                 goto errCleanUp;
1275             }
1276         } else if ((devType = strchrW(dev, '!')) != NULL) {
1277             *devType++ = '\0';
1278             tmp = devType; devType = dev; dev = tmp;
1279
1280             dwFlags |= MCI_OPEN_TYPE;
1281             data[2] = (DWORD)devType;
1282             devType = str_dup_upper(devType);
1283             dwFlags |= MCI_OPEN_ELEMENT;
1284             data[3] = (DWORD)dev;
1285         } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1286             /* this is the name of a mci driver's type */
1287             tmp = strchrW(dev, ' ');
1288             if (tmp) *tmp = '\0';
1289             data[2] = (DWORD)dev;
1290             devType = str_dup_upper(dev);
1291             if (tmp) *tmp = ' ';
1292             dwFlags |= MCI_OPEN_TYPE;
1293         } else {
1294             if ((devType = strstrW(args, wszTypeS)) != NULL) {
1295                 devType += 5;
1296                 tmp = strchrW(devType, ' ');
1297                 if (tmp) *tmp = '\0';
1298                 devType = str_dup_upper(devType);
1299                 if (tmp) *tmp = ' ';
1300                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1301             } else {
1302                 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1303                     goto errCleanUp;
1304
1305                 devType = str_dup_upper(buf);
1306             }
1307             dwFlags |= MCI_OPEN_ELEMENT;
1308             data[3] = (DWORD)dev;
1309         }
1310         if (!strstrW(args, wszSAliasS) && !dev) {
1311             dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1312             goto errCleanUp;
1313         }
1314
1315         dwRet = MCI_LoadMciDriver(devType, &wmd);
1316         if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1317             dwRet = MCIERR_INVALID_DEVICE_NAME;
1318         HeapFree(GetProcessHeap(), 0, devType);
1319         if (dwRet) {
1320             MCI_UnLoadMciDriver(wmd);
1321             goto errCleanUp;
1322         }
1323     } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1324         /* auto open */
1325         static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1326         WCHAR   buf[128];
1327         sprintfW(buf, wszOpenWait, dev);
1328
1329         if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1330             goto errCleanUp;
1331
1332         wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1333         if (!wmd) {
1334             /* FIXME: memory leak, MCI driver is not closed */
1335             dwRet = MCIERR_INVALID_DEVICE_ID;
1336             goto errCleanUp;
1337         }
1338     }
1339
1340     /* get the verb in the different command tables */
1341     if (wmd) {
1342         /* try the device specific command table */
1343         lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1344         if (!lpCmd) {
1345             /* try the type specific command table */
1346             if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1347                 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1348             if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1349                 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1350         }
1351     }
1352     /* try core command table */
1353     if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1354
1355     if (!lpCmd) {
1356         TRACE("Command %s not found!\n", debugstr_w(verb));
1357         dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1358         goto errCleanUp;
1359     }
1360
1361     /* set return information */
1362     switch (retType = MCI_GetReturnType(lpCmd)) {
1363     case 0:             offset = 1;     break;
1364     case MCI_INTEGER:   offset = 2;     break;
1365     case MCI_STRING:    data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1366     case MCI_RECT:      offset = 5;     break;
1367     default:    ERR("oops\n");
1368     }
1369
1370     TRACE("verb=%s on dev=%s; offset=%d\n", 
1371           debugstr_w(verb), debugstr_w(dev), offset);
1372
1373     if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1374         goto errCleanUp;
1375
1376     /* set up call back */
1377     if (dwFlags & MCI_NOTIFY) {
1378         data[0] = (DWORD)hwndCallback;
1379     }
1380
1381     /* FIXME: the command should get it's own notification window set up and
1382      * ask for device closing while processing the notification mechanism
1383      */
1384     if (lpstrRet && uRetLen) *lpstrRet = '\0';
1385
1386     TRACE("[%d, %s, %08x, %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s]\n",
1387           wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1388           data[0], debugstr_w((WCHAR *)data[0]), data[1], debugstr_w((WCHAR *)data[1]),
1389           data[2], debugstr_w((WCHAR *)data[2]), data[3], debugstr_w((WCHAR *)data[3]),
1390           data[4], debugstr_w((WCHAR *)data[4]), data[5], debugstr_w((WCHAR *)data[5]));
1391
1392     if (strcmpW(verb, wszOpen) == 0) {
1393         if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1394             MCI_UnLoadMciDriver(wmd);
1395         /* FIXME: notification is not properly shared across two opens */
1396     } else {
1397         dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD_PTR)data);
1398     }
1399     TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1400     dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1401     TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1402
1403 errCleanUp:
1404     HeapFree(GetProcessHeap(), 0, verb);
1405     return dwRet;
1406 }
1407
1408 /**************************************************************************
1409  *                              mciSendStringA                  [WINMM.@]
1410  */
1411 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1412                             UINT uRetLen, HWND hwndCallback)
1413 {
1414     LPWSTR      lpwstrCommand;
1415     LPWSTR      lpwstrRet = NULL;
1416     UINT        ret;
1417     INT len;
1418
1419     /* FIXME: is there something to do with lpstrReturnString ? */
1420     len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1421     lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1422     MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1423     if (lpstrRet)
1424     {
1425         lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1426         if (!lpwstrRet) {
1427             WARN("no memory\n");
1428             HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1429             return MCIERR_OUT_OF_MEMORY;
1430         }
1431     }
1432     ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1433     if (!ret && lpwstrRet)
1434         WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1435     HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1436     HeapFree(GetProcessHeap(), 0, lpwstrRet);
1437     return ret;
1438 }
1439
1440 /**************************************************************************
1441  *                              mciExecute                      [WINMM.@]
1442  */
1443 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1444 {
1445     char        strRet[256];
1446     DWORD       ret;
1447
1448     TRACE("(%s)!\n", lpstrCommand);
1449
1450     ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1451     if (ret != 0) {
1452         if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1453             sprintf(strRet, "Unknown MCI error (%d)", ret);
1454         }
1455         MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1456     }
1457     /* FIXME: what shall I return ? */
1458     return TRUE;
1459 }
1460
1461 /**************************************************************************
1462  *                      mciLoadCommandResource                  [WINMM.@]
1463  *
1464  * Strangely, this function only exists as a UNICODE one.
1465  */
1466 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1467 {
1468     UINT        ret = MCI_NO_COMMAND_TABLE;
1469     HRSRC       hRsrc = 0;
1470     HGLOBAL     hMem;
1471
1472     TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1473
1474     /* if a file named "resname.mci" exits, then load resource "resname" from it
1475      * otherwise directly from driver
1476      * We don't support it (who uses this feature ?), but we check anyway
1477      */
1478     if (!type) {
1479 #if 0
1480         /* FIXME: we should put this back into order, but I never found a program
1481          * actually using this feature, so we may not need it
1482          */
1483         char            buf[128];
1484         OFSTRUCT        ofs;
1485
1486         strcat(strcpy(buf, resname), ".mci");
1487         if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1488             FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1489         }
1490 #endif
1491     }
1492     if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1493         (hMem = LoadResource(hInst, hRsrc))) {
1494         ret = MCI_SetCommandTable(hMem, type);
1495         FreeResource(hMem);
1496     }
1497     else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1498
1499     TRACE("=> %04x\n", ret);
1500     return ret;
1501 }
1502
1503 /**************************************************************************
1504  *                      mciFreeCommandResource                  [WINMM.@]
1505  */
1506 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1507 {
1508     TRACE("(%08x)!\n", uTable);
1509
1510     if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1511         return FALSE;
1512
1513     FreeResource(S_MciCmdTable[uTable].hMem);
1514     S_MciCmdTable[uTable].hMem = NULL;
1515     S_MciCmdTable[uTable].lpTable = NULL;
1516     HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1517     S_MciCmdTable[uTable].aVerbs = 0;
1518     S_MciCmdTable[uTable].nVerbs = 0;
1519     return TRUE;
1520 }
1521
1522 /**************************************************************************
1523  *                      MCI_Open                                [internal]
1524  */
1525 static  DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1526 {
1527     WCHAR                       strDevTyp[128];
1528     DWORD                       dwRet;
1529     LPWINE_MCIDRIVER            wmd = NULL;
1530
1531     TRACE("(%08X, %p)\n", dwParam, lpParms);
1532     if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1533
1534     /* only two low bytes are generic, the other ones are dev type specific */
1535 #define WINE_MCIDRIVER_SUPP     (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1536                          MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1537                          MCI_NOTIFY|MCI_WAIT)
1538     if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1539         FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1540     }
1541 #undef WINE_MCIDRIVER_SUPP
1542
1543     strDevTyp[0] = 0;
1544
1545     if (dwParam & MCI_OPEN_TYPE) {
1546         if (dwParam & MCI_OPEN_TYPE_ID) {
1547             WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1548
1549             if (uDevType < MCI_DEVTYPE_FIRST ||
1550                 uDevType > MCI_DEVTYPE_LAST ||
1551                 !LoadStringW(hWinMM32Instance, uDevType,
1552                              strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1553                 dwRet = MCIERR_BAD_INTEGER;
1554                 goto errCleanUp;
1555             }
1556         } else {
1557             LPWSTR      ptr;
1558             if (lpParms->lpstrDeviceType == NULL) {
1559                 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1560                 goto errCleanUp;
1561             }
1562             strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1563             ptr = strchrW(strDevTyp, '!');
1564             if (ptr) {
1565                 /* this behavior is not documented in windows. However, since, in
1566                  * some occasions, MCI_OPEN handling is translated by WinMM into
1567                  * a call to mciSendString("open <type>"); this code shall be correct
1568                  */
1569                 if (dwParam & MCI_OPEN_ELEMENT) {
1570                     ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1571                         debugstr_w(lpParms->lpstrElementName), 
1572                         debugstr_w(strDevTyp));
1573                     dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1574                     goto errCleanUp;
1575                 }
1576                 dwParam |= MCI_OPEN_ELEMENT;
1577                 *ptr++ = 0;
1578                 /* FIXME: not a good idea to write in user supplied buffer */
1579                 lpParms->lpstrElementName = ptr;
1580             }
1581
1582         }
1583         TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1584     }
1585
1586     if (dwParam & MCI_OPEN_ELEMENT) {
1587         TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1588
1589         if (dwParam & MCI_OPEN_ELEMENT_ID) {
1590             FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1591             dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1592             goto errCleanUp;
1593         }
1594
1595         if (!lpParms->lpstrElementName) {
1596             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1597             goto errCleanUp;
1598         }
1599
1600         /* type, if given as a parameter, supersedes file extension */
1601         if (!strDevTyp[0] &&
1602             MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1603                                        strDevTyp, sizeof(strDevTyp))) {
1604             static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1605             if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1606                 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1607                 goto errCleanUp;
1608             }
1609             /* FIXME: this will not work if several CDROM drives are installed on the machine */
1610             strcpyW(strDevTyp, wszCdAudio);
1611         }
1612     }
1613
1614     if (strDevTyp[0] == 0) {
1615         FIXME("Couldn't load driver\n");
1616         dwRet = MCIERR_INVALID_DEVICE_NAME;
1617         goto errCleanUp;
1618     }
1619
1620     if (dwParam & MCI_OPEN_ALIAS) {
1621         TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1622         if (!lpParms->lpstrAlias) {
1623             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1624             goto errCleanUp;
1625         }
1626     }
1627
1628     if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1629         goto errCleanUp;
1630     }
1631
1632     if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1633         TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1634         /* FIXME: is dwRet the correct ret code ? */
1635         goto errCleanUp;
1636     }
1637
1638     /* only handled devices fall through */
1639     TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1640
1641     if (dwParam & MCI_NOTIFY)
1642         mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1643
1644     return 0;
1645 errCleanUp:
1646     if (wmd) MCI_UnLoadMciDriver(wmd);
1647
1648     if (dwParam & MCI_NOTIFY)
1649         mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1650     return dwRet;
1651 }
1652
1653 /**************************************************************************
1654  *                      MCI_Close                               [internal]
1655  */
1656 static  DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1657 {
1658     DWORD               dwRet;
1659     LPWINE_MCIDRIVER    wmd;
1660
1661     TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1662
1663     /* Every device must handle MCI_NOTIFY on its own. */
1664     if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1665         while (MciDrivers) {
1666             /* Retrieve the device ID under lock, but send the message without,
1667              * the driver might be calling some winmm functions from another
1668              * thread before being fully stopped.
1669              */
1670             EnterCriticalSection(&WINMM_cs);
1671             if (!MciDrivers)
1672             {
1673                 LeaveCriticalSection(&WINMM_cs);
1674                 break;
1675             }
1676             wDevID = MciDrivers->wDeviceID;
1677             LeaveCriticalSection(&WINMM_cs);
1678             MCI_Close(wDevID, dwParam, lpParms);
1679         }
1680         return 0;
1681     }
1682
1683     if (!(wmd = MCI_GetDriver(wDevID))) {
1684         return MCIERR_INVALID_DEVICE_ID;
1685     }
1686
1687     dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1688
1689     MCI_UnLoadMciDriver(wmd);
1690
1691     return dwRet;
1692 }
1693
1694 /**************************************************************************
1695  *                      MCI_WriteString                         [internal]
1696  */
1697 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1698 {
1699     DWORD       ret = 0;
1700
1701     if (lpSrcStr) {
1702         dstSize /= sizeof(WCHAR);
1703         if (dstSize <= strlenW(lpSrcStr)) {
1704             lstrcpynW(lpDstStr, lpSrcStr, dstSize - 1);
1705             ret = MCIERR_PARAM_OVERFLOW;
1706         } else {
1707             strcpyW(lpDstStr, lpSrcStr);
1708         }
1709     } else {
1710         *lpDstStr = 0;
1711     }
1712     return ret;
1713 }
1714
1715 /**************************************************************************
1716  *                      MCI_Sysinfo                             [internal]
1717  */
1718 static  DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1719 {
1720     DWORD               ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1721     WCHAR               buf[2048], *s = buf, *p;
1722     LPWINE_MCIDRIVER    wmd;
1723     HKEY                hKey;
1724
1725     if (lpParms == NULL)                        return MCIERR_NULL_PARAMETER_BLOCK;
1726     if (lpParms->lpstrReturn == NULL)           return MCIERR_PARAM_OVERFLOW;
1727
1728     TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1729           uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1730
1731     switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1732     case MCI_SYSINFO_QUANTITY:
1733         if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1734             if (dwFlags & MCI_SYSINFO_OPEN) {
1735                 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1736                 EnterCriticalSection(&WINMM_cs);
1737                 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1738                     cnt++;
1739                 }
1740                 LeaveCriticalSection(&WINMM_cs);
1741             } else {
1742                 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1743                 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1744                                    0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1745                     RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1746                     RegCloseKey( hKey );
1747                 }
1748                 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1749                     for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1750             }
1751         } else {
1752             if (dwFlags & MCI_SYSINFO_OPEN) {
1753                 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n", lpParms->wDeviceType);
1754                 EnterCriticalSection(&WINMM_cs);
1755                 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1756                     if (wmd->wType == lpParms->wDeviceType) cnt++;
1757                 }
1758                 LeaveCriticalSection(&WINMM_cs);
1759             } else {
1760                 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n", lpParms->wDeviceType);
1761                 FIXME("Don't know how to get # of MCI devices of a given type\n");
1762                 cnt = 1;
1763             }
1764         }
1765         *(DWORD*)lpParms->lpstrReturn = cnt;
1766         TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1767         ret = MCI_INTEGER_RETURNED;
1768         break;
1769     case MCI_SYSINFO_INSTALLNAME:
1770         TRACE("MCI_SYSINFO_INSTALLNAME\n");
1771         if ((wmd = MCI_GetDriver(uDevID))) {
1772             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1773                                   wmd->lpstrDeviceType);
1774         } else {
1775             *lpParms->lpstrReturn = 0;
1776             ret = MCIERR_INVALID_DEVICE_ID;
1777         }
1778         TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1779         break;
1780     case MCI_SYSINFO_NAME:
1781         TRACE("MCI_SYSINFO_NAME\n");
1782         if (dwFlags & MCI_SYSINFO_OPEN) {
1783             FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1784             ret = MCIERR_UNRECOGNIZED_COMMAND;
1785         } else {
1786             s = NULL;
1787             if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0, 
1788                                KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1789                 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 
1790                                       0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS && 
1791                     lpParms->dwNumber <= cnt) {
1792                     DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1793                     if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1, 
1794                                       buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1795                         s = buf;
1796                 }
1797                 RegCloseKey( hKey );
1798             }
1799             if (!s) {
1800                 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1801                     for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1802                         TRACE("%d: %s\n", cnt, debugstr_w(p));
1803                         if (cnt == lpParms->dwNumber - 1) {
1804                             s = p;
1805                             break;
1806                         }
1807                     }
1808                 }
1809             }
1810             ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1811         }
1812         TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1813         break;
1814     default:
1815         TRACE("Unsupported flag value=%08x\n", dwFlags);
1816         ret = MCIERR_UNRECOGNIZED_COMMAND;
1817     }
1818     return ret;
1819 }
1820
1821 /**************************************************************************
1822  *                      MCI_Break                               [internal]
1823  */
1824 static  DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1825 {
1826     DWORD       dwRet = 0;
1827
1828     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1829
1830     if (dwFlags & MCI_NOTIFY)
1831         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1832                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1833
1834     return dwRet;
1835 }
1836
1837 /**************************************************************************
1838  *                      MCI_Sound                               [internal]
1839  */
1840 static  DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1841 {
1842     DWORD       dwRet = 0;
1843
1844     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1845
1846     if (dwFlags & MCI_SOUND_NAME)
1847         dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1848     else
1849         dwRet = MMSYSERR_ERROR; /* what should be done ??? */
1850     if (dwFlags & MCI_NOTIFY)
1851         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1852                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1853
1854     return dwRet;
1855 }
1856
1857 /**************************************************************************
1858  *                      MCI_SendCommand                         [internal]
1859  */
1860 DWORD   MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1861 {
1862     DWORD               dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1863
1864     switch (wMsg) {
1865     case MCI_OPEN:
1866         dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1867         break;
1868     case MCI_CLOSE:
1869         dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1870         break;
1871     case MCI_SYSINFO:
1872         dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
1873         break;
1874     case MCI_BREAK:
1875         dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1876         break;
1877     case MCI_SOUND:
1878         dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
1879         break;
1880     default:
1881       if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1882             FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1883             dwRet = MCIERR_CANNOT_USE_ALL;
1884         } else {
1885             dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
1886         }
1887         break;
1888     }
1889     return dwRet;
1890 }
1891
1892 /**************************************************************************
1893  *                              MCI_CleanUp                     [internal]
1894  *
1895  * Some MCI commands need to be cleaned-up (when not called from
1896  * mciSendString), because MCI drivers return extra information for string
1897  * transformation. This function gets rid of them.
1898  */
1899 static LRESULT  MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
1900 {
1901     if (LOWORD(dwRet))
1902         return LOWORD(dwRet);
1903
1904     switch (wMsg) {
1905     case MCI_GETDEVCAPS:
1906         switch (dwRet & 0xFFFF0000ul) {
1907         case 0:
1908         case MCI_COLONIZED3_RETURN:
1909         case MCI_COLONIZED4_RETURN:
1910         case MCI_INTEGER_RETURNED:
1911             /* nothing to do */
1912             break;
1913         case MCI_RESOURCE_RETURNED:
1914         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1915             {
1916                 LPMCI_GETDEVCAPS_PARMS  lmgp;
1917
1918                 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
1919                 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
1920                 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1921             }
1922             break;
1923         default:
1924             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1925                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1926         }
1927         break;
1928     case MCI_STATUS:
1929         switch (dwRet & 0xFFFF0000ul) {
1930         case 0:
1931         case MCI_COLONIZED3_RETURN:
1932         case MCI_COLONIZED4_RETURN:
1933         case MCI_INTEGER_RETURNED:
1934             /* nothing to do */
1935             break;
1936         case MCI_RESOURCE_RETURNED:
1937         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1938             {
1939                 LPMCI_STATUS_PARMS      lsp;
1940
1941                 lsp = (LPMCI_STATUS_PARMS)dwParam2;
1942                 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
1943                 lsp->dwReturn = LOWORD(lsp->dwReturn);
1944             }
1945             break;
1946         default:
1947             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1948                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1949         }
1950         break;
1951     case MCI_SYSINFO:
1952         switch (dwRet & 0xFFFF0000ul) {
1953         case 0:
1954         case MCI_INTEGER_RETURNED:
1955             /* nothing to do */
1956             break;
1957         default:
1958             FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
1959         }
1960         break;
1961     default:
1962         if (HIWORD(dwRet)) {
1963             FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
1964                   dwRet, MCI_MessageToString(wMsg));
1965         }
1966         break;
1967     }
1968     return LOWORD(dwRet);
1969 }
1970
1971 /**************************************************************************
1972  *                              mciGetErrorStringW              [WINMM.@]
1973  */
1974 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
1975 {
1976     BOOL                ret = FALSE;
1977
1978     if (lpstrBuffer != NULL && uLength > 0 &&
1979         wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
1980
1981         if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
1982             ret = TRUE;
1983         }
1984     }
1985     return ret;
1986 }
1987
1988 /**************************************************************************
1989  *                              mciGetErrorStringA              [WINMM.@]
1990  */
1991 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
1992 {
1993     BOOL                ret = FALSE;
1994
1995     if (lpstrBuffer != NULL && uLength > 0 &&
1996         dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
1997
1998         if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
1999             ret = TRUE;
2000         }
2001     }
2002     return ret;
2003 }
2004
2005 /**************************************************************************
2006  *                      mciDriverNotify                         [WINMM.@]
2007  */
2008 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2009 {
2010     TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2011
2012     return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2013 }
2014
2015 /**************************************************************************
2016  *                      mciGetDriverData                        [WINMM.@]
2017  */
2018 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2019 {
2020     LPWINE_MCIDRIVER    wmd;
2021
2022     TRACE("(%04x)\n", uDeviceID);
2023
2024     wmd = MCI_GetDriver(uDeviceID);
2025
2026     if (!wmd) {
2027         WARN("Bad uDeviceID\n");
2028         return 0L;
2029     }
2030
2031     return wmd->dwPrivate;
2032 }
2033
2034 /**************************************************************************
2035  *                      mciSetDriverData                        [WINMM.@]
2036  */
2037 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2038 {
2039     LPWINE_MCIDRIVER    wmd;
2040
2041     TRACE("(%04x, %08lx)\n", uDeviceID, data);
2042
2043     wmd = MCI_GetDriver(uDeviceID);
2044
2045     if (!wmd) {
2046         WARN("Bad uDeviceID\n");
2047         return FALSE;
2048     }
2049
2050     wmd->dwPrivate = data;
2051     return TRUE;
2052 }
2053
2054 /**************************************************************************
2055  *                              mciSendCommandW                 [WINMM.@]
2056  *
2057  */
2058 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2059 {
2060     DWORD       dwRet;
2061
2062     TRACE("(%08x, %s, %08lx, %08lx)\n",
2063           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2064
2065     dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2066     dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2067     TRACE("=> %08x\n", dwRet);
2068     return dwRet;
2069 }
2070
2071 /**************************************************************************
2072  *                              mciSendCommandA                 [WINMM.@]
2073  */
2074 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2075 {
2076     DWORD ret;
2077     int mapped;
2078
2079     TRACE("(%08x, %s, %08lx, %08lx)\n",
2080           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2081
2082     mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2083     if (mapped == -1)
2084     {
2085         FIXME("message %04x mapping failed\n", wMsg);
2086         return MMSYSERR_NOMEM;
2087     }
2088     ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2089     if (mapped)
2090         MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2091     return ret;
2092 }
2093
2094 /**************************************************************************
2095  *                              mciGetDeviceIDA                 [WINMM.@]
2096  */
2097 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2098 {
2099     LPWSTR w = MCI_strdupAtoW(lpstrName);
2100     UINT ret = MCIERR_OUT_OF_MEMORY;
2101
2102     if (w)
2103     {
2104         ret = mciGetDeviceIDW(w);
2105         HeapFree(GetProcessHeap(), 0, w);
2106     }
2107     return ret;
2108 }
2109
2110 /**************************************************************************
2111  *                              mciGetDeviceIDW                 [WINMM.@]
2112  */
2113 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2114 {
2115     return MCI_GetDriverFromString(lpwstrName); 
2116 }
2117
2118 /**************************************************************************
2119  *                              MCI_DefYieldProc                [internal]
2120  */
2121 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2122 {
2123     INT16       ret;
2124     MSG         msg;
2125
2126     TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2127
2128     if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2129         (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2130         PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2131         ret = 0;
2132     } else {
2133         msg.hwnd = HWND_32(HIWORD(data));
2134         while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2135         ret = -1;
2136     }
2137     return ret;
2138 }
2139
2140 /**************************************************************************
2141  *                              mciSetYieldProc                 [WINMM.@]
2142  */
2143 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2144 {
2145     LPWINE_MCIDRIVER    wmd;
2146
2147     TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2148
2149     if (!(wmd = MCI_GetDriver(uDeviceID))) {
2150         WARN("Bad uDeviceID\n");
2151         return FALSE;
2152     }
2153
2154     wmd->lpfnYieldProc = fpYieldProc;
2155     wmd->dwYieldData   = dwYieldData;
2156
2157     return TRUE;
2158 }
2159
2160 /**************************************************************************
2161  *                              mciGetDeviceIDFromElementIDA    [WINMM.@]
2162  */
2163 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2164 {
2165     LPWSTR w = MCI_strdupAtoW(lpstrType);
2166     UINT ret = 0;
2167
2168     if (w)
2169     {
2170         ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2171         HeapFree(GetProcessHeap(), 0, w);
2172     }
2173     return ret;
2174 }
2175
2176 /**************************************************************************
2177  *                              mciGetDeviceIDFromElementIDW    [WINMM.@]
2178  */
2179 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2180 {
2181     /* FIXME: that's rather strange, there is no
2182      * mciGetDeviceIDFromElementID32A in winmm.spec
2183      */
2184     FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2185     return 0;
2186 }
2187
2188 /**************************************************************************
2189  *                              mciGetYieldProc                 [WINMM.@]
2190  */
2191 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2192 {
2193     LPWINE_MCIDRIVER    wmd;
2194
2195     TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2196
2197     if (!(wmd = MCI_GetDriver(uDeviceID))) {
2198         WARN("Bad uDeviceID\n");
2199         return NULL;
2200     }
2201     if (!wmd->lpfnYieldProc) {
2202         WARN("No proc set\n");
2203         return NULL;
2204     }
2205     if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2206     return wmd->lpfnYieldProc;
2207 }
2208
2209 /**************************************************************************
2210  *                              mciGetCreatorTask               [WINMM.@]
2211  */
2212 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2213 {
2214     LPWINE_MCIDRIVER    wmd;
2215     HTASK ret = 0;
2216
2217     if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)wmd->CreatorThread;
2218
2219     TRACE("(%u) => %p\n", uDeviceID, ret);
2220     return ret;
2221 }
2222
2223 /**************************************************************************
2224  *                      mciDriverYield                          [WINMM.@]
2225  */
2226 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2227 {
2228     LPWINE_MCIDRIVER    wmd;
2229     UINT                ret = 0;
2230
2231     TRACE("(%04x)\n", uDeviceID);
2232
2233     if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2234         MSG msg;
2235         PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2236     } else {
2237         ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2238     }
2239
2240     return ret;
2241 }