Set default video mode to 3 (80x25 color).
[wine/dcerpc.git] / ole / compobj.c
blob0661be6d78c561d6c3a0bc19e676baff6542cd49
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 * CoCreateGuid[OLE32.6]
222 * Just a random 128-bit number?
224 HRESULT WINAPI CoCreateGuid(GUID *pguid) {
225 FIXME(ole,"stub!\n");
226 return S_OK;
229 /******************************************************************************
230 * CLSIDFromString32 [OLE32.3]
231 * Converts a unique identifier from it's string representation into
232 * the GUID struct.
233 * RETURNS
234 * the converted GUID
236 OLESTATUS WINAPI CLSIDFromString32(
237 LPCOLESTR32 idstr, /* [in] string representation of GUID */
238 CLSID *id /* [out] GUID represented by above string */
240 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
241 OLESTATUS ret = CLSIDFromString16(xid,id);
243 HeapFree(GetProcessHeap(),0,xid);
244 return ret;
247 /******************************************************************************
248 * WINE_StringFromCLSID [???]
249 * Converts a GUID into the respective string representation.
251 * NOTES
252 * Why is this WINAPI?
254 * RETURNS
255 * the string representation and OLESTATUS
257 OLESTATUS WINAPI WINE_StringFromCLSID(
258 const CLSID *id, /* [in] GUID to be converted */
259 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
261 static const char *hex = "0123456789ABCDEF";
262 char *s;
263 int i;
265 if (!id)
266 { ERR(ole,"called with id=Null\n");
267 *idstr = 0x00;
268 return E_FAIL;
271 sprintf(idstr, "{%08lx-%04x-%04x-%02x%02x-",
272 id->Data1, id->Data2, id->Data3,
273 id->Data4[0], id->Data4[1]);
274 s = &idstr[25];
276 /* 6 hex bytes */
277 for (i = 2; i < 8; i++) {
278 *s++ = hex[id->Data4[i]>>4];
279 *s++ = hex[id->Data4[i] & 0xf];
282 *s++ = '}';
283 *s++ = '\0';
285 for (i = strlen(idstr)-1; i >= 0; i--) {
286 idstr[i] = toupper(idstr[i]);
289 TRACE(ole,"%p->%s\n", id, idstr);
291 return OLE_OK;
294 /******************************************************************************
295 * StringFromCLSID16 [COMPOBJ.19]
296 * Converts a GUID into the respective string representation.
297 * The target string is allocated using the OLE IMalloc.
298 * RETURNS
299 * the string representation and OLESTATUS
301 OLESTATUS WINAPI StringFromCLSID16(
302 const CLSID *id, /* [in] the GUID to be converted */
303 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
306 LPMALLOC16 mllc;
307 OLESTATUS ret;
308 DWORD args[2];
310 ret = CoGetMalloc16(0,&mllc);
311 if (ret) return ret;
313 args[0] = (DWORD)mllc;
314 args[1] = 40;
316 /* No need for a Callback entry, we have WOWCallback16Ex which does
317 * everything we need.
319 if (!WOWCallback16Ex(
320 (FARPROC16)((LPMALLOC16_VTABLE)PTR_SEG_TO_LIN(
321 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
322 )->fnAlloc,
323 WCB16_CDECL,
325 (LPVOID)args,
326 (LPDWORD)idstr
327 )) {
328 WARN(ole,"CallTo16 IMalloc16 failed\n");
329 return E_FAIL;
331 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
334 /******************************************************************************
335 * StringFromCLSID32 [OLE32.151]
336 * Converts a GUID into the respective string representation.
337 * The target string is allocated using the OLE IMalloc.
338 * RETURNS
339 * the string representation and OLESTATUS
341 OLESTATUS WINAPI StringFromCLSID32(
342 const CLSID *id, /* [in] the GUID to be converted */
343 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
345 char buf[80];
346 OLESTATUS ret;
347 LPMALLOC32 mllc;
349 if ((ret=CoGetMalloc32(0,&mllc)))
350 return ret;
352 ret=WINE_StringFromCLSID(id,buf);
353 if (!ret) {
354 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
355 lstrcpyAtoW(*idstr,buf);
357 return ret;
360 /******************************************************************************
361 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
363 * Converts a global unique identifier into a string of an API-
364 * specified fixed format. (The usual {.....} stuff.)
366 * RETURNS
367 * The (UNICODE) string representation of the GUID in 'str'
368 * The length of the resulting string, 0 if there was any problem.
370 INT32 WINAPI
371 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
373 char xguid[80];
375 if (WINE_StringFromCLSID(id,xguid))
376 return 0;
377 if (strlen(xguid)>=cmax)
378 return 0;
379 lstrcpyAtoW(str,xguid);
380 return strlen(xguid);
383 /******************************************************************************
384 * CLSIDFromProgID16 [COMPOBJ.61]
385 * Converts a program id into the respective GUID. (By using a registry lookup)
386 * RETURNS
387 * riid associated with the progid
389 OLESTATUS WINAPI CLSIDFromProgID16(
390 LPCOLESTR16 progid, /* [in] program id as found in registry */
391 LPCLSID riid /* [out] associated CLSID */
393 char *buf,buf2[80];
394 DWORD buf2len;
395 HRESULT err;
396 HKEY xhkey;
398 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
399 sprintf(buf,"%s\\CLSID",progid);
400 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
401 HeapFree(GetProcessHeap(),0,buf);
402 return OLE_ERROR_GENERIC;
404 HeapFree(GetProcessHeap(),0,buf);
405 buf2len = sizeof(buf2);
406 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
407 RegCloseKey(xhkey);
408 return OLE_ERROR_GENERIC;
410 RegCloseKey(xhkey);
411 return CLSIDFromString16(buf2,riid);
414 /******************************************************************************
415 * CLSIDFromProgID32 [OLE32.2]
416 * Converts a program id into the respective GUID. (By using a registry lookup)
417 * RETURNS
418 * riid associated with the progid
420 OLESTATUS WINAPI CLSIDFromProgID32(
421 LPCOLESTR32 progid, /* [in] program id as found in registry */
422 LPCLSID riid /* [out] associated CLSID */
424 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
425 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
427 HeapFree(GetProcessHeap(),0,pid);
428 return ret;
431 /***********************************************************************
432 * LookupETask (COMPOBJ.94)
434 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
435 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
436 if ((*hTask = GetCurrentTask()) == hETask) {
437 memcpy(p, Table_ETask, sizeof(Table_ETask));
439 return 0;
442 /***********************************************************************
443 * SetETask (COMPOBJ.95)
445 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
446 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
447 hETask = hTask;
448 return 0;
451 /***********************************************************************
452 * CallObjectInWOW (COMPOBJ.201)
454 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
455 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
456 return 0;
459 /******************************************************************************
460 * CoRegisterClassObject16 [COMPOBJ.5]
462 * Don't know where it registers it ...
464 OLESTATUS WINAPI CoRegisterClassObject16(
465 REFCLSID rclsid,
466 LPUNKNOWN pUnk,
467 DWORD dwClsContext,
468 DWORD flags,
469 LPDWORD lpdwRegister
471 char buf[80];
473 WINE_StringFromCLSID(rclsid,buf);
475 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
476 buf,pUnk,dwClsContext,flags,lpdwRegister
478 return 0;
481 /******************************************************************************
482 * CoRegisterClassObject32 [OLE32.36]
484 * Don't know where it registers it ...
486 OLESTATUS WINAPI CoRegisterClassObject32(
487 REFCLSID rclsid,
488 LPUNKNOWN pUnk,
489 DWORD dwClsContext,
490 DWORD flags,
491 LPDWORD lpdwRegister
493 char buf[80];
495 WINE_StringFromCLSID(rclsid,buf);
497 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
498 buf,pUnk,dwClsContext,flags,lpdwRegister
500 return 0;
503 /***********************************************************************
504 * CoGetClassObject [COMPOBJ.7]
506 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
507 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
509 char xclsid[50],xiid[50];
510 LPCLASSFACTORY lpclf;
511 HRESULT hres = E_UNEXPECTED;
513 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
514 WINE_StringFromCLSID((LPCLSID)iid,xiid);
515 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
517 *ppv = NULL;
518 lpclf = IClassFactory_Constructor();
519 if (lpclf)
521 hres = lpclf->lpvtbl->fnQueryInterface(lpclf,iid, ppv);
522 lpclf->lpvtbl->fnRelease(lpclf);
524 return hres;
527 /******************************************************************************
528 * CoRegisterMessageFilter16 [COMPOBJ.27]
530 OLESTATUS WINAPI CoRegisterMessageFilter16(
531 LPMESSAGEFILTER lpMessageFilter,
532 LPMESSAGEFILTER *lplpMessageFilter
534 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
535 return 0;
538 /***********************************************************************
539 * CoCreateInstance [COMPOBJ.13, OLE32.7]
541 HRESULT WINAPI CoCreateInstance(
542 REFCLSID rclsid,
543 LPUNKNOWN pUnkOuter,
544 DWORD dwClsContext,
545 REFIID iid,
546 LPVOID *ppv
548 #if 0
549 char buf[80],xbuf[80];
551 if (rclsid)
552 WINE_StringFromCLSID(rclsid,buf);
553 else
554 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
555 if (iid)
556 WINE_StringFromCLSID(iid,xbuf);
557 else
558 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
560 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
561 *ppv = NULL;
562 #else
563 HRESULT hres;
564 LPCLASSFACTORY lpclf = 0;
566 CoGetClassObject(rclsid, dwClsContext, NULL, &IID_IClassFactory, (LPVOID)&lpclf);
567 hres = lpclf->lpvtbl->fnCreateInstance(lpclf, pUnkOuter, iid, ppv);
568 lpclf->lpvtbl->fnRelease(lpclf);
569 return hres;
570 #endif
573 /***********************************************************************
574 * CoFreeUnusedLibraries [COMPOBJ.17]
576 void WINAPI CoFreeUnusedLibraries()
578 FIXME(ole,"(), stub !\n");
581 /***********************************************************************
582 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
583 * RETURNS
584 * the current system time in lpFileTime
586 HRESULT WINAPI CoFileTimeNow(
587 FILETIME *lpFileTime /* [out] the current time */
589 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
590 return S_OK;
593 /***********************************************************************
594 * CoTaskMemAlloc (OLE32.43)
595 * RETURNS
596 * pointer to newly allocated block
598 LPVOID WINAPI CoTaskMemAlloc(
599 ULONG size /* [in] size of memoryblock to be allocated */
601 LPMALLOC32 lpmalloc;
602 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
604 if (ret)
605 return NULL;
606 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
609 /***********************************************************************
610 * CoTaskMemFree (OLE32.44)
612 VOID WINAPI CoTaskMemFree(
613 LPVOID ptr /* [in] pointer to be freed */
615 LPMALLOC32 lpmalloc;
616 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
618 if (ret) return;
619 return lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
622 /***********************************************************************
623 * CoInitializeWOW (OLE32.27)
625 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
626 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
627 return 0;
630 /******************************************************************************
631 * CoLockObjectExternal16 [COMPOBJ.63]
633 HRESULT WINAPI CoLockObjectExternal16(
634 LPUNKNOWN pUnk, /* [in] object to be locked */
635 BOOL16 fLock, /* [in] do lock */
636 BOOL16 fLastUnlockReleases /* [in] ? */
638 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
639 return S_OK;
642 /******************************************************************************
643 * CoLockObjectExternal32 [OLE32.31]
645 HRESULT WINAPI CoLockObjectExternal32(
646 LPUNKNOWN pUnk, /* [in] object to be locked */
647 BOOL32 fLock, /* [in] do lock */
648 BOOL32 fLastUnlockReleases /* [in] ? */
650 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
651 return S_OK;
654 /***********************************************************************
655 * CoGetState16 [COMPOBJ.115]
657 HRESULT WINAPI CoGetState16(LPDWORD state)
659 FIXME(ole, "(%p),stub!\n", state);
660 *state = 0;
661 return S_OK;
663 /***********************************************************************
664 * CoSetState32 [COM32.42]
666 HRESULT WINAPI CoSetState32(LPDWORD state)
668 FIXME(ole, "(%p),stub!\n", state);
669 *state = 0;
670 return S_OK;