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