msvcrt: NULL terminate program arguments list in __getmainargs.
[wine] / dlls / dmloader / loader.c
1 /*
2  * IDirectMusicLoaderImpl
3  *
4  * Copyright (C) 2003-2004 Rok Mandeljc
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "dmloader_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
24
25 static inline IDirectMusicLoaderImpl* impl_from_IDirectMusicLoader8(IDirectMusicLoader8 *iface)
26 {
27     return CONTAINING_RECORD(iface, IDirectMusicLoaderImpl, IDirectMusicLoader8_iface);
28 }
29
30 static HRESULT DMUSIC_InitLoaderSettings(IDirectMusicLoader8 *iface);
31 static HRESULT DMUSIC_GetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID class_id, WCHAR *search_path, BOOL *cache);
32 static HRESULT DMUSIC_SetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID class_id, WCHAR *search_path, BOOL *cache);
33
34 static HRESULT DMUSIC_CopyDescriptor(DMUS_OBJECTDESC *pDst, DMUS_OBJECTDESC *pSrc)
35 {
36         if (TRACE_ON(dmloader))
37                 dump_DMUS_OBJECTDESC(pSrc);
38
39         /* copy field by field */
40         if (pSrc->dwValidData & DMUS_OBJ_CLASS) pDst->guidClass = pSrc->guidClass;
41         if (pSrc->dwValidData & DMUS_OBJ_OBJECT) pDst->guidObject = pSrc->guidObject;
42         if (pSrc->dwValidData & DMUS_OBJ_DATE) pDst->ftDate = pSrc->ftDate;
43         if (pSrc->dwValidData & DMUS_OBJ_VERSION) pDst->vVersion = pSrc->vVersion;
44         if (pSrc->dwValidData & DMUS_OBJ_NAME) strcpyW (pDst->wszName, pSrc->wszName);
45         if (pSrc->dwValidData & DMUS_OBJ_CATEGORY) strcpyW (pDst->wszCategory, pSrc->wszCategory);
46         if (pSrc->dwValidData & DMUS_OBJ_FILENAME) strcpyW (pDst->wszFileName, pSrc->wszFileName);
47         if (pSrc->dwValidData & DMUS_OBJ_STREAM) IStream_Clone (pSrc->pStream, &pDst->pStream);
48         if (pSrc->dwValidData & DMUS_OBJ_MEMORY) {
49                 pDst->pbMemData = pSrc->pbMemData;
50                 pDst->llMemLength = pSrc->llMemLength;
51         }
52         /* set flags */
53         pDst->dwValidData |= pSrc->dwValidData;
54         return S_OK;
55 }
56
57
58 static BOOL DMUSIC_IsValidLoadableClass (REFCLSID pClassID) {
59         if (IsEqualCLSID(pClassID, &CLSID_DirectMusicAudioPathConfig) ||
60                 IsEqualCLSID(pClassID, &CLSID_DirectMusicBand) ||
61                 IsEqualCLSID(pClassID, &CLSID_DirectMusicContainer) ||
62                 IsEqualCLSID(pClassID, &CLSID_DirectMusicCollection) ||
63                 IsEqualCLSID(pClassID, &CLSID_DirectMusicChordMap) ||
64                 IsEqualCLSID(pClassID, &CLSID_DirectMusicSegment) ||
65                 IsEqualCLSID(pClassID, &CLSID_DirectMusicScript) ||
66                 IsEqualCLSID(pClassID, &CLSID_DirectMusicSong) ||
67                 IsEqualCLSID(pClassID, &CLSID_DirectMusicStyle) ||
68                 IsEqualCLSID(pClassID, &CLSID_DirectMusicGraph) ||
69                 IsEqualCLSID(pClassID, &CLSID_DirectSoundWave) ||
70                 IsEqualCLSID(pClassID, &GUID_DirectMusicAllTypes))
71                 return TRUE;
72         else
73                 return FALSE;
74 }
75
76 /*****************************************************************************
77  * IDirectMusicLoaderImpl implementation
78  */
79 /* IUnknown/IDirectMusicLoader(8) part: */
80
81 static HRESULT WINAPI IDirectMusicLoaderImpl_QueryInterface(IDirectMusicLoader8 *iface, REFIID riid, void **ppobj)
82 {
83         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
84
85         TRACE("(%p, %s, %p)\n",This, debugstr_dmguid(riid), ppobj);
86         if (IsEqualIID (riid, &IID_IUnknown) || 
87             IsEqualIID (riid, &IID_IDirectMusicLoader) ||
88             IsEqualIID (riid, &IID_IDirectMusicLoader8)) {
89                 IDirectMusicLoader_AddRef (iface);
90                 *ppobj = This;
91                 return S_OK;
92         }
93         
94         WARN(": not found\n");
95         return E_NOINTERFACE;
96 }
97
98 static ULONG WINAPI IDirectMusicLoaderImpl_AddRef(IDirectMusicLoader8 *iface)
99 {
100     IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
101     ULONG ref = InterlockedIncrement(&This->ref);
102
103     TRACE("(%p)->(): new ref = %u\n", iface, ref);
104
105     return ref;
106 }
107
108 static ULONG WINAPI IDirectMusicLoaderImpl_Release(IDirectMusicLoader8 *iface)
109 {
110     IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
111     ULONG ref = InterlockedDecrement(&This->ref);
112
113     TRACE("(%p)->(): new ref = %u\n", iface, ref);
114
115     if (!ref) {
116         /* Firstly, release the cache */
117         IDirectMusicLoader8_ClearCache(iface, &GUID_DirectMusicAllTypes);
118         /* FIXME: Release all allocated entries */
119         HeapFree(GetProcessHeap(), 0, This);
120         unlock_module();
121     }
122
123     return ref;
124 }
125
126 static HRESULT WINAPI IDirectMusicLoaderImpl_GetObject(IDirectMusicLoader8 *iface, DMUS_OBJECTDESC *pDesc, REFIID riid, void **ppv)
127 {
128         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
129         HRESULT result = S_OK;
130         HRESULT ret = S_OK; /* used at the end of function, to determine whether everything went OK */
131         
132         struct list *pEntry;
133         LPWINE_LOADER_ENTRY pObjectEntry = NULL;
134         LPSTREAM pStream;
135         IPersistStream* pPersistStream = NULL;
136
137         LPDIRECTMUSICOBJECT pObject;
138         DMUS_OBJECTDESC GotDesc;
139         BOOL bCache;
140
141         TRACE("(%p)->(%p, %s, %p)\n", This, pDesc, debugstr_dmguid(riid), ppv);
142
143         if (TRACE_ON(dmloader))
144           dump_DMUS_OBJECTDESC(pDesc);
145         
146         /* sometimes it happens that guidClass is missing... which is a BadThingTM */
147         if (!(pDesc->dwValidData & DMUS_OBJ_CLASS)) {
148           ERR(": guidClass not valid but needed\n");
149           *ppv = NULL;
150           return DMUS_E_LOADER_NOCLASSID;
151         }
152         
153         /* OK, first we iterate through the list of objects we know about; these are either loaded (GetObject, LoadObjectFromFile)
154            or set via SetObject; */
155         TRACE(": looking if we have object in the cache or if it can be found via alias\n");
156         LIST_FOR_EACH(pEntry, This->pObjects) {
157                 LPWINE_LOADER_ENTRY pExistingEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
158                 if ((pDesc->dwValidData & DMUS_OBJ_OBJECT) &&
159                         (pExistingEntry->Desc.dwValidData & DMUS_OBJ_OBJECT) &&
160                         IsEqualGUID (&pDesc->guidObject, &pExistingEntry->Desc.guidObject)) {
161                         TRACE(": found it by object GUID\n");
162                         /* I suppose such stuff can happen only when GUID for object is given (GUID_DefaultGMCollection) */
163                         if (pExistingEntry->bInvalidDefaultDLS) {
164                                 TRACE(": found faulty default DLS collection... enabling M$ compliant behaviour\n");
165                                 return DMUS_E_LOADER_NOFILENAME;        
166                         }
167                         if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
168                                 TRACE(": already loaded\n");
169                                 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
170                         } else {
171                                 TRACE(": not loaded yet\n");
172                                 pObjectEntry = pExistingEntry;
173                         }
174                 }
175                 else if ((pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
176                                 (pExistingEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
177                                 !strncmpW (pDesc->wszFileName, pExistingEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
178                         TRACE(": found it by fullpath filename\n");
179                         if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
180                                 TRACE(": already loaded\n");
181                                 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
182                         } else {
183                                 TRACE(": not loaded yet\n");
184                                 pObjectEntry = pExistingEntry;
185                         }
186                 }
187                 else if ((pDesc->dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
188                                 (pExistingEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
189                                 !strncmpW (pDesc->wszName, pExistingEntry->Desc.wszName, DMUS_MAX_NAME) &&
190                                 !strncmpW (pDesc->wszCategory, pExistingEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
191                         TRACE(": found it by name and category\n");
192                         if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
193                                 TRACE(": already loaded\n");
194                                 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
195                         } else {
196                                 TRACE(": not loaded yet\n");
197                                 pObjectEntry = pExistingEntry;
198                         }
199                 }
200                 else if ((pDesc->dwValidData & DMUS_OBJ_NAME) &&
201                                 (pExistingEntry->Desc.dwValidData & DMUS_OBJ_NAME) &&
202                                 !strncmpW (pDesc->wszName, pExistingEntry->Desc.wszName, DMUS_MAX_NAME)) {
203                         TRACE(": found it by name\n");
204                         if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
205                                 TRACE(": already loaded\n");
206                                 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
207                         } else {
208                                 TRACE(": not loaded yet\n");
209                                 pObjectEntry = pExistingEntry;
210                         }
211                 }
212                 else if ((pDesc->dwValidData & DMUS_OBJ_FILENAME) &&
213                                 (pExistingEntry->Desc.dwValidData & DMUS_OBJ_FILENAME) &&
214                                 !strncmpW (pDesc->wszFileName, pExistingEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
215                         TRACE(": found it by filename\n");                              
216                         if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
217                                 TRACE(": already loaded\n");
218                                 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
219                         } else {
220                                 TRACE(": not loaded yet\n");
221                                 pObjectEntry = pExistingEntry;
222                         }
223                 }
224         }
225         
226         /* basically, if we found alias, we use its descriptor to load...
227            else we use info we were given */
228         if (pObjectEntry) {
229                 TRACE(": found alias entry for requested object... using stored info\n");
230                 /* I think in certain cases it can happen that entry's descriptor lacks info about
231                    where to load from (e.g.: if we loaded from stream and then released object
232                    from cache; then only its CLSID, GUID and perhaps name are left); so just
233                    overwrite whatever info the entry has (since it ought to be 100% correct) */
234                 DMUSIC_CopyDescriptor (pDesc, &pObjectEntry->Desc);
235                 /*pDesc = &pObjectEntry->Desc; */ /* FIXME: is this OK? */
236         } else {
237                 TRACE(": no cache/alias entry found for requested object\n");
238         }
239         
240         if (pDesc->dwValidData & DMUS_OBJ_URL) {
241                 TRACE(": loading from URLs not supported yet\n");
242                 return DMUS_E_LOADER_FORMATNOTSUPPORTED;
243         }
244         else if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
245                 /* load object from file */
246                 /* generate filename; if it's full path, don't add search 
247                    directory path, otherwise do */
248                 WCHAR wszFileName[MAX_PATH];
249
250                 if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) {
251                         lstrcpyW(wszFileName, pDesc->wszFileName);
252                 } else {
253                         WCHAR *p, wszSearchPath[MAX_PATH];
254                         DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, wszSearchPath, NULL);
255                         lstrcpyW(wszFileName, wszSearchPath);
256                         p = wszFileName + lstrlenW(wszFileName);
257                         if (p > wszFileName && p[-1] != '\\') *p++ = '\\';
258                         strcpyW(p, pDesc->wszFileName);
259                 }
260                 TRACE(": loading from file (%s)\n", debugstr_w(wszFileName));
261                 /* create stream and associate it with file */                  
262                 result = DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID*)&pStream);
263                 if (FAILED(result)) {
264                         ERR(": could not create file stream\n");
265                         return result;
266                 }
267                 result = IDirectMusicLoaderFileStream_Attach (pStream, wszFileName, iface);
268                 if (FAILED(result)) {
269                         ERR(": could not attach stream to file\n");
270                         IStream_Release (pStream);
271                         return result;
272                 }
273         }
274         else if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
275                 /* load object from resource */
276                 TRACE(": loading from resource\n");
277                 /* create stream and associate it with given resource */                        
278                 result = DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID*)&pStream);
279                 if (FAILED(result)) {
280                         ERR(": could not create resource stream\n");
281                         return result;
282                 }
283                 result = IDirectMusicLoaderResourceStream_Attach (pStream, pDesc->pbMemData, pDesc->llMemLength, 0, iface);
284                 if (FAILED(result)) {
285                         ERR(": could not attach stream to resource\n");
286                         IStream_Release (pStream);
287                         return result;
288                 }
289         }
290         else if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
291                 /* load object from stream */
292                 TRACE(": loading from stream\n");
293                 /* create universal stream and associate it with given one */                   
294                 result = DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID*)&pStream);
295                 if (FAILED(result)) {
296                         ERR(": could not create generic stream\n");
297                         return result;
298                 }
299                 result = IDirectMusicLoaderGenericStream_Attach (pStream, pDesc->pStream, iface);
300                 if (FAILED(result)) {
301                         ERR(": failed to attach stream\n");
302                         IStream_Release (pStream);
303                         return result;
304                 }
305         } else {
306                 /* nowhere to load from */
307                 FIXME(": unknown/unsupported way of loading\n");
308                 return DMUS_E_LOADER_NOFILENAME; /* test shows this is returned */
309         }
310
311         /* create object */
312         result = CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
313         if (FAILED(result)) {
314                 ERR(": could not create object\n");
315                 return result;
316         }
317         /* acquire PersistStream interface */
318         result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream);
319         if (FAILED(result)) {
320                 ERR("failed to Query\n");
321                 return result;
322         }
323         /* load */
324         result = IPersistStream_Load (pPersistStream, pStream);
325         if (result != S_OK) {
326                 WARN(": failed to (completely) load object (%s)\n", debugstr_dmreturn(result));
327                 return result;
328         }
329         /* get descriptor */
330         DM_STRUCT_INIT(&GotDesc);
331         result = IDirectMusicObject_GetDescriptor (pObject, &GotDesc);
332         /* set filename (if we loaded via filename) */
333         if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
334                 GotDesc.dwValidData |= (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH));
335                 strcpyW (GotDesc.wszFileName, pDesc->wszFileName);
336         }
337         if (FAILED(result)) {
338                 ERR(": failed to get descriptor\n");
339                 return result;
340         }
341         /* release all loading related stuff */
342         IStream_Release (pStream);
343         IPersistStream_Release (pPersistStream);        
344                 
345         /* add object to cache/overwrite existing info (if cache is enabled) */
346         DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, NULL, &bCache);
347         if (bCache) {
348                 if (!pObjectEntry) {
349                         pObjectEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_ENTRY));
350                         DM_STRUCT_INIT(&pObjectEntry->Desc);
351                         DMUSIC_CopyDescriptor (&pObjectEntry->Desc, &GotDesc);
352                         pObjectEntry->pObject = pObject;
353                         pObjectEntry->bInvalidDefaultDLS = FALSE;
354                         list_add_head (This->pObjects, &pObjectEntry->entry);
355                 } else {
356                         DMUSIC_CopyDescriptor (&pObjectEntry->Desc, &GotDesc);
357                         pObjectEntry->pObject = pObject;
358                         pObjectEntry->bInvalidDefaultDLS = FALSE;
359                 }
360                 TRACE(": filled in cache entry\n");
361         } else TRACE(": caching disabled\n");
362
363 #if 0
364         /* for debug purposes (e.g. to check if all files are cached) */
365         TRACE("*** Loader's cache ***\n");
366         int i = 0;
367         LIST_FOR_EACH (pEntry, This->pObjects) {
368                 i++;
369                 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
370                 TRACE(": entry nr. %i:\n%s\n  - bInvalidDefaultDLS = %i\n  - pObject = %p\n", i, debugstr_DMUS_OBJECTDESC(&pObjectEntry->Desc), pObjectEntry->bInvalidDefaultDLS, pObjectEntry->pObject);
371         }
372 #endif
373         
374         result = IDirectMusicObject_QueryInterface (pObject, riid, ppv);
375         if (!bCache) IDirectMusicObject_Release (pObject); /* since loader's reference is not needed */
376         /* if there was trouble with loading, and if no other error occurred,
377            we should return DMUS_S_PARTIALLOAD; else, error is returned */
378         if (result == S_OK)
379                 return ret;
380         else
381                 return result;
382 }
383
384 static HRESULT WINAPI IDirectMusicLoaderImpl_SetObject(IDirectMusicLoader8 *iface, DMUS_OBJECTDESC *pDesc)
385 {
386         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
387         LPSTREAM pStream;
388         LPDIRECTMUSICOBJECT pObject;
389         DMUS_OBJECTDESC Desc;
390         struct list *pEntry;
391         LPWINE_LOADER_ENTRY pObjectEntry, pNewEntry;
392         HRESULT hr;
393
394         TRACE("(%p)->(%p)\n", This, pDesc);
395
396         if (TRACE_ON(dmloader))
397                 dump_DMUS_OBJECTDESC(pDesc);
398
399         /* create stream and load additional info from it */
400         if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
401                 /* generate filename; if it's full path, don't add search 
402                    directory path, otherwise do */
403                 WCHAR wszFileName[MAX_PATH];
404
405                 if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) {
406                         lstrcpyW(wszFileName, pDesc->wszFileName);
407                 } else {
408                         WCHAR *p;
409                         WCHAR wszSearchPath[MAX_PATH];
410                         DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, wszSearchPath, NULL);
411                         lstrcpyW(wszFileName, wszSearchPath);
412                         p = wszFileName + lstrlenW(wszFileName);
413                         if (p > wszFileName && p[-1] != '\\') *p++ = '\\';
414                         strcpyW(p, pDesc->wszFileName);
415                 }
416                 /* create stream */
417                 hr = DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID*)&pStream);
418                 if (FAILED(hr)) {
419                         ERR(": could not create file stream\n");
420                         return DMUS_E_LOADER_FAILEDOPEN;
421                 }
422                 /* attach stream */
423                 hr = IDirectMusicLoaderFileStream_Attach (pStream, wszFileName, iface);
424                 if (FAILED(hr)) {
425                         ERR(": could not attach stream to file\n");
426                         IStream_Release (pStream);
427                         return DMUS_E_LOADER_FAILEDOPEN;
428                 }
429         }
430         else if (pDesc->dwValidData & DMUS_OBJ_STREAM) {        
431                 /* create stream */
432                 hr = DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID*)&pStream);
433                 if (FAILED(hr)) {
434                         ERR(": could not create generic stream\n");
435                         return DMUS_E_LOADER_FAILEDOPEN;
436                 }
437                 /* attach stream */
438                 hr = IDirectMusicLoaderGenericStream_Attach (pStream, pDesc->pStream, iface);
439                 if (FAILED(hr)) {
440                         ERR(": could not attach stream\n");
441                         IStream_Release (pStream);
442                         return DMUS_E_LOADER_FAILEDOPEN;
443                 }
444         }
445         else if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
446                 /* create stream */
447                 hr = DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID*)&pStream);
448                 if (FAILED(hr)) {
449                         ERR(": could not create resource stream\n");
450                         return DMUS_E_LOADER_FAILEDOPEN;
451                 }
452                 /* attach stream */
453                 hr = IDirectMusicLoaderResourceStream_Attach (pStream, pDesc->pbMemData, pDesc->llMemLength, 0, iface);
454                 if (FAILED(hr)) {
455                         ERR(": could not attach stream to resource\n");
456                         IStream_Release (pStream);
457                         return DMUS_E_LOADER_FAILEDOPEN;
458                 }
459         }
460         else {
461                 ERR(": no way to get additional info\n");
462                 return DMUS_E_LOADER_FAILEDOPEN;
463         }
464
465         /* create object */
466         CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
467
468         /* *sigh*... some ms objects have lousy implementation of ParseDescriptor that clears input descriptor :( */
469 #ifdef NOW_WE_ARE_FREE
470         /* parse descriptor: we actually use input descriptor; fields that aren't available from stream remain,
471            otherwise real info is set */
472          IDirectMusicObject_ParseDescriptor (pObject, pStream, pDesc);
473 #endif
474         /* hmph... due to some trouble I had with certain tests, we store current position and then set it back */
475         DM_STRUCT_INIT(&Desc);
476         if (FAILED(IDirectMusicObject_ParseDescriptor (pObject, pStream, &Desc))) {
477                 ERR(": couldn't parse descriptor\n");
478                 return DMUS_E_LOADER_FORMATNOTSUPPORTED;
479         }
480
481         /* copy elements from parsed descriptor into input descriptor; this sets new info, overwriting if necessary,
482            but leaves info that's provided by input and not available from stream */    
483         DMUSIC_CopyDescriptor (pDesc, &Desc);
484         
485         /* release everything */
486         IDirectMusicObject_Release (pObject);
487         IStream_Release (pStream);      
488         
489         /* sometimes it happens that twisted programs call SetObject for same object twice...
490            in such cases, native loader returns S_OK and does nothing... a sound plan */
491         LIST_FOR_EACH (pEntry, This->pObjects) {
492                 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
493                 if (!memcmp (&pObjectEntry->Desc, pDesc, sizeof(DMUS_OBJECTDESC))) {
494                         TRACE(": exactly same entry already exists\n");
495                         return S_OK;
496                 }
497         }               
498         
499         /* add new entry */
500         TRACE(": adding alias entry with following info:\n");
501         if (TRACE_ON(dmloader))
502                 dump_DMUS_OBJECTDESC(pDesc);
503         pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_ENTRY));
504         /* use this function instead of pure memcpy due to streams (memcpy just copies pointer), 
505            which is basically used further by app that called SetDescriptor... better safety than exception */
506         DMUSIC_CopyDescriptor (&pNewEntry->Desc, pDesc);
507         list_add_head (This->pObjects, &pNewEntry->entry);
508
509         return S_OK;
510 }
511
512 static HRESULT WINAPI IDirectMusicLoaderImpl_SetSearchDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzPath, BOOL fClear)
513 {
514         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
515         WCHAR wszCurrentPath[MAX_PATH];
516         TRACE("(%p, %s, %s, %d)\n", This, debugstr_dmguid(rguidClass), debugstr_w(pwzPath), fClear);
517         FIXME(": fClear ignored\n");
518         DMUSIC_GetLoaderSettings (iface, rguidClass, wszCurrentPath, NULL);
519         if (!strncmpW(wszCurrentPath, pwzPath, MAX_PATH)) {
520           return S_FALSE;
521         }
522         /* FIXME: check if path is valid; else return DMUS_E_LOADER_BADPATH */
523         return DMUSIC_SetLoaderSettings (iface, rguidClass, pwzPath, NULL);
524 }
525
526 static HRESULT WINAPI IDirectMusicLoaderImpl_ScanDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzFileExtension, WCHAR *pwzScanFileName)
527 {
528         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
529         static const WCHAR wszAny[] = {'*',0};
530         WIN32_FIND_DATAW FileData;
531         HANDLE hSearch;
532         WCHAR wszSearchString[MAX_PATH];
533         WCHAR *p;
534         HRESULT result;
535         TRACE("(%p, %s, %p, %p)\n", This, debugstr_dmguid(rguidClass), pwzFileExtension, pwzScanFileName);
536         if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || !DMUSIC_IsValidLoadableClass(rguidClass)) {
537                 ERR(": rguidClass invalid CLSID\n");
538                 return REGDB_E_CLASSNOTREG;
539         }
540         
541         /* get search path for given class */
542         DMUSIC_GetLoaderSettings (iface, rguidClass, wszSearchString, NULL);
543         
544         p = wszSearchString + lstrlenW(wszSearchString);
545         if (p > wszSearchString && p[-1] != '\\') *p++ = '\\';
546         *p++ = '*'; /* any file */
547         if (strcmpW (pwzFileExtension, wszAny)) *p++ = '.'; /* if we have actual extension, put a dot */
548         strcpyW (p, pwzFileExtension);
549         
550         TRACE(": search string: %s\n", debugstr_w(wszSearchString));
551         
552         hSearch = FindFirstFileW (wszSearchString, &FileData);
553         if (hSearch == INVALID_HANDLE_VALUE) {
554                 TRACE(": no files found\n");
555                 return S_FALSE;
556         }
557         
558         do {
559                 DMUS_OBJECTDESC Desc;
560                 DM_STRUCT_INIT(&Desc);
561                 Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_DATE;
562                 Desc.guidClass = *rguidClass;
563                 strcpyW (Desc.wszFileName, FileData.cFileName);
564                 FileTimeToLocalFileTime (&FileData.ftCreationTime, &Desc.ftDate);
565                 IDirectMusicLoader8_SetObject (iface, &Desc);
566                 
567                 if (!FindNextFileW (hSearch, &FileData)) {
568                         if (GetLastError () == ERROR_NO_MORE_FILES) {
569                                 TRACE(": search completed\n");
570                                 result = S_OK;
571                         } else {
572                                 ERR(": could not get next file\n");
573                                 result = E_FAIL;
574                         }
575                         FindClose (hSearch);
576                         return result;
577                 }
578         } while (1);
579 }
580
581 static HRESULT WINAPI IDirectMusicLoaderImpl_CacheObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
582 {
583         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
584         DMUS_OBJECTDESC Desc;
585         HRESULT result = DMUS_E_LOADER_OBJECTNOTFOUND;
586         struct list *pEntry;
587         LPWINE_LOADER_ENTRY  pObjectEntry = NULL;
588
589         TRACE("(%p, %p)\n", This, pObject);
590         
591         /* get descriptor */
592         DM_STRUCT_INIT(&Desc);
593         IDirectMusicObject_GetDescriptor (pObject, &Desc);
594         
595         /* now iterate through the list and check if we have an alias (without object), corresponding
596            to the descriptor of the input object */
597         LIST_FOR_EACH(pEntry, This->pObjects) {
598                 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
599                 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
600                         (pObjectEntry->Desc.dwValidData & DMUS_OBJ_OBJECT) &&
601                         IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
602                         TRACE(": found it by object GUID\n");
603                         if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
604                                 result = S_FALSE;
605                         else
606                                 result = S_OK;
607                         break;
608                 }
609                 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
610                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
611                                 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
612                         TRACE(": found it by fullpath filename\n");
613                         if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
614                                 result = S_FALSE;
615                         else
616                                 result = S_OK;
617                         break;
618                 }
619                 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
620                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
621                                 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
622                                 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
623                         TRACE(": found it by name and category\n");
624                         if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
625                                 result = S_FALSE;
626                         else
627                                 result = S_OK;
628                         break;
629                 }
630                 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
631                                 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_NAME) &&
632                                 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
633                         TRACE(": found it by name\n");
634                         if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
635                                 result = S_FALSE;
636                         else
637                                 result = S_OK;
638                         break;
639                 }
640                 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
641                                 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_FILENAME) &&
642                                 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
643                         TRACE(": found it by filename\n");                              
644                         if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
645                                 result = S_FALSE;
646                         else
647                                 result = S_OK;
648                         break;
649                 }
650         }
651         
652         /* if we found such alias, then set everything */
653         if (result == S_OK) {
654                 pObjectEntry->Desc.dwValidData &= DMUS_OBJ_LOADED;
655                 pObjectEntry->pObject = pObject;
656                 IDirectMusicObject_AddRef (pObjectEntry->pObject);
657         }
658         
659         return result;
660 }
661
662 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
663 {
664         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
665         DMUS_OBJECTDESC Desc;
666         struct list *pEntry;
667         LPWINE_LOADER_ENTRY pObjectEntry = NULL;
668         HRESULT result = S_FALSE;
669
670         TRACE("(%p, %p)\n", This, pObject);
671         
672         if(!pObject) return E_POINTER;
673
674         /* get descriptor */
675         DM_STRUCT_INIT(&Desc);
676         IDirectMusicObject_GetDescriptor (pObject, &Desc);
677         
678         /* iterate through the list of objects we know about; check only those with DMUS_OBJ_LOADED */
679         TRACE(": looking for the object in cache\n");
680         LIST_FOR_EACH(pEntry, This->pObjects) {
681                 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
682                 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
683                         (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_OBJECT | DMUS_OBJ_LOADED)) &&
684                         IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
685                         TRACE(": found it by object GUID\n");
686                         if (TRACE_ON(dmloader))
687                                 dump_DMUS_OBJECTDESC(&pObjectEntry->Desc);
688                         result = S_OK;
689                         break;
690                 }
691                 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
692                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_LOADED)) &&
693                                 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
694                         TRACE(": found it by fullpath filename\n");
695                         result = S_OK;                  
696                         break;
697                 }
698                 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
699                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY | DMUS_OBJ_LOADED)) &&
700                                 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
701                                 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
702                         TRACE(": found it by name and category\n");
703                         result = S_OK;                  
704                         break;
705                 }
706                 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
707                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_LOADED)) &&
708                                 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
709                         TRACE(": found it by name\n");
710                         result = S_OK;
711                         break;
712                 }
713                 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
714                                 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_LOADED)) &&
715                                 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
716                         TRACE(": found it by filename\n");
717                         result = S_OK;                  
718                         break;
719                 }
720         }
721         if (result == S_OK) {
722                 /*TRACE(": releasing:\n%s  - bInvalidDefaultDLS = %i\n  - pObject = %p\n", debugstr_DMUS_OBJECTDESC(&pObjectEntry->Desc), pObjectEntry->bInvalidDefaultDLS, pObjectEntry->pObject); */
723                 IDirectMusicObject_Release (pObjectEntry->pObject);
724                 pObjectEntry->pObject = NULL;
725                 pObjectEntry->Desc.dwValidData &= ~DMUS_OBJ_LOADED;
726         } 
727         return result;
728 }
729
730 static HRESULT WINAPI IDirectMusicLoaderImpl_ClearCache(IDirectMusicLoader8 *iface, REFGUID rguidClass)
731 {
732         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
733         struct list *pEntry;
734         LPWINE_LOADER_ENTRY pObjectEntry;
735         TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidClass));
736         
737         LIST_FOR_EACH (pEntry, This->pObjects) {
738                 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
739                 
740                 if ((IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) &&
741                         (pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED)) {
742                         /* basically, wrap to ReleaseObject for each object found */
743                         IDirectMusicLoader8_ReleaseObject (iface, pObjectEntry->pObject);
744                 }
745         }
746         
747         return S_OK;
748 }
749
750 static HRESULT WINAPI IDirectMusicLoaderImpl_EnableCache(IDirectMusicLoader8 *iface, REFGUID rguidClass, BOOL fEnable)
751 {
752         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
753         BOOL bCurrent;
754         TRACE("(%p, %s, %d)\n", This, debugstr_dmguid(rguidClass), fEnable);
755         DMUSIC_GetLoaderSettings (iface, rguidClass, NULL, &bCurrent);
756         if (bCurrent == fEnable)
757                 return S_FALSE;
758         else
759                 return DMUSIC_SetLoaderSettings (iface, rguidClass, NULL, &fEnable);
760 }
761
762 static HRESULT WINAPI IDirectMusicLoaderImpl_EnumObject(IDirectMusicLoader8 *iface, REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc)
763 {
764         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
765         DWORD dwCount = 0;
766         struct list *pEntry;
767         LPWINE_LOADER_ENTRY pObjectEntry;
768         TRACE("(%p, %s, %d, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc);
769
770         DM_STRUCT_INIT(pDesc);
771
772         LIST_FOR_EACH (pEntry, This->pObjects) {
773                 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
774
775                 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) {
776                         if (dwCount == dwIndex) {
777                                 *pDesc = pObjectEntry->Desc;
778                                 /* we aren't supposed to reveal this info */
779                                 pDesc->dwValidData &= ~(DMUS_OBJ_MEMORY | DMUS_OBJ_STREAM);
780                                 pDesc->pbMemData = NULL;
781                                 pDesc->llMemLength = 0;
782                                 pDesc->pStream = NULL;
783                                 return S_OK;
784                         }
785                         dwCount++;
786                 }
787         }
788         
789         TRACE(": not found\n"); 
790         return S_FALSE;
791 }
792
793 static void WINAPI IDirectMusicLoaderImpl_CollectGarbage(IDirectMusicLoader8 *iface)
794 {
795     FIXME("(%p)->(): stub\n", iface);
796 }
797
798 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObjectByUnknown(IDirectMusicLoader8 *iface, IUnknown *pObject)
799 {
800         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
801         HRESULT result;
802         LPDIRECTMUSICOBJECT pObjectInterface;
803         
804         TRACE("(%p, %p)\n", This, pObject);
805         
806         if (IsBadReadPtr (pObject, sizeof(*pObject))) {
807                 ERR(": pObject bad write pointer\n");
808                 return E_POINTER;
809         }
810         /* we simply get IDirectMusicObject interface */
811         result = IUnknown_QueryInterface (pObject, &IID_IDirectMusicObject, (LPVOID*)&pObjectInterface);
812         if (FAILED(result)) return result;
813         /* and release it in old-fashioned way */
814         result = IDirectMusicLoader8_ReleaseObject (iface, pObjectInterface);
815         IDirectMusicObject_Release (pObjectInterface);
816         
817         return result;
818 }
819
820 static HRESULT WINAPI IDirectMusicLoaderImpl_LoadObjectFromFile(IDirectMusicLoader8 *iface, REFGUID rguidClassID, REFIID iidInterfaceID, WCHAR *pwzFilePath, void **ppObject)
821 {
822         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
823         DMUS_OBJECTDESC ObjDesc;
824         WCHAR wszLoaderSearchPath[MAX_PATH];
825
826         TRACE("(%p, %s, %s, %s, %p): wrapping to IDirectMusicLoaderImpl_GetObject\n", This, debugstr_dmguid(rguidClassID), debugstr_dmguid(iidInterfaceID), debugstr_w(pwzFilePath), ppObject);
827
828         DM_STRUCT_INIT(&ObjDesc);       
829         ObjDesc.dwValidData = DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_CLASS; /* I believe I've read somewhere in MSDN that this function requires either full path or relative path */
830         ObjDesc.guidClass = *rguidClassID;
831         /* OK, MSDN says that search order is the following:
832             - current directory (DONE)
833             - windows search path (FIXME: how do I get that?)
834             - loader's search path (DONE)
835         */
836         DMUSIC_GetLoaderSettings (iface, rguidClassID, wszLoaderSearchPath, NULL);
837     /* search in current directory */
838         if (!SearchPathW (NULL, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL) &&
839         /* search in loader's search path */
840                 !SearchPathW (wszLoaderSearchPath, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL)) {
841                 /* cannot find file */
842                 TRACE(": cannot find file\n");
843                 return DMUS_E_LOADER_FAILEDOPEN;
844         }
845         
846         TRACE(": full file path = %s\n", debugstr_w (ObjDesc.wszFileName));
847         
848         return IDirectMusicLoader_GetObject(iface, &ObjDesc, iidInterfaceID, ppObject);
849 }
850
851 static const IDirectMusicLoader8Vtbl DirectMusicLoader_Loader_Vtbl = {
852     IDirectMusicLoaderImpl_QueryInterface,
853     IDirectMusicLoaderImpl_AddRef,
854     IDirectMusicLoaderImpl_Release,
855     IDirectMusicLoaderImpl_GetObject,
856     IDirectMusicLoaderImpl_SetObject,
857     IDirectMusicLoaderImpl_SetSearchDirectory,
858     IDirectMusicLoaderImpl_ScanDirectory,
859     IDirectMusicLoaderImpl_CacheObject,
860     IDirectMusicLoaderImpl_ReleaseObject,
861     IDirectMusicLoaderImpl_ClearCache,
862     IDirectMusicLoaderImpl_EnableCache,
863     IDirectMusicLoaderImpl_EnumObject,
864     IDirectMusicLoaderImpl_CollectGarbage,
865     IDirectMusicLoaderImpl_ReleaseObjectByUnknown,
866     IDirectMusicLoaderImpl_LoadObjectFromFile
867 };
868
869 /* help function for DMUSIC_SetDefaultDLS */
870 static HRESULT DMUSIC_GetDefaultGMPath (WCHAR wszPath[MAX_PATH]) {
871         HKEY hkDM;
872         DWORD returnType, sizeOfReturnBuffer = MAX_PATH;
873         char szPath[MAX_PATH];
874
875         if ((RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic" , 0, KEY_READ, &hkDM) != ERROR_SUCCESS) ||
876             (RegQueryValueExA (hkDM, "GMFilePath", NULL, &returnType, (LPBYTE) szPath, &sizeOfReturnBuffer) != ERROR_SUCCESS)) {
877                 WARN(": registry entry missing\n" );
878                 return E_FAIL;
879         }
880         /* FIXME: Check return types to ensure we're interpreting data right */
881         MultiByteToWideChar (CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
882
883         return S_OK;
884 }
885
886 /* for ClassFactory */
887 HRESULT WINAPI DMUSIC_CreateDirectMusicLoaderImpl(const GUID *lpcGUID, void **ppobj, IUnknown *pUnkOuter)
888 {
889         IDirectMusicLoaderImpl *obj;
890         DMUS_OBJECTDESC Desc;
891         LPWINE_LOADER_ENTRY pDefaultDLSEntry;
892         struct list *pEntry;
893
894         TRACE("(%s, %p, %p)\n", debugstr_dmguid(lpcGUID), ppobj, pUnkOuter);
895         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicLoaderImpl));
896         if (NULL == obj) {
897                 *ppobj = NULL;
898                 return E_OUTOFMEMORY;
899         }
900         obj->IDirectMusicLoader8_iface.lpVtbl = &DirectMusicLoader_Loader_Vtbl;
901         obj->ref = 0; /* Will be inited with QueryInterface */
902         /* init cache/alias list */
903         obj->pObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
904         list_init (obj->pObjects);
905         /* init settings */
906         obj->pClassSettings = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
907         list_init (obj->pClassSettings);
908         DMUSIC_InitLoaderSettings(&obj->IDirectMusicLoader8_iface);
909
910         /* set default DLS collection (via SetObject... so that loading via DMUS_OBJ_OBJECT is possible) */
911         DM_STRUCT_INIT(&Desc);
912         Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_OBJECT;
913         Desc.guidClass = CLSID_DirectMusicCollection;
914         Desc.guidObject = GUID_DefaultGMCollection;
915         DMUSIC_GetDefaultGMPath (Desc.wszFileName);
916         IDirectMusicLoader_SetObject(&obj->IDirectMusicLoader8_iface, &Desc);
917         /* and now the workaroundTM for "invalid" default DLS; basically, 
918            my tests showed that if GUID chunk is present in default DLS 
919            collection, loader treats it as "invalid" and returns 
920            DMUS_E_LOADER_NOFILENAME for all requests for it; basically, we check 
921            if out input guidObject was overwritten */
922         pEntry = list_head (obj->pObjects);
923         pDefaultDLSEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
924         if (!IsEqualGUID(&Desc.guidObject, &GUID_DefaultGMCollection)) {
925                 pDefaultDLSEntry->bInvalidDefaultDLS = TRUE;
926         }
927
928         lock_module();
929
930         return IDirectMusicLoader_QueryInterface(&obj->IDirectMusicLoader8_iface, lpcGUID, ppobj);
931 }
932
933 /* help function for retrieval of search path and caching option for certain class */
934 static HRESULT DMUSIC_GetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
935 {
936         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
937         struct list *pEntry;
938         TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
939         
940         LIST_FOR_EACH(pEntry, This->pClassSettings) {
941                 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
942                 if (IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
943                         if (wszSearchPath)
944                                 strcpyW(wszSearchPath, pOptionEntry->wszSearchPath);
945                         if (pbCache)
946                                 *pbCache = pOptionEntry->bCache;
947                         return S_OK;
948                 }
949         }
950         return S_FALSE;
951 }
952
953 /* help function for setting search path and caching option for certain class */
954 static HRESULT DMUSIC_SetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
955 {
956         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
957         struct list *pEntry;
958         HRESULT result = S_FALSE; /* in case pClassID != GUID_DirectMusicAllTypes and not a valid CLSID */
959         TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
960         
961         LIST_FOR_EACH(pEntry, This->pClassSettings) {
962                 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
963                 /* well, either we have GUID_DirectMusicAllTypes and need to set it to all,
964                    or specific CLSID is given and we set it only to it */
965                 if (IsEqualGUID (pClassID, &GUID_DirectMusicAllTypes) ||
966                         IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
967                         if (wszSearchPath)
968                                 strcpyW(pOptionEntry->wszSearchPath, wszSearchPath);
969                         if (pbCache)
970                                 pOptionEntry->bCache = *pbCache;
971                         result = S_OK;
972                 }
973         }
974
975         return result;
976 }
977
978 static HRESULT DMUSIC_InitLoaderSettings(IDirectMusicLoader8 *iface)
979 {
980         IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
981
982         /* hard-coded list of classes */
983         static REFCLSID classes[] = {
984                 &CLSID_DirectMusicAudioPathConfig,
985                 &CLSID_DirectMusicBand,
986                 &CLSID_DirectMusicContainer,
987                 &CLSID_DirectMusicCollection,
988                 &CLSID_DirectMusicChordMap,
989                 &CLSID_DirectMusicSegment,
990                 &CLSID_DirectMusicScript,
991                 &CLSID_DirectMusicSong,
992                 &CLSID_DirectMusicStyle,
993                 &CLSID_DirectMusicGraph,
994                 &CLSID_DirectSoundWave
995         };
996         
997         unsigned int i;
998         WCHAR wszCurrent[MAX_PATH];
999
1000         TRACE(": (%p)\n", This);
1001         GetCurrentDirectoryW (MAX_PATH, wszCurrent);
1002
1003         for (i = 0; i < sizeof(classes)/sizeof(REFCLSID); i++) {
1004                 LPWINE_LOADER_OPTION pNewSetting = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_OPTION));
1005                 pNewSetting->guidClass = *classes[i];
1006                 strcpyW (pNewSetting->wszSearchPath, wszCurrent);
1007                 pNewSetting->bCache = TRUE;
1008                 list_add_tail (This->pClassSettings, &pNewSetting->entry);
1009         }
1010
1011         return S_OK;
1012 }