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