Release 980913
[wine/multimedia.git] / ole / compobj.c
blob30d0869e355820b5b8bf83a40f742da10b37bfef
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 */
7 #define INITGUID
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include "ole.h"
14 #include "ole2.h"
15 #include "winerror.h"
16 #include "debug.h"
17 #include "file.h"
18 #include "compobj.h"
19 #include "heap.h"
20 #include "ldt.h"
21 #include "interfaces.h"
22 #include "shlobj.h"
23 #include "ddraw.h"
24 #include "dsound.h"
25 #include "dinput.h"
26 #include "d3d.h"
28 LPMALLOC16 currentMalloc16=NULL;
29 LPMALLOC32 currentMalloc32=NULL;
31 HTASK16 hETask = 0;
32 WORD Table_ETask[62];
34 /***********************************************************************
35 * CoBuildVersion [COMPOBJ.1]
37 * RETURNS
38 * Current built version, hiword is majornumber, loword is minornumber
40 DWORD WINAPI CoBuildVersion()
42 TRACE(ole,"(void)\n");
43 return (rmm<<16)+rup;
46 /***********************************************************************
47 * CoInitialize [COMPOBJ.2]
48 * Set the win16 IMalloc used for memory management
50 HRESULT WINAPI CoInitialize16(
51 LPMALLOC16 lpReserved /* [in] pointer to win16 malloc interface */
52 ) {
53 currentMalloc16 = lpReserved;
54 return S_OK;
57 /***********************************************************************
58 * CoInitialize (OLE32.26)
59 * Set the win32 IMalloc used for memorymanagement
61 HRESULT WINAPI CoInitialize32(
62 LPMALLOC32 lpReserved /* [in] pointer to win32 malloc interface */
63 ) {
64 currentMalloc32 = lpReserved;
65 return S_OK;
68 /***********************************************************************
69 * CoUnitialize [COMPOBJ.3]
70 * Don't know what it does.
72 void WINAPI CoUnitialize()
74 TRACE(ole,"(void)\n");
77 /***********************************************************************
78 * CoGetMalloc16 [COMPOBJ.4]
79 * RETURNS
80 * The current win16 IMalloc
82 HRESULT WINAPI CoGetMalloc16(
83 DWORD dwMemContext, /* [in] unknown */
84 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
85 ) {
86 if(!currentMalloc16)
87 currentMalloc16 = IMalloc16_Constructor();
88 *lpMalloc = currentMalloc16;
89 return S_OK;
92 /***********************************************************************
93 * CoGetMalloc32 [OLE32.4]
94 * RETURNS
95 * The current win32 IMalloc
97 HRESULT WINAPI CoGetMalloc32(
98 DWORD dwMemContext, /* [in] unknown */
99 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
101 if(!currentMalloc32)
102 currentMalloc32 = IMalloc32_Constructor();
103 *lpMalloc = currentMalloc32;
104 return S_OK;
107 /***********************************************************************
108 * CoCreateStandardMalloc16 [COMPOBJ.71]
110 OLESTATUS WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
111 LPMALLOC16 *lpMalloc)
113 /* FIXME: docu says we shouldn't return the same allocator as in
114 * CoGetMalloc16 */
115 *lpMalloc = IMalloc16_Constructor();
116 return S_OK;
119 /***********************************************************************
120 * CoDisconnectObject
122 OLESTATUS WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
124 TRACE(ole,"%p %lx\n",lpUnk,reserved);
125 return S_OK;
128 /***********************************************************************
129 * IsEqualGUID [COMPOBJ.18]
130 * Compares two Unique Identifiers
131 * RETURNS
132 * TRUE if equal
134 BOOL16 WINAPI IsEqualGUID(
135 GUID* g1, /* [in] unique id 1 */
136 GUID* g2 /* [in] unique id 2 */
138 return !memcmp( g1, g2, sizeof(GUID) );
141 /***********************************************************************
142 * CLSIDFromString [COMPOBJ.20]
143 * Converts a unique identifier from it's string representation into
144 * the GUID struct.
145 * RETURNS
146 * the converted GUID
149 /* Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6] */
151 OLESTATUS WINAPI CLSIDFromString16(
152 LPCOLESTR16 idstr, /* [in] string representation of guid */
153 CLSID *id /* [out] GUID converted from string */
155 BYTE *s = (BYTE *) idstr;
156 BYTE *p;
157 int i;
158 BYTE table[256];
160 TRACE(ole,"%s -> %p\n", idstr, id);
162 /* quick lookup table */
163 memset(table, 0, 256);
165 for (i = 0; i < 10; i++) {
166 table['0' + i] = i;
168 for (i = 0; i < 6; i++) {
169 table['A' + i] = i+10;
170 table['a' + i] = i+10;
173 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
175 if (strlen(idstr) != 38)
176 return OLE_ERROR_OBJECT;
178 p = (BYTE *) id;
180 s++; /* skip leading brace */
181 for (i = 0; i < 4; i++) {
182 p[3 - i] = table[*s]<<4 | table[*(s+1)];
183 s += 2;
185 p += 4;
186 s++; /* skip - */
188 for (i = 0; i < 2; i++) {
189 p[1-i] = table[*s]<<4 | table[*(s+1)];
190 s += 2;
192 p += 2;
193 s++; /* skip - */
195 for (i = 0; i < 2; i++) {
196 p[1-i] = table[*s]<<4 | table[*(s+1)];
197 s += 2;
199 p += 2;
200 s++; /* skip - */
202 /* these are just sequential bytes */
203 for (i = 0; i < 2; i++) {
204 *p++ = table[*s]<<4 | table[*(s+1)];
205 s += 2;
207 s++; /* skip - */
209 for (i = 0; i < 6; i++) {
210 *p++ = table[*s]<<4 | table[*(s+1)];
211 s += 2;
214 return S_OK;
217 /***********************************************************************
218 * CLSIDFromString (OLE32.3)
219 * Converts a unique identifier from it's string representation into
220 * the GUID struct.
221 * RETURNS
222 * the converted GUID
224 OLESTATUS WINAPI CLSIDFromString32(
225 LPCOLESTR32 idstr, /* [in] string representation of GUID */
226 CLSID *id /* [out] GUID represented by above string */
228 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
229 OLESTATUS ret = CLSIDFromString16(xid,id);
231 HeapFree(GetProcessHeap(),0,xid);
232 return ret;
235 /***********************************************************************
236 * WINE_StringFromCLSID [internal]
237 * Converts a GUID into the respective string representation.
238 * RETURNS
239 * the string representation and OLESTATUS
241 OLESTATUS WINAPI WINE_StringFromCLSID(
242 const CLSID *id, /* [in] GUID to be converted */
243 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
245 static const char *hex = "0123456789ABCDEF";
246 char *s;
247 int i;
249 if (!id)
250 { ERR(ole,"called with id=Null\n");
251 *idstr = 0x00;
252 return E_FAIL;
255 sprintf(idstr, "{%08lx-%04x-%04x-%02x%02x-",
256 id->Data1, id->Data2, id->Data3,
257 id->Data4[0], id->Data4[1]);
258 s = &idstr[25];
260 /* 6 hex bytes */
261 for (i = 2; i < 8; i++) {
262 *s++ = hex[id->Data4[i]>>4];
263 *s++ = hex[id->Data4[i] & 0xf];
266 *s++ = '}';
267 *s++ = '\0';
269 for (i = strlen(idstr)-1; i >= 0; i--) {
270 idstr[i] = toupper(idstr[i]);
273 TRACE(ole,"%p->%s\n", id, idstr);
275 return OLE_OK;
278 /***********************************************************************
279 * StringFromCLSID [COMPOBJ.19]
280 * Converts a GUID into the respective string representation.
281 * The target string is allocated using the OLE IMalloc.
282 * RETURNS
283 * the string representation and OLESTATUS
285 OLESTATUS WINAPI StringFromCLSID16(
286 const CLSID *id, /* [in] the GUID to be converted */
287 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
290 LPMALLOC16 mllc;
291 OLESTATUS ret;
292 DWORD args[2];
294 ret = CoGetMalloc16(0,&mllc);
295 if (ret) return ret;
297 args[0] = (DWORD)mllc;
298 args[1] = 40;
300 /* No need for a Callback entry, we have WOWCallback16Ex which does
301 * everything we need.
303 if (!WOWCallback16Ex(
304 (FARPROC16)((LPMALLOC16_VTABLE)PTR_SEG_TO_LIN(
305 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
306 )->fnAlloc,
307 WCB16_CDECL,
309 (LPVOID)args,
310 (LPDWORD)idstr
311 )) {
312 WARN(ole,"CallTo16 IMalloc16 failed\n");
313 return E_FAIL;
315 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
318 /***********************************************************************
319 * StringFromCLSID [OLE32.151]
320 * Converts a GUID into the respective string representation.
321 * The target string is allocated using the OLE IMalloc.
322 * RETURNS
323 * the string representation and OLESTATUS
325 OLESTATUS WINAPI StringFromCLSID32(
326 const CLSID *id, /* [in] the GUID to be converted */
327 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
329 char buf[80];
330 OLESTATUS ret;
331 LPMALLOC32 mllc;
333 if ((ret=CoGetMalloc32(0,&mllc)))
334 return ret;
336 ret=WINE_StringFromCLSID(id,buf);
337 if (!ret) {
338 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
339 lstrcpyAtoW(*idstr,buf);
341 return ret;
344 /***********************************************************************
345 * StringFromGUID2 (OLE32.152)
347 * Converts a global unique identifier into a string of an API-
348 * specified fixed format. (The usual {.....} stuff.)
350 * RETURNS
351 * The (UNICODE) string representation of the GUID in 'str'
352 * The length of the resulting string, 0 if there was any problem.
354 INT32 WINAPI
355 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
357 char xguid[80];
359 if (WINE_StringFromCLSID(id,xguid))
360 return 0;
361 if (strlen(xguid)>=cmax)
362 return 0;
363 lstrcpyAtoW(str,xguid);
364 return strlen(xguid);
367 /***********************************************************************
368 * CLSIDFromProgID [COMPOBJ.61]
369 * Converts a program id into the respective GUID. (By using a registry lookup)
370 * RETURNS
371 * riid associated with the progid
373 OLESTATUS WINAPI CLSIDFromProgID16(
374 LPCOLESTR16 progid, /* [in] program id as found in registry */
375 LPCLSID riid /* [out] associated CLSID */
377 char *buf,buf2[80];
378 DWORD buf2len;
379 HRESULT err;
380 HKEY xhkey;
382 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
383 sprintf(buf,"%s\\CLSID",progid);
384 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
385 HeapFree(GetProcessHeap(),0,buf);
386 return OLE_ERROR_GENERIC;
388 HeapFree(GetProcessHeap(),0,buf);
389 buf2len = sizeof(buf2);
390 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
391 RegCloseKey(xhkey);
392 return OLE_ERROR_GENERIC;
394 RegCloseKey(xhkey);
395 return CLSIDFromString16(buf2,riid);
398 /***********************************************************************
399 * CLSIDFromProgID (OLE32.2)
400 * Converts a program id into the respective GUID. (By using a registry lookup)
401 * RETURNS
402 * riid associated with the progid
404 OLESTATUS WINAPI CLSIDFromProgID32(
405 LPCOLESTR32 progid, /* [in] program id as found in registry */
406 LPCLSID riid /* [out] associated CLSID */
408 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
409 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
411 HeapFree(GetProcessHeap(),0,pid);
412 return ret;
415 /***********************************************************************
416 * LookupETask (COMPOBJ.94)
418 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
419 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
420 if ((*hTask = GetCurrentTask()) == hETask) {
421 memcpy(p, Table_ETask, sizeof(Table_ETask));
423 return 0;
426 /***********************************************************************
427 * SetETask (COMPOBJ.95)
429 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
430 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
431 hETask = hTask;
432 return 0;
435 /***********************************************************************
436 * CallObjectInWOW (COMPOBJ.201)
438 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
439 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
440 return 0;
443 /***********************************************************************
444 * CoRegisterClassObject [COMPOBJ.5]
445 * Don't know where it registers it ...
447 OLESTATUS WINAPI CoRegisterClassObject16(
448 REFCLSID rclsid,
449 LPUNKNOWN pUnk,
450 DWORD dwClsContext,
451 DWORD flags,
452 LPDWORD lpdwRegister
454 char buf[80];
456 WINE_StringFromCLSID(rclsid,buf);
458 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
459 buf,pUnk,dwClsContext,flags,lpdwRegister
461 return 0;
464 /***********************************************************************
465 * CoRegisterClassObject (OLE32.36)
466 * Don't know where it registers it ...
468 OLESTATUS WINAPI CoRegisterClassObject32(
469 REFCLSID rclsid,
470 LPUNKNOWN pUnk,
471 DWORD dwClsContext,
472 DWORD flags,
473 LPDWORD lpdwRegister
475 char buf[80];
477 WINE_StringFromCLSID(rclsid,buf);
479 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
480 buf,pUnk,dwClsContext,flags,lpdwRegister
482 return 0;
485 /***********************************************************************
486 * CoGetClassObject [COMPOBJ.7]
488 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
489 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
491 char xclsid[50],xiid[50];
492 LPCLASSFACTORY lpclf;
493 HRESULT hres = E_UNEXPECTED;
495 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
496 WINE_StringFromCLSID((LPCLSID)iid,xiid);
497 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
499 *ppv = NULL;
500 lpclf = IClassFactory_Constructor();
501 if (lpclf)
503 hres = lpclf->lpvtbl->fnQueryInterface(lpclf,iid, ppv);
504 lpclf->lpvtbl->fnRelease(lpclf);
506 return hres;
509 /***********************************************************************
510 * CoRegisterMessageFilter [COMPOBJ.27]
512 OLESTATUS WINAPI CoRegisterMessageFilter16(
513 LPMESSAGEFILTER lpMessageFilter,
514 LPMESSAGEFILTER *lplpMessageFilter
516 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
517 return 0;
520 /***********************************************************************
521 * CoCreateInstance [COMPOBJ.13, OLE32.7]
523 HRESULT WINAPI CoCreateInstance(
524 REFCLSID rclsid,
525 LPUNKNOWN pUnkOuter,
526 DWORD dwClsContext,
527 REFIID iid,
528 LPVOID *ppv
530 #if 0
531 char buf[80],xbuf[80];
533 if (rclsid)
534 WINE_StringFromCLSID(rclsid,buf);
535 else
536 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
537 if (iid)
538 WINE_StringFromCLSID(iid,xbuf);
539 else
540 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
542 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
543 *ppv = NULL;
544 #else
545 HRESULT hres;
546 LPCLASSFACTORY lpclf = 0;
548 CoGetClassObject(rclsid, dwClsContext, NULL, &IID_IClassFactory, (LPVOID)&lpclf);
549 hres = lpclf->lpvtbl->fnCreateInstance(lpclf, pUnkOuter, iid, ppv);
550 lpclf->lpvtbl->fnRelease(lpclf);
551 return hres;
552 #endif
555 /***********************************************************************
556 * CoFreeUnusedLibraries [COMPOBJ.17]
558 void WINAPI CoFreeUnusedLibraries()
560 FIXME(ole,"(), stub !\n");
563 /***********************************************************************
564 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
565 * RETURNS
566 * the current system time in lpFileTime
568 HRESULT WINAPI CoFileTimeNow(
569 FILETIME *lpFileTime /* [out] the current time */
571 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
572 return S_OK;
575 /***********************************************************************
576 * CoTaskMemAlloc (OLE32.43)
577 * RETURNS
578 * pointer to newly allocated block
580 LPVOID WINAPI CoTaskMemAlloc(
581 ULONG size /* [in] size of memoryblock to be allocated */
583 LPMALLOC32 lpmalloc;
584 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
586 if (ret)
587 return NULL;
588 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
591 /***********************************************************************
592 * CoTaskMemFree (OLE32.44)
594 VOID WINAPI CoTaskMemFree(
595 LPVOID ptr /* [in] pointer to be freed */
597 LPMALLOC32 lpmalloc;
598 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
600 if (ret) return;
601 return lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
604 /***********************************************************************
605 * CoInitializeWOW (OLE32.27)
607 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
608 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
609 return 0;
612 /***********************************************************************
613 * CoLockObjectExternal (COMPOBJ.63)
615 HRESULT WINAPI CoLockObjectExternal16(
616 LPUNKNOWN pUnk, /* [in] object to be locked */
617 BOOL16 fLock, /* [in] do lock */
618 BOOL16 fLastUnlockReleases /* [in] ? */
620 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
621 return S_OK;
623 /***********************************************************************
624 * CoLockObjectExternal (OLE32.31)
626 HRESULT WINAPI CoLockObjectExternal32(
627 LPUNKNOWN pUnk, /* [in] object to be locked */
628 BOOL32 fLock, /* [in] do lock */
629 BOOL32 fLastUnlockReleases /* [in] ? */
631 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
632 return S_OK;
635 /***********************************************************************
636 * CoGetState16 [COMPOBJ.115]
638 HRESULT WINAPI CoGetState16(LPDWORD state)
640 FIXME(ole, "(%p),stub!\n", state);
641 *state = 0;
642 return S_OK;