Release 980601
[wine/multimedia.git] / ole / compobj.c
blob35eb290e441f47b0d8a7c57920089380374ac729
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 /***********************************************************************
32 * CoBuildVersion [COMPOBJ.1]
34 * RETURNS
35 * Current built version, hiword is majornumber, loword is minornumber
37 DWORD WINAPI CoBuildVersion()
39 TRACE(ole,"(void)\n");
40 return (rmm<<16)+rup;
43 /***********************************************************************
44 * CoInitialize [COMPOBJ.2]
45 * Set the win16 IMalloc used for memory management
47 HRESULT WINAPI CoInitialize16(
48 LPMALLOC16 lpReserved /* [in] pointer to win16 malloc interface */
49 ) {
50 currentMalloc16 = lpReserved;
51 return S_OK;
54 /***********************************************************************
55 * CoInitialize (OLE32.26)
56 * Set the win32 IMalloc used for memorymanagement
58 HRESULT WINAPI CoInitialize32(
59 LPMALLOC32 lpReserved /* [in] pointer to win32 malloc interface */
60 ) {
61 currentMalloc32 = lpReserved;
62 return S_OK;
65 /***********************************************************************
66 * CoUnitialize [COMPOBJ.3]
67 * Don't know what it does.
69 void WINAPI CoUnitialize()
71 TRACE(ole,"(void)\n");
74 /***********************************************************************
75 * CoGetMalloc16 [COMPOBJ.4]
76 * RETURNS
77 * The current win16 IMalloc
79 HRESULT WINAPI CoGetMalloc16(
80 DWORD dwMemContext, /* [in] unknown */
81 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
82 ) {
83 if(!currentMalloc16)
84 currentMalloc16 = IMalloc16_Constructor();
85 *lpMalloc = currentMalloc16;
86 return S_OK;
89 /***********************************************************************
90 * CoGetMalloc32 [OLE32.4]
91 * RETURNS
92 * The current win32 IMalloc
94 HRESULT WINAPI CoGetMalloc32(
95 DWORD dwMemContext, /* [in] unknown */
96 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
97 ) {
98 if(!currentMalloc32)
99 currentMalloc32 = IMalloc32_Constructor();
100 *lpMalloc = currentMalloc32;
101 return S_OK;
104 /***********************************************************************
105 * CoDisconnectObject
107 OLESTATUS WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
109 TRACE(ole,"%p %lx\n",lpUnk,reserved);
110 return S_OK;
113 /***********************************************************************
114 * IsEqualGUID [COMPOBJ.18]
115 * Compares two Unique Identifiers
116 * RETURNS
117 * TRUE if equal
119 BOOL16 WINAPI IsEqualGUID(
120 GUID* g1, /* [in] unique id 1 */
121 GUID* g2 /* [in] unique id 2 */
123 return !memcmp( g1, g2, sizeof(GUID) );
126 /***********************************************************************
127 * CLSIDFromString [COMPOBJ.20]
128 * Converts a unique identifier from it's string representation into
129 * the GUID struct.
130 * RETURNS
131 * the converted GUID
134 /* Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6] */
136 OLESTATUS WINAPI CLSIDFromString16(
137 LPCOLESTR16 idstr, /* [in] string representation of guid */
138 CLSID *id /* [out] GUID converted from string */
140 BYTE *s = (BYTE *) idstr;
141 BYTE *p;
142 int i;
143 BYTE table[256];
145 TRACE(ole,"%s -> %p\n", idstr, id);
147 /* quick lookup table */
148 memset(table, 0, 256);
150 for (i = 0; i < 10; i++) {
151 table['0' + i] = i;
153 for (i = 0; i < 6; i++) {
154 table['A' + i] = i+10;
155 table['a' + i] = i+10;
158 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
160 if (strlen(idstr) != 38)
161 return OLE_ERROR_OBJECT;
163 p = (BYTE *) id;
165 s++; /* skip leading brace */
166 for (i = 0; i < 4; i++) {
167 p[3 - i] = table[*s]<<4 | table[*(s+1)];
168 s += 2;
170 p += 4;
171 s++; /* skip - */
173 for (i = 0; i < 2; i++) {
174 p[1-i] = table[*s]<<4 | table[*(s+1)];
175 s += 2;
177 p += 2;
178 s++; /* skip - */
180 for (i = 0; i < 2; i++) {
181 p[1-i] = table[*s]<<4 | table[*(s+1)];
182 s += 2;
184 p += 2;
185 s++; /* skip - */
187 /* these are just sequential bytes */
188 for (i = 0; i < 2; i++) {
189 *p++ = table[*s]<<4 | table[*(s+1)];
190 s += 2;
192 s++; /* skip - */
194 for (i = 0; i < 6; i++) {
195 *p++ = table[*s]<<4 | table[*(s+1)];
196 s += 2;
199 return S_OK;
202 /***********************************************************************
203 * CLSIDFromString (OLE32.3)
204 * Converts a unique identifier from it's string representation into
205 * the GUID struct.
206 * RETURNS
207 * the converted GUID
209 OLESTATUS WINAPI CLSIDFromString32(
210 LPCOLESTR32 idstr, /* [in] string representation of GUID */
211 CLSID *id /* [out] GUID represented by above string */
213 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
214 OLESTATUS ret = CLSIDFromString16(xid,id);
216 HeapFree(GetProcessHeap(),0,xid);
217 return ret;
220 /***********************************************************************
221 * WINE_StringFromCLSID [internal]
222 * Converts a GUID into the respective string representation.
223 * RETURNS
224 * the string representation and OLESTATUS
226 OLESTATUS WINAPI WINE_StringFromCLSID(
227 const CLSID *id, /* [in] GUID to be converted */
228 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
230 static const char *hex = "0123456789ABCDEF";
231 char *s;
232 int i;
234 sprintf(idstr, "{%08lx-%04x-%04x-%02x%02x-",
235 id->Data1, id->Data2, id->Data3,
236 id->Data4[0], id->Data4[1]);
237 s = &idstr[25];
239 /* 6 hex bytes */
240 for (i = 2; i < 8; i++) {
241 *s++ = hex[id->Data4[i]>>4];
242 *s++ = hex[id->Data4[i] & 0xf];
245 *s++ = '}';
246 *s++ = '\0';
248 for (i = strlen(idstr)-1; i >= 0; i--) {
249 idstr[i] = toupper(idstr[i]);
252 TRACE(ole,"%p->%s\n", id, idstr);
254 return OLE_OK;
257 /***********************************************************************
258 * StringFromCLSID [COMPOBJ.19]
259 * Converts a GUID into the respective string representation.
260 * The target string is allocated using the OLE IMalloc.
261 * RETURNS
262 * the string representation and OLESTATUS
264 OLESTATUS WINAPI StringFromCLSID16(
265 const CLSID *id, /* [in] the GUID to be converted */
266 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
269 LPMALLOC16 mllc;
270 OLESTATUS ret;
271 DWORD args[2];
273 ret = CoGetMalloc16(0,&mllc);
274 if (ret) return ret;
276 args[0] = (DWORD)mllc;
277 args[1] = 40;
279 /* No need for a Callback entry, we have WOWCallback16Ex which does
280 * everything we need.
282 if (!WOWCallback16Ex(
283 (FARPROC16)((LPMALLOC16_VTABLE)PTR_SEG_TO_LIN(
284 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
285 )->fnAlloc,
286 WCB16_CDECL,
288 (LPVOID)args,
289 (LPDWORD)idstr
290 )) {
291 WARN(ole,"CallTo16 IMalloc16 failed\n");
292 return E_FAIL;
294 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
297 /***********************************************************************
298 * StringFromCLSID [OLE32.151]
299 * Converts a GUID into the respective string representation.
300 * The target string is allocated using the OLE IMalloc.
301 * RETURNS
302 * the string representation and OLESTATUS
304 OLESTATUS WINAPI StringFromCLSID32(
305 const CLSID *id, /* [in] the GUID to be converted */
306 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
308 char buf[80];
309 OLESTATUS ret;
310 LPMALLOC32 mllc;
312 if ((ret=CoGetMalloc32(0,&mllc)))
313 return ret;
315 ret=WINE_StringFromCLSID(id,buf);
316 if (!ret) {
317 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
318 lstrcpyAtoW(*idstr,buf);
320 return ret;
323 /***********************************************************************
324 * StringFromGUID2 (OLE32.152)
326 * Converts a global unique identifier into a string of an API-
327 * specified fixed format. (The usual {.....} stuff.)
329 * RETURNS
330 * The (UNICODE) string representation of the GUID in 'str'
331 * The length of the resulting string, 0 if there was any problem.
333 INT32 WINAPI
334 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
336 char xguid[80];
338 if (WINE_StringFromCLSID(id,xguid))
339 return 0;
340 if (strlen(xguid)>=cmax)
341 return 0;
342 lstrcpyAtoW(str,xguid);
343 return strlen(xguid);
346 /***********************************************************************
347 * CLSIDFromProgID [COMPOBJ.61]
348 * Converts a program id into the respective GUID. (By using a registry lookup)
349 * RETURNS
350 * riid associated with the progid
352 OLESTATUS WINAPI CLSIDFromProgID16(
353 LPCOLESTR16 progid, /* [in] program id as found in registry */
354 LPCLSID riid /* [out] associated CLSID */
356 char *buf,buf2[80];
357 DWORD buf2len;
358 HRESULT err;
359 HKEY xhkey;
361 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
362 sprintf(buf,"%s\\CLSID",progid);
363 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
364 HeapFree(GetProcessHeap(),0,buf);
365 return OLE_ERROR_GENERIC;
367 HeapFree(GetProcessHeap(),0,buf);
368 buf2len = sizeof(buf2);
369 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
370 RegCloseKey(xhkey);
371 return OLE_ERROR_GENERIC;
373 RegCloseKey(xhkey);
374 return CLSIDFromString16(buf2,riid);
377 /***********************************************************************
378 * CLSIDFromProgID (OLE32.2)
379 * Converts a program id into the respective GUID. (By using a registry lookup)
380 * RETURNS
381 * riid associated with the progid
383 OLESTATUS WINAPI CLSIDFromProgID32(
384 LPCOLESTR32 progid, /* [in] program id as found in registry */
385 LPCLSID riid /* [out] associated CLSID */
387 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
388 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
390 HeapFree(GetProcessHeap(),0,pid);
391 return ret;
394 /***********************************************************************
395 * LookupETask (COMPOBJ.94)
397 OLESTATUS WINAPI LookupETask(LPVOID p1,LPVOID p2) {
398 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
399 return 0;
402 /***********************************************************************
403 * LookupETask (COMPOBJ.201)
405 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
406 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
407 return 0;
410 /***********************************************************************
411 * CoRegisterClassObject [COMPOBJ.5]
412 * Don't know where it registers it ...
414 OLESTATUS WINAPI CoRegisterClassObject16(
415 REFCLSID rclsid,
416 LPUNKNOWN pUnk,
417 DWORD dwClsContext,
418 DWORD flags,
419 LPDWORD lpdwRegister
421 char buf[80];
423 WINE_StringFromCLSID(rclsid,buf);
425 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
426 buf,pUnk,dwClsContext,flags,lpdwRegister
428 return 0;
431 /***********************************************************************
432 * CoRegisterClassObject (OLE32.36)
433 * Don't know where it registers it ...
435 OLESTATUS WINAPI CoRegisterClassObject32(
436 REFCLSID rclsid,
437 LPUNKNOWN pUnk,
438 DWORD dwClsContext,
439 DWORD flags,
440 LPDWORD lpdwRegister
442 char buf[80];
444 WINE_StringFromCLSID(rclsid,buf);
446 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
447 buf,pUnk,dwClsContext,flags,lpdwRegister
449 return 0;
452 /***********************************************************************
453 * CoRegisterMessageFilter [COMPOBJ.27]
455 OLESTATUS WINAPI CoRegisterMessageFilter16(
456 LPMESSAGEFILTER lpMessageFilter,
457 LPMESSAGEFILTER *lplpMessageFilter
459 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
460 return 0;
463 /***********************************************************************
464 * CoCreateInstance [COMPOBJ.13, OLE32.7]
466 HRESULT WINAPI CoCreateInstance(
467 REFCLSID rclsid,
468 LPUNKNOWN pUnkOuter,
469 DWORD dwClsContext,
470 REFIID riid,
471 LPVOID *ppv
473 char buf[80],xbuf[80];
475 if (rclsid)
476 WINE_StringFromCLSID(rclsid,buf);
477 else
478 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
479 if (riid)
480 WINE_StringFromCLSID(riid,xbuf);
481 else
482 sprintf(xbuf,"<riid-0x%08lx>",(DWORD)riid);
484 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
485 *ppv = NULL;
486 return S_OK;
489 /***********************************************************************
490 * CoFreeUnusedLibraries [COMPOBJ.17]
492 void WINAPI CoFreeUnusedLibraries()
494 FIXME(ole,"(), stub !\n");
497 /***********************************************************************
498 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
499 * RETURNS
500 * the current system time in lpFileTime
502 HRESULT WINAPI CoFileTimeNow(
503 FILETIME *lpFileTime /* [out] the current time */
505 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
506 return S_OK;
509 /***********************************************************************
510 * CoTaskMemAlloc (OLE32.43)
511 * RETURNS
512 * pointer to newly allocated block
514 LPVOID WINAPI CoTaskMemAlloc(
515 ULONG size /* [in] size of memoryblock to be allocated */
517 LPMALLOC32 lpmalloc;
518 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
520 if (ret)
521 return NULL;
522 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
525 /***********************************************************************
526 * CoTaskMemFree (OLE32.44)
528 VOID WINAPI CoTaskMemFree(
529 LPVOID ptr /* [in] pointer to be freed */
531 LPMALLOC32 lpmalloc;
532 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
534 if (ret) return;
535 return lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
538 /***********************************************************************
539 * CoInitializeWOW (OLE32.27)
541 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
542 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
543 return 0;
546 /***********************************************************************
547 * CoLockObjectExternal (COMPOBJ.63)
549 HRESULT WINAPI CoLockObjectExternal16(
550 LPUNKNOWN pUnk, /* [in] object to be locked */
551 BOOL16 fLock, /* [in] do lock */
552 BOOL16 fLastUnlockReleases /* [in] ? */
554 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
555 return S_OK;