Started the implementation of IMAGEHLP.DLL.
[wine/multimedia.git] / ole / compobj.c
blob0357b69ae13f0e77aef5a685d8bbdeb3858751d2
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"
27 #include "dplay.h"
30 LPMALLOC16 currentMalloc16=NULL;
31 LPMALLOC32 currentMalloc32=NULL;
33 HTASK16 hETask = 0;
34 WORD Table_ETask[62];
36 /******************************************************************************
37 * CoBuildVersion [COMPOBJ.1]
39 * RETURNS
40 * Current built version, hiword is majornumber, loword is minornumber
42 DWORD WINAPI CoBuildVersion()
44 TRACE(ole,"(void)\n");
45 return (rmm<<16)+rup;
48 /******************************************************************************
49 * CoInitialize16 [COMPOBJ.2]
50 * Set the win16 IMalloc used for memory management
52 HRESULT WINAPI CoInitialize16(
53 LPMALLOC16 lpReserved /* [in] pointer to win16 malloc interface */
54 ) {
55 currentMalloc16 = lpReserved;
56 return S_OK;
59 /******************************************************************************
60 * CoInitialize32 [OLE32.26]
61 * Set the win32 IMalloc used for memorymanagement
63 HRESULT WINAPI CoInitialize32(
64 LPMALLOC32 lpReserved /* [in] pointer to win32 malloc interface */
65 ) {
66 currentMalloc32 = lpReserved;
67 return S_OK;
70 /***********************************************************************
71 * CoUnitialize [COMPOBJ.3]
72 * Don't know what it does.
74 void WINAPI CoUnitialize()
76 TRACE(ole,"(void)\n");
79 /***********************************************************************
80 * CoGetMalloc16 [COMPOBJ.4]
81 * RETURNS
82 * The current win16 IMalloc
84 HRESULT WINAPI CoGetMalloc16(
85 DWORD dwMemContext, /* [in] unknown */
86 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
87 ) {
88 if(!currentMalloc16)
89 currentMalloc16 = IMalloc16_Constructor();
90 *lpMalloc = currentMalloc16;
91 return S_OK;
94 /******************************************************************************
95 * CoGetMalloc32 [OLE32.20]
97 * RETURNS
98 * The current win32 IMalloc
100 HRESULT WINAPI CoGetMalloc32(
101 DWORD dwMemContext, /* [in] unknown */
102 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
104 if(!currentMalloc32)
105 currentMalloc32 = IMalloc32_Constructor();
106 *lpMalloc = currentMalloc32;
107 return S_OK;
110 /***********************************************************************
111 * CoCreateStandardMalloc16 [COMPOBJ.71]
113 OLESTATUS WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
114 LPMALLOC16 *lpMalloc)
116 /* FIXME: docu says we shouldn't return the same allocator as in
117 * CoGetMalloc16 */
118 *lpMalloc = IMalloc16_Constructor();
119 return S_OK;
122 /******************************************************************************
123 * CoDisconnectObject [COMPOBJ.15]
125 OLESTATUS WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
127 TRACE(ole,"%p %lx\n",lpUnk,reserved);
128 return S_OK;
131 /***********************************************************************
132 * IsEqualGUID [COMPOBJ.18]
133 * Compares two Unique Identifiers
134 * RETURNS
135 * TRUE if equal
137 BOOL16 WINAPI IsEqualGUID(
138 GUID* g1, /* [in] unique id 1 */
139 GUID* g2 /* [in] unique id 2 */
141 return !memcmp( g1, g2, sizeof(GUID) );
144 /******************************************************************************
145 * CLSIDFromString16 [COMPOBJ.20]
146 * Converts a unique identifier from it's string representation into
147 * the GUID struct.
149 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
151 * RETURNS
152 * the converted GUID
154 OLESTATUS WINAPI CLSIDFromString16(
155 LPCOLESTR16 idstr, /* [in] string representation of guid */
156 CLSID *id /* [out] GUID converted from string */
158 BYTE *s = (BYTE *) idstr;
159 BYTE *p;
160 int i;
161 BYTE table[256];
163 TRACE(ole,"%s -> %p\n", idstr, id);
165 /* quick lookup table */
166 memset(table, 0, 256);
168 for (i = 0; i < 10; i++) {
169 table['0' + i] = i;
171 for (i = 0; i < 6; i++) {
172 table['A' + i] = i+10;
173 table['a' + i] = i+10;
176 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
178 if (strlen(idstr) != 38)
179 return OLE_ERROR_OBJECT;
181 p = (BYTE *) id;
183 s++; /* skip leading brace */
184 for (i = 0; i < 4; i++) {
185 p[3 - i] = table[*s]<<4 | table[*(s+1)];
186 s += 2;
188 p += 4;
189 s++; /* skip - */
191 for (i = 0; i < 2; i++) {
192 p[1-i] = table[*s]<<4 | table[*(s+1)];
193 s += 2;
195 p += 2;
196 s++; /* skip - */
198 for (i = 0; i < 2; i++) {
199 p[1-i] = table[*s]<<4 | table[*(s+1)];
200 s += 2;
202 p += 2;
203 s++; /* skip - */
205 /* these are just sequential bytes */
206 for (i = 0; i < 2; i++) {
207 *p++ = table[*s]<<4 | table[*(s+1)];
208 s += 2;
210 s++; /* skip - */
212 for (i = 0; i < 6; i++) {
213 *p++ = table[*s]<<4 | table[*(s+1)];
214 s += 2;
217 return S_OK;
220 /******************************************************************************
221 * CLSIDFromString32 [OLE32.3]
222 * Converts a unique identifier from it's string representation into
223 * the GUID struct.
224 * RETURNS
225 * the converted GUID
227 OLESTATUS WINAPI CLSIDFromString32(
228 LPCOLESTR32 idstr, /* [in] string representation of GUID */
229 CLSID *id /* [out] GUID represented by above string */
231 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
232 OLESTATUS ret = CLSIDFromString16(xid,id);
234 HeapFree(GetProcessHeap(),0,xid);
235 return ret;
238 /******************************************************************************
239 * WINE_StringFromCLSID [???]
240 * Converts a GUID into the respective string representation.
241 * RETURNS
242 * the string representation and OLESTATUS
244 OLESTATUS WINAPI WINE_StringFromCLSID(
245 const CLSID *id, /* [in] GUID to be converted */
246 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
248 static const char *hex = "0123456789ABCDEF";
249 char *s;
250 int i;
252 if (!id)
253 { ERR(ole,"called with id=Null\n");
254 *idstr = 0x00;
255 return E_FAIL;
258 sprintf(idstr, "{%08lx-%04x-%04x-%02x%02x-",
259 id->Data1, id->Data2, id->Data3,
260 id->Data4[0], id->Data4[1]);
261 s = &idstr[25];
263 /* 6 hex bytes */
264 for (i = 2; i < 8; i++) {
265 *s++ = hex[id->Data4[i]>>4];
266 *s++ = hex[id->Data4[i] & 0xf];
269 *s++ = '}';
270 *s++ = '\0';
272 for (i = strlen(idstr)-1; i >= 0; i--) {
273 idstr[i] = toupper(idstr[i]);
276 TRACE(ole,"%p->%s\n", id, idstr);
278 return OLE_OK;
281 /******************************************************************************
282 * StringFromCLSID16 [COMPOBJ.19]
283 * Converts a GUID into the respective string representation.
284 * The target string is allocated using the OLE IMalloc.
285 * RETURNS
286 * the string representation and OLESTATUS
288 OLESTATUS WINAPI StringFromCLSID16(
289 const CLSID *id, /* [in] the GUID to be converted */
290 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
293 LPMALLOC16 mllc;
294 OLESTATUS ret;
295 DWORD args[2];
297 ret = CoGetMalloc16(0,&mllc);
298 if (ret) return ret;
300 args[0] = (DWORD)mllc;
301 args[1] = 40;
303 /* No need for a Callback entry, we have WOWCallback16Ex which does
304 * everything we need.
306 if (!WOWCallback16Ex(
307 (FARPROC16)((LPMALLOC16_VTABLE)PTR_SEG_TO_LIN(
308 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
309 )->fnAlloc,
310 WCB16_CDECL,
312 (LPVOID)args,
313 (LPDWORD)idstr
314 )) {
315 WARN(ole,"CallTo16 IMalloc16 failed\n");
316 return E_FAIL;
318 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
321 /******************************************************************************
322 * StringFromCLSID32 [OLE32.151]
323 * Converts a GUID into the respective string representation.
324 * The target string is allocated using the OLE IMalloc.
325 * RETURNS
326 * the string representation and OLESTATUS
328 OLESTATUS WINAPI StringFromCLSID32(
329 const CLSID *id, /* [in] the GUID to be converted */
330 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
332 char buf[80];
333 OLESTATUS ret;
334 LPMALLOC32 mllc;
336 if ((ret=CoGetMalloc32(0,&mllc)))
337 return ret;
339 ret=WINE_StringFromCLSID(id,buf);
340 if (!ret) {
341 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
342 lstrcpyAtoW(*idstr,buf);
344 return ret;
347 /******************************************************************************
348 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
350 * Converts a global unique identifier into a string of an API-
351 * specified fixed format. (The usual {.....} stuff.)
353 * RETURNS
354 * The (UNICODE) string representation of the GUID in 'str'
355 * The length of the resulting string, 0 if there was any problem.
357 INT32 WINAPI
358 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
360 char xguid[80];
362 if (WINE_StringFromCLSID(id,xguid))
363 return 0;
364 if (strlen(xguid)>=cmax)
365 return 0;
366 lstrcpyAtoW(str,xguid);
367 return strlen(xguid);
370 /******************************************************************************
371 * CLSIDFromProgID16 [COMPOBJ.61]
372 * Converts a program id into the respective GUID. (By using a registry lookup)
373 * RETURNS
374 * riid associated with the progid
376 OLESTATUS WINAPI CLSIDFromProgID16(
377 LPCOLESTR16 progid, /* [in] program id as found in registry */
378 LPCLSID riid /* [out] associated CLSID */
380 char *buf,buf2[80];
381 DWORD buf2len;
382 HRESULT err;
383 HKEY xhkey;
385 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
386 sprintf(buf,"%s\\CLSID",progid);
387 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
388 HeapFree(GetProcessHeap(),0,buf);
389 return OLE_ERROR_GENERIC;
391 HeapFree(GetProcessHeap(),0,buf);
392 buf2len = sizeof(buf2);
393 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
394 RegCloseKey(xhkey);
395 return OLE_ERROR_GENERIC;
397 RegCloseKey(xhkey);
398 return CLSIDFromString16(buf2,riid);
401 /******************************************************************************
402 * CLSIDFromProgID32 [OLE32.2]
403 * Converts a program id into the respective GUID. (By using a registry lookup)
404 * RETURNS
405 * riid associated with the progid
407 OLESTATUS WINAPI CLSIDFromProgID32(
408 LPCOLESTR32 progid, /* [in] program id as found in registry */
409 LPCLSID riid /* [out] associated CLSID */
411 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
412 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
414 HeapFree(GetProcessHeap(),0,pid);
415 return ret;
418 /***********************************************************************
419 * LookupETask (COMPOBJ.94)
421 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
422 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
423 if ((*hTask = GetCurrentTask()) == hETask) {
424 memcpy(p, Table_ETask, sizeof(Table_ETask));
426 return 0;
429 /***********************************************************************
430 * SetETask (COMPOBJ.95)
432 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
433 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
434 hETask = hTask;
435 return 0;
438 /***********************************************************************
439 * CallObjectInWOW (COMPOBJ.201)
441 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
442 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
443 return 0;
446 /******************************************************************************
447 * CoRegisterClassObject16 [COMPOBJ.5]
449 * Don't know where it registers it ...
451 OLESTATUS WINAPI CoRegisterClassObject16(
452 REFCLSID rclsid,
453 LPUNKNOWN pUnk,
454 DWORD dwClsContext,
455 DWORD flags,
456 LPDWORD lpdwRegister
458 char buf[80];
460 WINE_StringFromCLSID(rclsid,buf);
462 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
463 buf,pUnk,dwClsContext,flags,lpdwRegister
465 return 0;
468 /******************************************************************************
469 * CoRegisterClassObject32 [OLE32.36]
471 * Don't know where it registers it ...
473 OLESTATUS WINAPI CoRegisterClassObject32(
474 REFCLSID rclsid,
475 LPUNKNOWN pUnk,
476 DWORD dwClsContext,
477 DWORD flags,
478 LPDWORD lpdwRegister
480 char buf[80];
482 WINE_StringFromCLSID(rclsid,buf);
484 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
485 buf,pUnk,dwClsContext,flags,lpdwRegister
487 return 0;
490 /***********************************************************************
491 * CoGetClassObject [COMPOBJ.7]
493 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
494 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
496 char xclsid[50],xiid[50];
497 LPCLASSFACTORY lpclf;
498 HRESULT hres = E_UNEXPECTED;
500 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
501 WINE_StringFromCLSID((LPCLSID)iid,xiid);
502 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
504 *ppv = NULL;
505 lpclf = IClassFactory_Constructor();
506 if (lpclf)
508 hres = lpclf->lpvtbl->fnQueryInterface(lpclf,iid, ppv);
509 lpclf->lpvtbl->fnRelease(lpclf);
511 return hres;
514 /******************************************************************************
515 * CoRegisterMessageFilter16 [COMPOBJ.27]
517 OLESTATUS WINAPI CoRegisterMessageFilter16(
518 LPMESSAGEFILTER lpMessageFilter,
519 LPMESSAGEFILTER *lplpMessageFilter
521 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
522 return 0;
525 /***********************************************************************
526 * CoCreateInstance [COMPOBJ.13, OLE32.7]
528 HRESULT WINAPI CoCreateInstance(
529 REFCLSID rclsid,
530 LPUNKNOWN pUnkOuter,
531 DWORD dwClsContext,
532 REFIID iid,
533 LPVOID *ppv
535 #if 0
536 char buf[80],xbuf[80];
538 if (rclsid)
539 WINE_StringFromCLSID(rclsid,buf);
540 else
541 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
542 if (iid)
543 WINE_StringFromCLSID(iid,xbuf);
544 else
545 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
547 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
548 *ppv = NULL;
549 #else
550 HRESULT hres;
551 LPCLASSFACTORY lpclf = 0;
553 CoGetClassObject(rclsid, dwClsContext, NULL, &IID_IClassFactory, (LPVOID)&lpclf);
554 hres = lpclf->lpvtbl->fnCreateInstance(lpclf, pUnkOuter, iid, ppv);
555 lpclf->lpvtbl->fnRelease(lpclf);
556 return hres;
557 #endif
560 /***********************************************************************
561 * CoFreeUnusedLibraries [COMPOBJ.17]
563 void WINAPI CoFreeUnusedLibraries()
565 FIXME(ole,"(), stub !\n");
568 /***********************************************************************
569 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
570 * RETURNS
571 * the current system time in lpFileTime
573 HRESULT WINAPI CoFileTimeNow(
574 FILETIME *lpFileTime /* [out] the current time */
576 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
577 return S_OK;
580 /***********************************************************************
581 * CoTaskMemAlloc (OLE32.43)
582 * RETURNS
583 * pointer to newly allocated block
585 LPVOID WINAPI CoTaskMemAlloc(
586 ULONG size /* [in] size of memoryblock to be allocated */
588 LPMALLOC32 lpmalloc;
589 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
591 if (ret)
592 return NULL;
593 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
596 /***********************************************************************
597 * CoTaskMemFree (OLE32.44)
599 VOID WINAPI CoTaskMemFree(
600 LPVOID ptr /* [in] pointer to be freed */
602 LPMALLOC32 lpmalloc;
603 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
605 if (ret) return;
606 return lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
609 /***********************************************************************
610 * CoInitializeWOW (OLE32.27)
612 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
613 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
614 return 0;
617 /******************************************************************************
618 * CoLockObjectExternal16 [COMPOBJ.63]
620 HRESULT WINAPI CoLockObjectExternal16(
621 LPUNKNOWN pUnk, /* [in] object to be locked */
622 BOOL16 fLock, /* [in] do lock */
623 BOOL16 fLastUnlockReleases /* [in] ? */
625 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
626 return S_OK;
629 /******************************************************************************
630 * CoLockObjectExternal32 [OLE32.31]
632 HRESULT WINAPI CoLockObjectExternal32(
633 LPUNKNOWN pUnk, /* [in] object to be locked */
634 BOOL32 fLock, /* [in] do lock */
635 BOOL32 fLastUnlockReleases /* [in] ? */
637 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
638 return S_OK;
641 /***********************************************************************
642 * CoGetState16 [COMPOBJ.115]
644 HRESULT WINAPI CoGetState16(LPDWORD state)
646 FIXME(ole, "(%p),stub!\n", state);
647 *state = 0;
648 return S_OK;