dmusic: Replace debugstr_DMUS_OBJECTDESC by a specific dump function to avoid debug...
[wine] / dlls / dmusic / collection.c
1 /* IDirectMusicCollection Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "dmusic_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface);
26 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface);
27 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
28 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
29
30 /*****************************************************************************
31  * IDirectMusicCollectionImpl implementation
32  */
33 /* IDirectMusicCollectionImpl IUnknown part: */
34 static HRESULT WINAPI IDirectMusicCollectionImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
35         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
36         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
37
38         if (IsEqualIID (riid, &IID_IUnknown)) {
39                 *ppobj = &This->UnknownVtbl;
40                 IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
41                 return S_OK;    
42         } else if (IsEqualIID (riid, &IID_IDirectMusicCollection)) {
43                 *ppobj = &This->CollectionVtbl;
44                 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef ((LPDIRECTMUSICCOLLECTION)&This->CollectionVtbl);
45                 return S_OK;
46         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
47                 *ppobj = &This->ObjectVtbl;
48                 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);          
49                 return S_OK;
50         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
51                 *ppobj = &This->PersistStreamVtbl;
52                 IDirectMusicCollectionImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);           
53                 return S_OK;
54         }
55         
56         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
57         return E_NOINTERFACE;
58 }
59
60 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) {
61         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
62         ULONG refCount = InterlockedIncrement(&This->ref);
63
64         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
65
66         DMUSIC_LockModule();
67
68         return refCount;
69 }
70
71 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) {
72         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
73         ULONG refCount = InterlockedDecrement(&This->ref);
74
75         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
76
77         if (!refCount) {
78                 HeapFree(GetProcessHeap(), 0, This);
79         }
80
81         DMUSIC_UnlockModule();
82
83         return refCount;
84 }
85
86 static const IUnknownVtbl DirectMusicCollection_Unknown_Vtbl = {
87         IDirectMusicCollectionImpl_IUnknown_QueryInterface,
88         IDirectMusicCollectionImpl_IUnknown_AddRef,
89         IDirectMusicCollectionImpl_IUnknown_Release
90 };
91
92 /* IDirectMusicCollectionImpl IDirectMusicCollection part: */
93 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface (LPDIRECTMUSICCOLLECTION iface, REFIID riid, LPVOID *ppobj) {
94         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
95         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
96 }
97
98 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface) {
99         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
100         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
101 }
102
103 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_Release (LPDIRECTMUSICCOLLECTION iface) {
104         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
105         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
106 }
107
108 /* IDirectMusicCollection Interface follow: */
109 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwPatch, IDirectMusicInstrument** ppInstrument) {
110         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
111         DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
112         struct list *listEntry;
113         DWORD dwInstPatch;
114
115         TRACE("(%p, %d, %p)\n", This, dwPatch, ppInstrument);
116         
117         LIST_FOR_EACH (listEntry, &This->Instruments) {
118                 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
119                 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, &dwInstPatch);
120                 if (dwPatch == dwInstPatch) {
121                         *ppInstrument = tmpEntry->pInstrument;
122                         IDirectMusicInstrument_AddRef (tmpEntry->pInstrument);
123                         IDirectMusicInstrumentImpl_Custom_Load (tmpEntry->pInstrument, This->pStm); /* load instrument before returning it */
124                         TRACE(": returning instrument %p\n", *ppInstrument);
125                         return S_OK;
126                 }
127                         
128         }
129         TRACE(": instrument not found\n");
130         
131         return DMUS_E_INVALIDPATCH;
132 }
133
134 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwIndex, DWORD* pdwPatch, LPWSTR pwszName, DWORD dwNameLen) {
135         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
136         unsigned int r = 0;
137         DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
138         struct list *listEntry;
139        DWORD dwLen;
140                 
141         TRACE("(%p, %d, %p, %p, %d)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
142         LIST_FOR_EACH (listEntry, &This->Instruments) {
143                 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
144                 if (r == dwIndex) {
145                         ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
146                         IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
147                        if (pwszName) {
148                                dwLen = min(strlenW(pInstrument->wszName),dwNameLen-1);
149                                memcpy (pwszName, pInstrument->wszName, dwLen * sizeof(WCHAR));
150                                pwszName[dwLen] = '\0';
151                        }
152                         return S_OK;
153                 }
154                 r++;            
155         }
156         
157         return S_FALSE;
158 }
159
160 static const IDirectMusicCollectionVtbl DirectMusicCollection_Collection_Vtbl = {
161         IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface,
162         IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef,
163         IDirectMusicCollectionImpl_IDirectMusicCollection_Release,
164         IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument,
165         IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument
166 };
167
168 /* IDirectMusicCollectionImpl IDirectMusicObject part: */
169 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
170         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
171         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
172 }
173
174 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
175         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
176         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
177 }
178
179 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
180         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
181         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
182 }
183
184 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
185         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
186         TRACE("(%p, %p)\n", This, pDesc);
187         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
188         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
189         return S_OK;
190 }
191
192 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor(LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc)
193 {
194         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
195
196         TRACE("(%p, %p)\n", iface, pDesc);
197
198         if (!pDesc)
199                 return E_POINTER;
200
201         if (TRACE_ON(dmusic))
202         {
203                 TRACE("Setting descriptor:\n");
204                 dump_DMUS_OBJECTDESC(pDesc);
205         }
206
207         /* According to MSDN, we should copy only given values, not whole struct */     
208         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
209                 This->pDesc->guidObject = pDesc->guidObject;
210         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
211                 This->pDesc->guidClass = pDesc->guidClass;
212         if (pDesc->dwValidData & DMUS_OBJ_NAME)
213                lstrcpynW(This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
214         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
215                lstrcpynW(This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
216         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
217                lstrcpynW(This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
218         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
219                 This->pDesc->vVersion = pDesc->vVersion;
220         if (pDesc->dwValidData & DMUS_OBJ_DATE)
221                 This->pDesc->ftDate = pDesc->ftDate;
222         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
223                 This->pDesc->llMemLength = pDesc->llMemLength;
224                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
225         }
226         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
227                 /* according to MSDN, we copy the stream */
228                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
229         }
230         
231         /* add new flags */
232         This->pDesc->dwValidData |= pDesc->dwValidData;
233
234         return S_OK;
235 }
236
237 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor(LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc)
238 {
239         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
240         DMUS_PRIVATE_CHUNK Chunk;
241         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
242         LARGE_INTEGER liMove; /* used when skipping chunks */
243
244         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
245
246         /* FIXME: should this be determined from stream? */
247         pDesc->dwValidData |= DMUS_OBJ_CLASS;
248         pDesc->guidClass = CLSID_DirectMusicCollection;
249
250         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
251         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
252         switch (Chunk.fccID) {  
253                 case FOURCC_RIFF: {
254                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
255                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
256                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
257                         StreamCount = 0;
258                         if (Chunk.fccID == mmioFOURCC('D','L','S',' ')) {
259                                 TRACE_(dmfile)(": collection form\n");
260                                 do {
261                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
262                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
263                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
264                                         switch (Chunk.fccID) {
265                                                 case FOURCC_DLID: {
266                                                         TRACE_(dmfile)(": GUID chunk\n");
267                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
268                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
269                                                         break;
270                                                 }
271                                                 case DMUS_FOURCC_VERSION_CHUNK: {
272                                                         TRACE_(dmfile)(": version chunk\n");
273                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
274                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
275                                                         break;
276                                                 }
277                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
278                                                         TRACE_(dmfile)(": category chunk\n");
279                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
280                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
281                                                         break;
282                                                 }
283                                                 case FOURCC_LIST: {
284                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
285                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
286                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
287                                                         ListCount[0] = 0;
288                                                         switch (Chunk.fccID) {
289                                                                 /* pure INFO list, such can be found in dls collections */
290                                                                 case mmioFOURCC('I','N','F','O'): {
291                                                                         TRACE_(dmfile)(": INFO list\n");
292                                                                         do {
293                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
294                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
295                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
296                                                                                 switch (Chunk.fccID) {
297                                                                                         case mmioFOURCC('I','N','A','M'):{
298                                                                                                 CHAR szName[DMUS_MAX_NAME];                                                                                             
299                                                                                                 TRACE_(dmfile)(": name chunk\n");
300                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
301                                                                                                 IStream_Read (pStream, szName, Chunk.dwSize, NULL);
302                                                                                                 MultiByteToWideChar (CP_ACP, 0, szName, -1, pDesc->wszName, DMUS_MAX_NAME);
303                                                                                                 if (even_or_odd(Chunk.dwSize)) {
304                                                                                                         ListCount[0] ++;
305                                                                                                         liMove.QuadPart = 1;
306                                                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
307                                                                                                 }
308                                                                                                 break;
309                                                                                         }
310                                                                                         case mmioFOURCC('I','A','R','T'): {
311                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
312                                                                                                 if (even_or_odd(Chunk.dwSize)) {
313                                                                                                         ListCount[0] ++;
314                                                                                                         Chunk.dwSize++;
315                                                                                                 }
316                                                                                                 liMove.QuadPart = Chunk.dwSize;
317                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
318                                                                                                 break;
319                                                                                         }
320                                                                                         case mmioFOURCC('I','C','O','P'): {
321                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
322                                                                                                 if (even_or_odd(Chunk.dwSize)) {
323                                                                                                         ListCount[0] ++;
324                                                                                                         Chunk.dwSize++;
325                                                                                                 }
326                                                                                                 liMove.QuadPart = Chunk.dwSize;
327                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
328                                                                                                 break;
329                                                                                         }
330                                                                                         case mmioFOURCC('I','S','B','J'): {
331                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
332                                                                                                 if (even_or_odd(Chunk.dwSize)) {
333                                                                                                         ListCount[0] ++;
334                                                                                                         Chunk.dwSize++;
335                                                                                                 }
336                                                                                                 liMove.QuadPart = Chunk.dwSize;
337                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
338                                                                                                 break;
339                                                                                         }
340                                                                                         case mmioFOURCC('I','C','M','T'): {
341                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
342                                                                                                 if (even_or_odd(Chunk.dwSize)) {
343                                                                                                         ListCount[0] ++;
344                                                                                                         Chunk.dwSize++;
345                                                                                                 }
346                                                                                                 liMove.QuadPart = Chunk.dwSize;
347                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
348                                                                                                 break;
349                                                                                         }
350                                                                                         default: {
351                                                                                                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
352                                                                                                 if (even_or_odd(Chunk.dwSize)) {
353                                                                                                         ListCount[0] ++;
354                                                                                                         Chunk.dwSize++;
355                                                                                                 }
356                                                                                                 liMove.QuadPart = Chunk.dwSize;
357                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
358                                                                                                 break;                                          
359                                                                                         }
360                                                                                 }
361                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
362                                                                         } while (ListCount[0] < ListSize[0]);
363                                                                         break;
364                                                                 }
365                                                                 default: {
366                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
367                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
368                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
369                                                                         break;                                          
370                                                                 }
371                                                         }
372                                                         break;
373                                                 }       
374                                                 default: {
375                                                         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
376                                                         liMove.QuadPart = Chunk.dwSize;
377                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
378                                                         break;                                          
379                                                 }
380                                         }
381                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
382                                 } while (StreamCount < StreamSize);
383                         } else {
384                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
385                                 liMove.QuadPart = StreamSize;
386                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
387                                 return E_FAIL;
388                         }
389                 
390                         TRACE_(dmfile)(": reading finished\n");
391                         break;
392                 }
393                 default: {
394                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
395                         liMove.QuadPart = Chunk.dwSize;
396                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
397                         return DMUS_E_INVALIDFILE;
398                 }
399         }
400
401         if (TRACE_ON(dmusic))
402         {
403                 TRACE("Returning descriptor:\n");
404                 dump_DMUS_OBJECTDESC(pDesc);
405         }
406
407         return S_OK;
408 }
409
410 static const IDirectMusicObjectVtbl DirectMusicCollection_Object_Vtbl = {
411         IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface,
412         IDirectMusicCollectionImpl_IDirectMusicObject_AddRef,
413         IDirectMusicCollectionImpl_IDirectMusicObject_Release,
414         IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor,
415         IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor,
416         IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor
417 };
418
419 /* IDirectMusicCollectionImpl IPersistStream part: */
420 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
421         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
422         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
423 }
424
425 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
426         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
427         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
428 }
429
430 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
431         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
432         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
433 }
434
435 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
436         return E_NOTIMPL;
437 }
438
439 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
440         return E_NOTIMPL;
441 }
442
443 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
444         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
445
446         DMUS_PRIVATE_CHUNK Chunk;
447         DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
448         LARGE_INTEGER liMove; /* used when skipping chunks */
449         ULARGE_INTEGER dlibCollectionPosition, dlibInstrumentPosition, dlibWavePoolPosition;
450         
451         IStream_AddRef (pStm); /* add count for later references */
452         liMove.QuadPart = 0;
453         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibCollectionPosition); /* store offset, in case it'll be needed later */
454         This->liCollectionPosition.QuadPart = dlibCollectionPosition.QuadPart;
455         This->pStm = pStm;
456         
457         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
458         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
459         switch (Chunk.fccID) {  
460                 case FOURCC_RIFF: {
461                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
462                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
463                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
464                         StreamCount = 0;
465                         switch (Chunk.fccID) {
466                                 case FOURCC_DLS: {
467                                         TRACE_(dmfile)(": collection form\n");
468                                         do {
469                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
470                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
471                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
472                                                 switch (Chunk.fccID) {
473                                                         case FOURCC_COLH: {
474                                                                 TRACE_(dmfile)(": collection header chunk\n");
475                                                                 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
476                                                                 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
477                                                                 break;                                                          
478                                                         }
479                                                         case FOURCC_DLID: {
480                                                                 TRACE_(dmfile)(": DLID (GUID) chunk\n");
481                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
482                                                                 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
483                                                                 break;
484                                                         }
485                                                         case FOURCC_VERS: {
486                                                                 TRACE_(dmfile)(": version chunk\n");
487                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
488                                                                 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
489                                                                 break;
490                                                         }
491                                                         case FOURCC_PTBL: {
492                                                                 TRACE_(dmfile)(": pool table chunk\n");
493                                                                 This->pPoolTable = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(POOLTABLE));
494                                                                 IStream_Read (pStm, This->pPoolTable, sizeof(POOLTABLE), NULL);
495                                                                 Chunk.dwSize -= sizeof(POOLTABLE);
496                                                                 This->pPoolCues = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, This->pPoolTable->cCues*sizeof(POOLCUE));
497                                                                 IStream_Read (pStm, This->pPoolCues, Chunk.dwSize, NULL);
498                                                                 break;
499                                                         }
500                                                         case FOURCC_LIST: {
501                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
502                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
503                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
504                                                                 ListCount[0] = 0;
505                                                                 switch (Chunk.fccID) {
506                                                                         case mmioFOURCC('I','N','F','O'): {
507                                                                                 TRACE_(dmfile)(": INFO list\n");
508                                                                                 do {
509                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
510                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
511                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
512                                                                                         switch (Chunk.fccID) {
513                                                                                                 case mmioFOURCC('I','N','A','M'): {
514                                                                                                         CHAR szName[DMUS_MAX_NAME];
515                                                                                                         TRACE_(dmfile)(": name chunk\n");
516                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
517                                                                                                         IStream_Read (pStm, szName, Chunk.dwSize, NULL);
518                                                                                                         MultiByteToWideChar (CP_ACP, 0, szName, -1, This->pDesc->wszName, DMUS_MAX_NAME);
519                                                                                                         if (even_or_odd(Chunk.dwSize)) {
520                                                                                                                 ListCount[0] ++;
521                                                                                                                 liMove.QuadPart = 1;
522                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
523                                                                                                         }
524                                                                                                         break;
525                                                                                                 }
526                                                                                                 case mmioFOURCC('I','A','R','T'): {
527                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
528                                                                                                         if (even_or_odd(Chunk.dwSize)) {
529                                                                                                                 ListCount[0] ++;
530                                                                                                                 Chunk.dwSize++;
531                                                                                                         }
532                                                                                                         liMove.QuadPart = Chunk.dwSize;
533                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
534                                                                                                         break;
535                                                                                                 }
536                                                                                                 case mmioFOURCC('I','C','O','P'): {
537                                                                                                         TRACE_(dmfile)(": copyright chunk\n");
538                                                                                                         This->szCopyright = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
539                                                                                                         IStream_Read (pStm, This->szCopyright, Chunk.dwSize, NULL);
540                                                                                                         if (even_or_odd(Chunk.dwSize)) {
541                                                                                                                 ListCount[0] ++;
542                                                                                                                 liMove.QuadPart = 1;
543                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
544                                                                                                         }
545                                                                                                         break;
546                                                                                                 }
547                                                                                                 case mmioFOURCC('I','S','B','J'): {
548                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
549                                                                                                         if (even_or_odd(Chunk.dwSize)) {
550                                                                                                                 ListCount[0] ++;
551                                                                                                                 Chunk.dwSize++;
552                                                                                                         }
553                                                                                                         liMove.QuadPart = Chunk.dwSize;
554                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
555                                                                                                         break;
556                                                                                                 }
557                                                                                                 case mmioFOURCC('I','C','M','T'): {
558                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
559                                                                                                         if (even_or_odd(Chunk.dwSize)) {
560                                                                                                                 ListCount[0] ++;
561                                                                                                                 Chunk.dwSize++;
562                                                                                                         }
563                                                                                                         liMove.QuadPart = Chunk.dwSize;
564                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
565                                                                                                         break;
566                                                                                                 }
567                                                                                                 default: {
568                                                                                                         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
569                                                                                                         if (even_or_odd(Chunk.dwSize)) {
570                                                                                                                 ListCount[0] ++;
571                                                                                                                 Chunk.dwSize++;
572                                                                                                         }
573                                                                                                         liMove.QuadPart = Chunk.dwSize;
574                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
575                                                                                                         break;                                          
576                                                                                                 }
577                                                                                         }
578                                                                                         TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
579                                                                                 } while (ListCount[0] < ListSize[0]);
580                                                                                 break;
581                                                                         }
582                                                                         case FOURCC_WVPL: {
583                                                                                 TRACE_(dmfile)(": wave pool list (mark & skip)\n");
584                                                                                 liMove.QuadPart = 0;
585                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibWavePoolPosition); /* store position */
586                                                                                 This->liWavePoolTablePosition.QuadPart = dlibWavePoolPosition.QuadPart;
587                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
588                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
589                                                                                 break;  
590                                                                         }
591                                                                         case FOURCC_LINS: {
592                                                                                 TRACE_(dmfile)(": instruments list\n");
593                                                                                 do {
594                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
595                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
596                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
597                                                                                         switch (Chunk.fccID) {
598                                                                                                 case FOURCC_LIST: {
599                                                                                                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
600                                                                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
601                                                                                                         ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
602                                                                                                         ListCount[1] = 0;                                                                                                       
603                                                                                                         switch (Chunk.fccID) {
604                                                                                                                 case FOURCC_INS: {
605                                                                                                                         LPDMUS_PRIVATE_INSTRUMENTENTRY pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENTENTRY));
606                                                                                                                         TRACE_(dmfile)(": instrument list\n");
607                                                                                                                         DMUSIC_CreateDirectMusicInstrumentImpl (&IID_IDirectMusicInstrument, (LPVOID*)&pNewInstrument->pInstrument, NULL); /* only way to create this one... even M$ does it discretely */
608                                                                                                                         {
609                                                                                                                             ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, pNewInstrument->pInstrument, pInstrument);
610                                                                                                                             liMove.QuadPart = 0;
611                                                                                                                             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibInstrumentPosition);
612                                                                                                                             pInstrument->liInstrumentPosition.QuadPart = dlibInstrumentPosition.QuadPart - (2*sizeof(FOURCC) + sizeof(DWORD)); /* store offset, it'll be needed later */
613
614                                                                                                                             do {
615                                                                                                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
616                                                                                                                                 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
617                                                                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
618                                                                                                                                 switch (Chunk.fccID) {
619                                                                                                                                         case FOURCC_INSH: {
620                                                                                                                                                 TRACE_(dmfile)(": instrument header chunk\n");
621                                                                                                                                                 pInstrument->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
622                                                                                                                                                 IStream_Read (pStm, pInstrument->pHeader, Chunk.dwSize, NULL);
623                                                                                                                                                 break;  
624                                                                                                                                         }
625                                                                                                                                         case FOURCC_DLID: {
626                                                                                                                                                 TRACE_(dmfile)(": DLID (GUID) chunk\n");
627                                                                                                                                                 pInstrument->pInstrumentID = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
628                                                                                                                                                 IStream_Read (pStm, pInstrument->pInstrumentID, Chunk.dwSize, NULL);
629                                                                                                                                                 break;
630                                                                                                                                         }
631                                                                                                                                         case FOURCC_LIST: {
632                                                                                                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
633                                                                                                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
634                                                                                                                                                 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
635                                                                                                                                                 ListCount[2] = 0;
636                                                                                                                                                 switch (Chunk.fccID) {
637                                                                                                                                                         default: {
638                                                                                                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
639                                                                                                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
640                                                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
641                                                                                                                                                                 break;                                          
642                                                                                                                                                         }
643                                                                                                                                                 }
644                                                                                                                                                 break;
645                                                                                                                                         }                               
646                                                                                                                                         default: {
647                                                                                                                                                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
648                                                                                                                                                 liMove.QuadPart = Chunk.dwSize;
649                                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
650                                                                                                                                                 break;                                          
651                                                                                                                                         }
652                                                                                                                                 }
653                                                                                                                                 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
654                                                                                                                             } while (ListCount[1] < ListSize[1]);
655                                                                                                                             /* DEBUG: dumps whole instrument object tree: */
656                                                                                                                             if (TRACE_ON(dmusic)) {
657                                                                                                                                 TRACE("*** IDirectMusicInstrument (%p) ***\n", pInstrument);
658                                                                                                                                 if (pInstrument->pInstrumentID)
659                                                                                                                                         TRACE(" - GUID = %s\n", debugstr_dmguid(pInstrument->pInstrumentID));
660                                                                                                                                 
661                                                                                                                                 TRACE(" - Instrument header:\n");
662                                                                                                                                 TRACE("    - cRegions: %d\n", pInstrument->pHeader->cRegions);
663                                                                                                                                 TRACE("    - Locale:\n");
664                                                                                                                                 TRACE("       - ulBank: %d\n", pInstrument->pHeader->Locale.ulBank);
665                                                                                                                                 TRACE("       - ulInstrument: %d\n", pInstrument->pHeader->Locale.ulInstrument);
666                                                                                                                                 TRACE("       => dwPatch: %d\n", MIDILOCALE2Patch(&pInstrument->pHeader->Locale));
667                                                                                                                             }
668                                                                                                                             list_add_tail (&This->Instruments, &pNewInstrument->entry);
669                                                                                                                         }
670                                                                                                                         break;
671                                                                                                                 }
672                                                                                                         }
673                                                                                                         break;
674                                                                                                 }
675                                                                                                 default: {
676                                                                                                         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
677                                                                                                         liMove.QuadPart = Chunk.dwSize;
678                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
679                                                                                                         break;                                          
680                                                                                                 }
681                                                                                         }
682                                                                                         TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
683                                                                                 } while (ListCount[0] < ListSize[0]);
684                                                                                 break;
685                                                                         }
686                                                                         default: {
687                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
688                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
689                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
690                                                                                 break;                                          
691                                                                         }
692                                                                 }
693                                                                 break;
694                                                         }       
695                                                         default: {
696                                                                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
697                                                                 liMove.QuadPart = Chunk.dwSize;
698                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
699                                                                 break;                                          
700                                                         }
701                                                 }
702                                                 TRACE_(dmfile)(": StreamCount = %d < StreamSize = %d\n", StreamCount, StreamSize);
703                                         } while (StreamCount < StreamSize);
704                                         break;
705                                 }
706                                 default: {
707                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
708                                         liMove.QuadPart = StreamSize;
709                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
710                                         return E_FAIL;
711                                 }
712                         }
713                         TRACE_(dmfile)(": reading finished\n");
714                         break;
715                 }
716                 default: {
717                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
718                         liMove.QuadPart = Chunk.dwSize;
719                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
720                         return E_FAIL;
721                 }
722         }
723
724         /* DEBUG: dumps whole collection object tree: */
725         if (TRACE_ON(dmusic)) {
726                 int r = 0;
727                 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
728                 struct list *listEntry;
729
730                 TRACE("*** IDirectMusicCollection (%p) ***\n", This->CollectionVtbl);
731                 if (This->pDesc->dwValidData & DMUS_OBJ_OBJECT)
732                         TRACE(" - GUID = %s\n", debugstr_dmguid(&This->pDesc->guidObject));
733                 if (This->pDesc->dwValidData & DMUS_OBJ_VERSION)
734                         TRACE(" - Version = %i,%i,%i,%i\n", (This->pDesc->vVersion.dwVersionMS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionMS & 0x0000FFFF,
735                                 (This->pDesc->vVersion.dwVersionLS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionLS & 0x0000FFFF);
736                 if (This->pDesc->dwValidData & DMUS_OBJ_NAME)
737                         TRACE(" - Name = %s\n", debugstr_w(This->pDesc->wszName));
738                 
739                 TRACE(" - Collection header:\n");
740                 TRACE("    - cInstruments: %d\n", This->pHeader->cInstruments);
741                 TRACE(" - Instruments:\n");
742                 
743                 LIST_FOR_EACH (listEntry, &This->Instruments) {
744                         tmpEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry );
745                         TRACE("    - Instrument[%i]: %p\n", r, tmpEntry->pInstrument);
746                         r++;
747                 }
748         }
749         
750         return S_OK;
751 }
752
753 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
754         return E_NOTIMPL;
755 }
756
757 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
758         return E_NOTIMPL;
759 }
760
761 static const IPersistStreamVtbl DirectMusicCollection_PersistStream_Vtbl = {
762         IDirectMusicCollectionImpl_IPersistStream_QueryInterface,
763         IDirectMusicCollectionImpl_IPersistStream_AddRef,
764         IDirectMusicCollectionImpl_IPersistStream_Release,
765         IDirectMusicCollectionImpl_IPersistStream_GetClassID,
766         IDirectMusicCollectionImpl_IPersistStream_IsDirty,
767         IDirectMusicCollectionImpl_IPersistStream_Load,
768         IDirectMusicCollectionImpl_IPersistStream_Save,
769         IDirectMusicCollectionImpl_IPersistStream_GetSizeMax
770 };
771
772
773 /* for ClassFactory */
774 HRESULT WINAPI DMUSIC_CreateDirectMusicCollectionImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
775         IDirectMusicCollectionImpl* obj;
776         
777         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicCollectionImpl));
778         if (NULL == obj) {
779                 *ppobj = NULL;
780                 return E_OUTOFMEMORY;
781         }
782         obj->UnknownVtbl = &DirectMusicCollection_Unknown_Vtbl;
783         obj->CollectionVtbl = &DirectMusicCollection_Collection_Vtbl;
784         obj->ObjectVtbl = &DirectMusicCollection_Object_Vtbl;
785         obj->PersistStreamVtbl = &DirectMusicCollection_PersistStream_Vtbl;
786         obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
787         DM_STRUCT_INIT(obj->pDesc);
788         obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
789         obj->pDesc->guidClass = CLSID_DirectMusicCollection;
790         obj->ref = 0; /* will be inited by QueryInterface */
791         list_init (&obj->Instruments);
792
793         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
794 }