source: trunk/src/ole32/compobj.c@ 7343

Last change on this file since 7343 was 7343, checked in by sandervl, 24 years ago

removed unnecessary changes

File size: 54.6 KB
Line 
1/*
2 * COMPOBJ library
3 *
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
8 */
9
10#include "config.h"
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <string.h>
15#include <assert.h>
16#include "windef.h"
17#include "wtypes.h"
18#include "wingdi.h"
19#include "wine/winbase16.h"
20#include "winerror.h"
21#include "wownt32.h"
22#include "ole2ver.h"
23#include "debugtools.h"
24#include "heap.h"
25#include "winreg.h"
26#include "rpc.h"
27
28#include "wine/obj_base.h"
29#include "wine/obj_misc.h"
30#include "wine/obj_storage.h"
31#include "wine/obj_clientserver.h"
32
33#include "ole.h"
34#include "ifs.h"
35#include "compobj_private.h"
36
37#ifdef __WIN32OS2__
38#include <heapstring.h>
39
40#undef FIXME
41#undef TRACE
42#ifdef DEBUG
43#define TRACE WriteLog("OLE32: %s", __FUNCTION__); WriteLog
44#define FIXME WriteLog("FIXME OLE32: %s", __FUNCTION__); WriteLog
45#else
46#define TRACE 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
47#define FIXME 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
48#endif
49#endif
50
51DEFAULT_DEBUG_CHANNEL(ole);
52
53/****************************************************************************
54 * COM External Lock structures and methods declaration
55 *
56 * This api provides a linked list to managed external references to
57 * COM objects.
58 *
59 * The public interface consists of three calls:
60 * COM_ExternalLockAddRef
61 * COM_ExternalLockRelease
62 * COM_ExternalLockFreeList
63 */
64
65#define EL_END_OF_LIST 0
66#define EL_NOT_FOUND 0
67
68/*
69 * Declaration of the static structure that manage the
70 * external lock to COM objects.
71 */
72typedef struct COM_ExternalLock COM_ExternalLock;
73typedef struct COM_ExternalLockList COM_ExternalLockList;
74
75struct COM_ExternalLock
76{
77 IUnknown *pUnk; /* IUnknown referenced */
78 ULONG uRefCount; /* external lock counter to IUnknown object*/
79 COM_ExternalLock *next; /* Pointer to next element in list */
80};
81
82struct COM_ExternalLockList
83{
84 COM_ExternalLock *head; /* head of list */
85};
86
87/*
88 * Declaration and initialization of the static structure that manages
89 * the external lock to COM objects.
90 */
91static COM_ExternalLockList elList = { EL_END_OF_LIST };
92
93/*
94 * Public Interface to the external lock list
95 */
96static void COM_ExternalLockFreeList();
97static void COM_ExternalLockAddRef(IUnknown *pUnk);
98static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
99void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
100
101/*
102 * Private methods used to managed the linked list
103 */
104static BOOL COM_ExternalLockInsert(
105 IUnknown *pUnk);
106
107static void COM_ExternalLockDelete(
108 COM_ExternalLock *element);
109
110static COM_ExternalLock* COM_ExternalLockFind(
111 IUnknown *pUnk);
112
113static COM_ExternalLock* COM_ExternalLockLocate(
114 COM_ExternalLock *element,
115 IUnknown *pUnk);
116
117/****************************************************************************
118 * This section defines variables internal to the COM module.
119 *
120 * TODO: Most of these things will have to be made thread-safe.
121 */
122HINSTANCE16 COMPOBJ_hInstance = 0;
123HINSTANCE COMPOBJ_hInstance32 = 0;
124static int COMPOBJ_Attach = 0;
125
126LPMALLOC16 currentMalloc16=NULL;
127LPMALLOC currentMalloc32=NULL;
128
129HTASK16 hETask = 0;
130WORD Table_ETask[62];
131
132/*
133 * This lock count counts the number of times CoInitialize is called. It is
134 * decreased every time CoUninitialize is called. When it hits 0, the COM
135 * libraries are freed
136 */
137static ULONG s_COMLockCount = 0;
138
139/*
140 * This linked list contains the list of registered class objects. These
141 * are mostly used to register the factories for out-of-proc servers of OLE
142 * objects.
143 *
144 * TODO: Make this data structure aware of inter-process communication. This
145 * means that parts of this will be exported to the Wine Server.
146 */
147typedef struct tagRegisteredClass
148{
149 CLSID classIdentifier;
150 LPUNKNOWN classObject;
151 DWORD runContext;
152 DWORD connectFlags;
153 DWORD dwCookie;
154 struct tagRegisteredClass* nextClass;
155} RegisteredClass;
156
157static RegisteredClass* firstRegisteredClass = NULL;
158
159/* this open DLL table belongs in a per process table, but my guess is that
160 * it shouldn't live in the kernel, so I'll put them out here in DLL
161 * space assuming that there is one OLE32 per process.
162 */
163typedef struct tagOpenDll {
164 HINSTANCE hLibrary;
165 struct tagOpenDll *next;
166} OpenDll;
167
168static OpenDll *openDllList = NULL; /* linked list of open dlls */
169
170/*****************************************************************************
171 * This section contains prototypes to internal methods for this
172 * module
173 */
174static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
175 DWORD dwClsContext,
176 LPUNKNOWN* ppUnk);
177
178static void COM_RevokeAllClasses();
179
180
181/******************************************************************************
182 * CoBuildVersion [COMPOBJ.1]
183 *
184 * RETURNS
185 * Current build version, hiword is majornumber, loword is minornumber
186 */
187DWORD WINAPI CoBuildVersion(void)
188{
189 TRACE("Returning version %d, build %d.\n", rmm, rup);
190 return (rmm<<16)+rup;
191}
192
193#ifndef __WIN32OS2__
194/******************************************************************************
195 * CoInitialize16 [COMPOBJ.2]
196 * Set the win16 IMalloc used for memory management
197 */
198HRESULT WINAPI CoInitialize16(
199 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
200) {
201 currentMalloc16 = (LPMALLOC16)lpReserved;
202 return S_OK;
203}
204#endif
205
206/******************************************************************************
207 * CoInitialize [OLE32.26]
208 *
209 * Initializes the COM libraries.
210 *
211 * See CoInitializeEx
212 */
213HRESULT WINAPI CoInitialize(
214 LPVOID lpReserved /* [in] pointer to win32 malloc interface
215 (obsolete, should be NULL) */
216)
217{
218 /*
219 * Just delegate to the newer method.
220 */
221 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
222}
223
224/******************************************************************************
225 * CoInitializeEx [OLE32.163]
226 *
227 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
228 * used for memory management is obsolete.
229 *
230 * RETURNS
231 * S_OK if successful,
232 * S_FALSE if this function was called already.
233 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
234 * threading model.
235 *
236 * BUGS
237 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
238 * is never returned.
239 *
240 * See the windows documentation for more details.
241 */
242HRESULT WINAPI CoInitializeEx(
243 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
244 (obsolete, should be NULL) */
245 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
246)
247{
248 HRESULT hr;
249
250 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
251
252 if (lpReserved!=NULL)
253 {
254 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
255 }
256
257 /*
258 * Check for unsupported features.
259 */
260 if (dwCoInit!=COINIT_APARTMENTTHREADED)
261 {
262 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
263 /* Hope for the best and continue anyway */
264 }
265
266 /*
267 * Check the lock count. If this is the first time going through the initialize
268 * process, we have to initialize the libraries.
269 */
270 if (s_COMLockCount==0)
271 {
272 /*
273 * Initialize the various COM libraries and data structures.
274 */
275 TRACE("() - Initializing the COM libraries\n");
276
277 RunningObjectTableImpl_Initialize();
278
279 hr = S_OK;
280 }
281 else
282 hr = S_FALSE;
283
284 /*
285 * Crank-up that lock count.
286 */
287 s_COMLockCount++;
288
289 return hr;
290}
291
292#ifndef __WIN32OS2__
293/***********************************************************************
294 * CoUninitialize16 [COMPOBJ.3]
295 * Don't know what it does.
296 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
297 * believe is the correct spelling
298 */
299void WINAPI CoUninitialize16(void)
300{
301 TRACE("()\n");
302 CoFreeAllLibraries();
303}
304#endif
305
306/***********************************************************************
307 * CoUninitialize [OLE32.47]
308 *
309 * This method will release the COM libraries.
310 *
311 * See the windows documentation for more details.
312 */
313void WINAPI CoUninitialize(void)
314{
315 TRACE("()\n");
316
317 /*
318 * Decrease the reference count.
319 */
320 s_COMLockCount--;
321
322 /*
323 * If we are back to 0 locks on the COM library, make sure we free
324 * all the associated data structures.
325 */
326 if (s_COMLockCount==0)
327 {
328 /*
329 * Release the various COM libraries and data structures.
330 */
331 TRACE("() - Releasing the COM libraries\n");
332
333 RunningObjectTableImpl_UnInitialize();
334 /*
335 * Release the references to the registered class objects.
336 */
337 COM_RevokeAllClasses();
338
339 /*
340 * This will free the loaded COM Dlls.
341 */
342 CoFreeAllLibraries();
343
344 /*
345 * This will free list of external references to COM objects.
346 */
347 COM_ExternalLockFreeList();
348}
349}
350
351#ifndef __WIN32OS2__
352/***********************************************************************
353 * CoGetMalloc16 [COMPOBJ.4]
354 * RETURNS
355 * The current win16 IMalloc
356 */
357HRESULT WINAPI CoGetMalloc16(
358 DWORD dwMemContext, /* [in] unknown */
359 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
360) {
361 if(!currentMalloc16)
362 currentMalloc16 = IMalloc16_Constructor();
363 *lpMalloc = currentMalloc16;
364 return S_OK;
365}
366#endif
367
368/******************************************************************************
369 * CoGetMalloc [OLE32.20]
370 *
371 * RETURNS
372 * The current win32 IMalloc
373 */
374HRESULT WINAPI CoGetMalloc(
375 DWORD dwMemContext, /* [in] unknown */
376 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
377) {
378 if(!currentMalloc32)
379 currentMalloc32 = IMalloc_Constructor();
380 *lpMalloc = currentMalloc32;
381 return S_OK;
382}
383
384#ifndef __WIN32OS2__
385/***********************************************************************
386 * CoCreateStandardMalloc16 [COMPOBJ.71]
387 */
388HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
389 LPMALLOC16 *lpMalloc)
390{
391 /* FIXME: docu says we shouldn't return the same allocator as in
392 * CoGetMalloc16 */
393 *lpMalloc = IMalloc16_Constructor();
394 return S_OK;
395}
396
397/******************************************************************************
398 * CoDisconnectObject [COMPOBJ.15]
399 */
400HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
401{
402 TRACE("(%p, %lx)\n",lpUnk,reserved);
403 return S_OK;
404}
405
406/***********************************************************************
407 * IsEqualGUID16 [COMPOBJ.18]
408 *
409 * Compares two Unique Identifiers.
410 *
411 * RETURNS
412 * TRUE if equal
413 */
414BOOL16 WINAPI IsEqualGUID16(
415 GUID* g1, /* [in] unique id 1 */
416 GUID* g2 /* [in] unique id 2 */
417) {
418 return !memcmp( g1, g2, sizeof(GUID) );
419}
420#endif
421
422/******************************************************************************
423 * CLSIDFromString16 [COMPOBJ.20]
424 * Converts a unique identifier from its string representation into
425 * the GUID struct.
426 *
427 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
428 *
429 * RETURNS
430 * the converted GUID
431 */
432HRESULT WINAPI CLSIDFromString16(
433 LPCOLESTR16 idstr, /* [in] string representation of guid */
434 CLSID *id /* [out] GUID converted from string */
435) {
436 BYTE *s = (BYTE *) idstr;
437 BYTE *p;
438 int i;
439 BYTE table[256];
440
441 if (!s)
442 s = "{00000000-0000-0000-0000-000000000000}";
443 else { /* validate the CLSID string */
444
445 if (strlen(s) != 38)
446 return CO_E_CLASSSTRING;
447
448 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
449 return CO_E_CLASSSTRING;
450
451 for (i=1; i<37; i++)
452 {
453 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
454 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
455 ((s[i] >= 'a') && (s[i] <= 'f')) ||
456 ((s[i] >= 'A') && (s[i] <= 'F')))
457 )
458 return CO_E_CLASSSTRING;
459 }
460 }
461
462 TRACE("%s -> %p\n", s, id);
463
464 /* quick lookup table */
465 memset(table, 0, 256);
466
467 for (i = 0; i < 10; i++) {
468 table['0' + i] = i;
469 }
470 for (i = 0; i < 6; i++) {
471 table['A' + i] = i+10;
472 table['a' + i] = i+10;
473 }
474
475 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
476
477 p = (BYTE *) id;
478
479 s++; /* skip leading brace */
480 for (i = 0; i < 4; i++) {
481 p[3 - i] = table[*s]<<4 | table[*(s+1)];
482 s += 2;
483 }
484 p += 4;
485 s++; /* skip - */
486
487 for (i = 0; i < 2; i++) {
488 p[1-i] = table[*s]<<4 | table[*(s+1)];
489 s += 2;
490 }
491 p += 2;
492 s++; /* skip - */
493
494 for (i = 0; i < 2; i++) {
495 p[1-i] = table[*s]<<4 | table[*(s+1)];
496 s += 2;
497 }
498 p += 2;
499 s++; /* skip - */
500
501 /* these are just sequential bytes */
502 for (i = 0; i < 2; i++) {
503 *p++ = table[*s]<<4 | table[*(s+1)];
504 s += 2;
505 }
506 s++; /* skip - */
507
508 for (i = 0; i < 6; i++) {
509 *p++ = table[*s]<<4 | table[*(s+1)];
510 s += 2;
511 }
512
513 return S_OK;
514}
515
516/******************************************************************************
517 * CoCreateGuid[OLE32.6]
518 *
519 */
520HRESULT WINAPI CoCreateGuid(
521 GUID *pguid /* [out] points to the GUID to initialize */
522) {
523 return UuidCreate(pguid);
524}
525
526/******************************************************************************
527 * CLSIDFromString [OLE32.3]
528 * Converts a unique identifier from its string representation into
529 * the GUID struct.
530 *
531 * UNDOCUMENTED
532 * If idstr is not a valid CLSID string then it gets treated as a ProgID
533 *
534 * RETURNS
535 * the converted GUID
536 */
537HRESULT WINAPI CLSIDFromString(
538 LPCOLESTR idstr, /* [in] string representation of GUID */
539 CLSID *id /* [out] GUID represented by above string */
540) {
541 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
542 HRESULT ret = CLSIDFromString16(xid,id);
543
544 HeapFree(GetProcessHeap(),0,xid);
545 if(ret != S_OK) { /* It appears a ProgID is also valid */
546 ret = CLSIDFromProgID(idstr, id);
547 }
548 return ret;
549}
550
551/******************************************************************************
552 * WINE_StringFromCLSID [Internal]
553 * Converts a GUID into the respective string representation.
554 *
555 * NOTES
556 *
557 * RETURNS
558 * the string representation and HRESULT
559 */
560#ifdef __WIN32OS2__
561HRESULT WINAPI WINE_StringFromCLSID(
562 const CLSID *id, /* [in] GUID to be converted */
563 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
564#else
565static HRESULT WINE_StringFromCLSID(
566 const CLSID *id, /* [in] GUID to be converted */
567 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
568#endif
569) {
570 static const char *hex = "0123456789ABCDEF";
571 char *s;
572 int i;
573
574 if (!id)
575 { ERR("called with id=Null\n");
576 *idstr = 0x00;
577 return E_FAIL;
578 }
579
580 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
581 id->Data1, id->Data2, id->Data3,
582 id->Data4[0], id->Data4[1]);
583 s = &idstr[25];
584
585 /* 6 hex bytes */
586 for (i = 2; i < 8; i++) {
587 *s++ = hex[id->Data4[i]>>4];
588 *s++ = hex[id->Data4[i] & 0xf];
589 }
590
591 *s++ = '}';
592 *s++ = '\0';
593
594 TRACE("%p->%s\n", id, idstr);
595
596 return S_OK;
597}
598#ifndef __WIN32OS2__
599/******************************************************************************
600 * StringFromCLSID16 [COMPOBJ.19]
601 * Converts a GUID into the respective string representation.
602 * The target string is allocated using the OLE IMalloc.
603 * RETURNS
604 * the string representation and HRESULT
605 */
606HRESULT WINAPI StringFromCLSID16(
607 REFCLSID id, /* [in] the GUID to be converted */
608 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
609
610) {
611 extern BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
612 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode );
613 LPMALLOC16 mllc;
614 HRESULT ret;
615 DWORD args[2];
616
617 ret = CoGetMalloc16(0,&mllc);
618 if (ret) return ret;
619
620 args[0] = (DWORD)mllc;
621 args[1] = 40;
622
623 /* No need for a Callback entry, we have WOWCallback16Ex which does
624 * everything we need.
625 */
626 if (!K32WOWCallback16Ex(
627 (DWORD)((ICOM_VTABLE(IMalloc16)*)MapSL(
628 (SEGPTR)ICOM_VTBL(((LPMALLOC16)MapSL((SEGPTR)mllc))))
629 )->Alloc,
630 WCB16_CDECL,
631 2*sizeof(DWORD),
632 (LPVOID)args,
633 (LPDWORD)idstr
634 )) {
635 WARN("CallTo16 IMalloc16 failed\n");
636 return E_FAIL;
637 }
638 return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
639}
640#endif
641/******************************************************************************
642 * StringFromCLSID [OLE32.151]
643 * Converts a GUID into the respective string representation.
644 * The target string is allocated using the OLE IMalloc.
645 * RETURNS
646 * the string representation and HRESULT
647 */
648HRESULT WINAPI StringFromCLSID(
649 REFCLSID id, /* [in] the GUID to be converted */
650 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
651) {
652 char buf[80];
653 HRESULT ret;
654 LPMALLOC mllc;
655
656 if ((ret=CoGetMalloc(0,&mllc)))
657 return ret;
658
659 ret=WINE_StringFromCLSID(id,buf);
660 if (!ret) {
661 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
662 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
663 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
664 }
665 return ret;
666}
667
668/******************************************************************************
669 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
670 *
671 * Converts a global unique identifier into a string of an API-
672 * specified fixed format. (The usual {.....} stuff.)
673 *
674 * RETURNS
675 * The (UNICODE) string representation of the GUID in 'str'
676 * The length of the resulting string, 0 if there was any problem.
677 */
678INT WINAPI
679StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
680{
681 char xguid[80];
682
683 if (WINE_StringFromCLSID(id,xguid))
684 return 0;
685 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
686}
687
688/******************************************************************************
689 * ProgIDFromCLSID [OLE32.133]
690 * Converts a class id into the respective Program ID. (By using a registry lookup)
691 * RETURNS S_OK on success
692 * riid associated with the progid
693 */
694
695HRESULT WINAPI ProgIDFromCLSID(
696 REFCLSID clsid, /* [in] class id as found in registry */
697 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
698)
699{
700 char strCLSID[50], *buf, *buf2;
701 DWORD buf2len;
702 HKEY xhkey;
703 LPMALLOC mllc;
704 HRESULT ret = S_OK;
705
706 WINE_StringFromCLSID(clsid, strCLSID);
707
708 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
709 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
710 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
711 ret = REGDB_E_CLASSNOTREG;
712
713 HeapFree(GetProcessHeap(), 0, buf);
714
715 if (ret == S_OK)
716 {
717 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
718 buf2len = 255;
719 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
720 ret = REGDB_E_CLASSNOTREG;
721
722 if (ret == S_OK)
723 {
724 if (CoGetMalloc(0,&mllc))
725 ret = E_OUTOFMEMORY;
726 else
727 {
728 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf2, -1, NULL, 0 );
729 *lplpszProgID = IMalloc_Alloc(mllc, len * sizeof(WCHAR) );
730 MultiByteToWideChar( CP_ACP, 0, buf2, -1, *lplpszProgID, len );
731 }
732 }
733 HeapFree(GetProcessHeap(), 0, buf2);
734 }
735
736 RegCloseKey(xhkey);
737 return ret;
738}
739
740/******************************************************************************
741 * CLSIDFromProgID16 [COMPOBJ.61]
742 * Converts a program id into the respective GUID. (By using a registry lookup)
743 * RETURNS
744 * riid associated with the progid
745 */
746HRESULT WINAPI CLSIDFromProgID16(
747 LPCOLESTR16 progid, /* [in] program id as found in registry */
748 LPCLSID riid /* [out] associated CLSID */
749) {
750 char *buf,buf2[80];
751 DWORD buf2len;
752 HRESULT err;
753 HKEY xhkey;
754
755 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
756 sprintf(buf,"%s\\CLSID",progid);
757 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
758 HeapFree(GetProcessHeap(),0,buf);
759 return CO_E_CLASSSTRING;
760 }
761 HeapFree(GetProcessHeap(),0,buf);
762 buf2len = sizeof(buf2);
763 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
764 RegCloseKey(xhkey);
765 return CO_E_CLASSSTRING;
766 }
767 RegCloseKey(xhkey);
768 return CLSIDFromString16(buf2,riid);
769}
770
771/******************************************************************************
772 * CLSIDFromProgID [OLE32.2]
773 * Converts a program id into the respective GUID. (By using a registry lookup)
774 * RETURNS
775 * riid associated with the progid
776 */
777HRESULT WINAPI CLSIDFromProgID(
778 LPCOLESTR progid, /* [in] program id as found in registry */
779 LPCLSID riid /* [out] associated CLSID */
780) {
781 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
782 HRESULT ret = CLSIDFromProgID16(pid,riid);
783
784 HeapFree(GetProcessHeap(),0,pid);
785 return ret;
786}
787
788
789
790/*****************************************************************************
791 * CoGetPSClsid [OLE32.22]
792 *
793 * This function returns the CLSID of the DLL that implements the proxy and stub
794 * for the specified interface.
795 *
796 * It determines this by searching the
797 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32 in the registry
798 * and any interface id registered by CoRegisterPSClsid within the current process.
799 *
800 * FIXME: We only search the registry, not ids registered with CoRegisterPSClsid.
801 */
802HRESULT WINAPI CoGetPSClsid(
803 REFIID riid, /* [in] Interface whose proxy/stub CLSID is to be returned */
804 CLSID *pclsid ) /* [out] Where to store returned proxy/stub CLSID */
805{
806 char *buf, buf2[40];
807 DWORD buf2len;
808 HKEY xhkey;
809
810 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
811
812 /* Get the input iid as a string */
813 WINE_StringFromCLSID(riid, buf2);
814 /* Allocate memory for the registry key we will construct.
815 (length of iid string plus constant length of static text */
816 buf = HeapAlloc(GetProcessHeap(), 0, strlen(buf2)+27);
817 if (buf == NULL)
818 {
819 return (E_OUTOFMEMORY);
820 }
821
822 /* Construct the registry key we want */
823 sprintf(buf,"Interface\\%s\\ProxyStubClsid32", buf2);
824
825 /* Open the key.. */
826 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
827 {
828 HeapFree(GetProcessHeap(),0,buf);
829 return (E_INVALIDARG);
830 }
831 HeapFree(GetProcessHeap(),0,buf);
832
833 /* ... Once we have the key, query the registry to get the
834 value of CLSID as a string, and convert it into a
835 proper CLSID structure to be passed back to the app */
836 buf2len = sizeof(buf2);
837 if ( (RegQueryValueA(xhkey,NULL,buf2,&buf2len)) )
838 {
839 RegCloseKey(xhkey);
840 return E_INVALIDARG;
841 }
842 RegCloseKey(xhkey);
843
844 /* We have the CLSid we want back from the registry as a string, so
845 lets convert it into a CLSID structure */
846 if ( (CLSIDFromString16(buf2,pclsid)) != NOERROR)
847 {
848 return E_INVALIDARG;
849 }
850
851 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
852 return (S_OK);
853}
854
855
856
857/***********************************************************************
858 * WriteClassStm
859 *
860 * This function write a CLSID on stream
861 */
862HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
863{
864 TRACE("(%p,%p)\n",pStm,rclsid);
865
866 if (rclsid==NULL)
867 return E_INVALIDARG;
868
869 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
870}
871
872/***********************************************************************
873 * ReadClassStm
874 *
875 * This function read a CLSID from a stream
876 */
877HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
878{
879 ULONG nbByte;
880 HRESULT res;
881
882 TRACE("(%p,%p)\n",pStm,rclsid);
883
884 if (rclsid==NULL)
885 return E_INVALIDARG;
886
887 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
888
889 if (FAILED(res))
890 return res;
891
892 if (nbByte != sizeof(CLSID))
893 return S_FALSE;
894 else
895 return S_OK;
896}
897
898#ifndef __WIN32OS2__
899/* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
900/***********************************************************************
901 * LookupETask (COMPOBJ.94)
902 */
903HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
904 FIXME("(%p,%p),stub!\n",hTask,p);
905 if ((*hTask = GetCurrentTask()) == hETask) {
906 memcpy(p, Table_ETask, sizeof(Table_ETask));
907 }
908 return 0;
909}
910
911/* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
912/***********************************************************************
913 * SetETask (COMPOBJ.95)
914 */
915HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
916 FIXME("(%04x,%p),stub!\n",hTask,p);
917 hETask = hTask;
918 return 0;
919}
920
921/* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
922/***********************************************************************
923 * CallObjectInWOW (COMPOBJ.201)
924 */
925HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
926 FIXME("(%p,%p),stub!\n",p1,p2);
927 return 0;
928}
929
930/******************************************************************************
931 * CoRegisterClassObject16 [COMPOBJ.5]
932 *
933 * Don't know where it registers it ...
934 */
935HRESULT WINAPI CoRegisterClassObject16(
936 REFCLSID rclsid,
937 LPUNKNOWN pUnk,
938 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
939 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
940 LPDWORD lpdwRegister
941) {
942 char buf[80];
943
944 WINE_StringFromCLSID(rclsid,buf);
945
946 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
947 buf,pUnk,dwClsContext,flags,lpdwRegister
948 );
949 return 0;
950}
951
952
953/******************************************************************************
954 * CoRevokeClassObject16 [COMPOBJ.6]
955 *
956 */
957HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
958{
959 FIXME("(0x%08lx),stub!\n", dwRegister);
960 return 0;
961}
962#endif
963
964
965/***
966 * COM_GetRegisteredClassObject
967 *
968 * This internal method is used to scan the registered class list to
969 * find a class object.
970 *
971 * Params:
972 * rclsid Class ID of the class to find.
973 * dwClsContext Class context to match.
974 * ppv [out] returns a pointer to the class object. Complying
975 * to normal COM usage, this method will increase the
976 * reference count on this object.
977 */
978static HRESULT COM_GetRegisteredClassObject(
979 REFCLSID rclsid,
980 DWORD dwClsContext,
981 LPUNKNOWN* ppUnk)
982{
983 RegisteredClass* curClass;
984
985 /*
986 * Sanity check
987 */
988 assert(ppUnk!=0);
989
990 /*
991 * Iterate through the whole list and try to match the class ID.
992 */
993 curClass = firstRegisteredClass;
994
995 while (curClass != 0)
996 {
997 /*
998 * Check if we have a match on the class ID.
999 */
1000 if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
1001 {
1002 /*
1003 * Since we don't do out-of process or DCOM just right away, let's ignore the
1004 * class context.
1005 */
1006
1007 /*
1008 * We have a match, return the pointer to the class object.
1009 */
1010 *ppUnk = curClass->classObject;
1011
1012 IUnknown_AddRef(curClass->classObject);
1013
1014 return S_OK;
1015 }
1016
1017 /*
1018 * Step to the next class in the list.
1019 */
1020 curClass = curClass->nextClass;
1021 }
1022
1023 /*
1024 * If we get to here, we haven't found our class.
1025 */
1026 return S_FALSE;
1027}
1028
1029/******************************************************************************
1030 * CoRegisterClassObject [OLE32.36]
1031 *
1032 * This method will register the class object for a given class ID.
1033 *
1034 * See the Windows documentation for more details.
1035 */
1036HRESULT WINAPI CoRegisterClassObject(
1037 REFCLSID rclsid,
1038 LPUNKNOWN pUnk,
1039 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1040 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1041 LPDWORD lpdwRegister
1042)
1043{
1044 RegisteredClass* newClass;
1045 LPUNKNOWN foundObject;
1046 HRESULT hr;
1047 char buf[80];
1048
1049 WINE_StringFromCLSID(rclsid,buf);
1050
1051 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1052 buf,pUnk,dwClsContext,flags,lpdwRegister);
1053
1054 /*
1055 * Perform a sanity check on the parameters
1056 */
1057 if ( (lpdwRegister==0) || (pUnk==0) )
1058 {
1059 return E_INVALIDARG;
1060}
1061
1062 /*
1063 * Initialize the cookie (out parameter)
1064 */
1065 *lpdwRegister = 0;
1066
1067 /*
1068 * First, check if the class is already registered.
1069 * If it is, this should cause an error.
1070 */
1071 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1072
1073 if (hr == S_OK)
1074 {
1075 /*
1076 * The COM_GetRegisteredClassObject increased the reference count on the
1077 * object so it has to be released.
1078 */
1079 IUnknown_Release(foundObject);
1080
1081 return CO_E_OBJISREG;
1082 }
1083
1084 /*
1085 * If it is not registered, we must create a new entry for this class and
1086 * append it to the registered class list.
1087 * We use the address of the chain node as the cookie since we are sure it's
1088 * unique.
1089 */
1090 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1091
1092 /*
1093 * Initialize the node.
1094 */
1095 newClass->classIdentifier = *rclsid;
1096 newClass->runContext = dwClsContext;
1097 newClass->connectFlags = flags;
1098 newClass->dwCookie = (DWORD)newClass;
1099 newClass->nextClass = firstRegisteredClass;
1100
1101 /*
1102 * Since we're making a copy of the object pointer, we have to increase its
1103 * reference count.
1104 */
1105 newClass->classObject = pUnk;
1106 IUnknown_AddRef(newClass->classObject);
1107
1108 firstRegisteredClass = newClass;
1109
1110 /*
1111 * Assign the out parameter (cookie)
1112 */
1113 *lpdwRegister = newClass->dwCookie;
1114
1115 /*
1116 * We're successful Yippee!
1117 */
1118 return S_OK;
1119}
1120
1121/***********************************************************************
1122 * CoRevokeClassObject [OLE32.40]
1123 *
1124 * This method will remove a class object from the class registry
1125 *
1126 * See the Windows documentation for more details.
1127 */
1128HRESULT WINAPI CoRevokeClassObject(
1129 DWORD dwRegister)
1130{
1131 RegisteredClass** prevClassLink;
1132 RegisteredClass* curClass;
1133
1134 TRACE("(%08lx)\n",dwRegister);
1135
1136 /*
1137 * Iterate through the whole list and try to match the cookie.
1138 */
1139 curClass = firstRegisteredClass;
1140 prevClassLink = &firstRegisteredClass;
1141
1142 while (curClass != 0)
1143 {
1144 /*
1145 * Check if we have a match on the cookie.
1146 */
1147 if (curClass->dwCookie == dwRegister)
1148 {
1149 /*
1150 * Remove the class from the chain.
1151 */
1152 *prevClassLink = curClass->nextClass;
1153
1154 /*
1155 * Release the reference to the class object.
1156 */
1157 IUnknown_Release(curClass->classObject);
1158
1159 /*
1160 * Free the memory used by the chain node.
1161 */
1162 HeapFree(GetProcessHeap(), 0, curClass);
1163
1164 return S_OK;
1165}
1166
1167 /*
1168 * Step to the next class in the list.
1169 */
1170 prevClassLink = &(curClass->nextClass);
1171 curClass = curClass->nextClass;
1172 }
1173
1174 /*
1175 * If we get to here, we haven't found our class.
1176 */
1177 return E_INVALIDARG;
1178}
1179
1180/***********************************************************************
1181 * CoGetClassObject [COMPOBJ.7]
1182 */
1183HRESULT WINAPI CoGetClassObject(
1184 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1185 REFIID iid, LPVOID *ppv
1186) {
1187 LPUNKNOWN regClassObject;
1188 HRESULT hres = E_UNEXPECTED;
1189 char xclsid[80];
1190 WCHAR dllName[MAX_PATH+1];
1191 DWORD dllNameLen = sizeof(dllName);
1192 HINSTANCE hLibrary;
1193#ifdef __WIN32OS2__
1194 typedef HRESULT (* CALLBACK DllGetClassObjectFunc)(REFCLSID clsid,
1195 REFIID iid, LPVOID *ppv);
1196#else
1197 typedef HRESULT CALLBACK (*DllGetClassObjectFunc)(REFCLSID clsid,
1198 REFIID iid, LPVOID *ppv);
1199#endif
1200 DllGetClassObjectFunc DllGetClassObject;
1201
1202 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1203
1204 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",
1205 debugstr_guid(rclsid),
1206 debugstr_guid(iid)
1207 );
1208
1209 if (pServerInfo) {
1210 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1211 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1212 }
1213
1214 /*
1215 * First, try and see if we can't match the class ID with one of the
1216 * registered classes.
1217 */
1218 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1219 {
1220 /*
1221 * Get the required interface from the retrieved pointer.
1222 */
1223 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1224
1225 /*
1226 * Since QI got another reference on the pointer, we want to release the
1227 * one we already have. If QI was unsuccessful, this will release the object. This
1228 * is good since we are not returning it in the "out" parameter.
1229 */
1230 IUnknown_Release(regClassObject);
1231
1232 return hres;
1233 }
1234
1235 /* out of process and remote servers not supported yet */
1236 if ( ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1237 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)
1238 ){
1239 FIXME("%s %s not supported!\n",
1240 (dwClsContext&CLSCTX_LOCAL_SERVER)?"CLSCTX_LOCAL_SERVER":"",
1241 (dwClsContext&CLSCTX_REMOTE_SERVER)?"CLSCTX_REMOTE_SERVER":""
1242 );
1243 return E_ACCESSDENIED;
1244 }
1245
1246 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1247 HKEY key;
1248 char buf[200];
1249
1250 sprintf(buf,"CLSID\\%s\\InprocServer32",xclsid);
1251 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key);
1252
1253 if (hres != ERROR_SUCCESS) {
1254 return REGDB_E_CLASSNOTREG;
1255 }
1256
1257 memset(dllName,0,sizeof(dllName));
1258 hres= RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)dllName,&dllNameLen);
1259 if (hres)
1260 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1261 RegCloseKey(key);
1262 TRACE("found InprocServer32 dll %s\n", debugstr_w(dllName));
1263
1264 /* open dll, call DllGetClassObject */
1265 hLibrary = CoLoadLibrary(dllName, TRUE);
1266 if (hLibrary == 0) {
1267 FIXME("couldn't load InprocServer32 dll %s\n", debugstr_w(dllName));
1268 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1269 }
1270 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1271 if (!DllGetClassObject) {
1272 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1273 FIXME("couldn't find function DllGetClassObject in %s\n", debugstr_w(dllName));
1274 return E_ACCESSDENIED;
1275 }
1276
1277 /*
1278 * Ask the DLL for its class object. (there was a note here about class
1279 * factories but this is good.
1280 */
1281 return DllGetClassObject(rclsid, iid, ppv);
1282 }
1283 return hres;
1284}
1285
1286/***********************************************************************
1287 * CoResumeClassObjects
1288 *
1289 * Resumes classobjects registered with REGCLS suspended
1290 */
1291HRESULT WINAPI CoResumeClassObjects(void)
1292{
1293 FIXME("\n");
1294 return S_OK;
1295}
1296
1297/***********************************************************************
1298 * GetClassFile
1299 *
1300 * This function supplies the CLSID associated with the given filename.
1301 */
1302HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1303{
1304 IStorage *pstg=0;
1305 HRESULT res;
1306 int nbElm=0,length=0,i=0;
1307 LONG sizeProgId=20;
1308 LPOLESTR *pathDec=0,absFile=0,progId=0;
1309 WCHAR extention[100]={0};
1310
1311 TRACE("()\n");
1312
1313 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1314 if((StgIsStorageFile(filePathName))==S_OK){
1315
1316 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1317
1318 if (SUCCEEDED(res))
1319 res=ReadClassStg(pstg,pclsid);
1320
1321 IStorage_Release(pstg);
1322
1323 return res;
1324 }
1325 /* if the file is not a storage object then attemps to match various bits in the file against a
1326 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1327 this case
1328
1329 for(i=0;i<nFileTypes;i++)
1330
1331 for(i=0;j<nPatternsForType;j++){
1332
1333 PATTERN pat;
1334 HANDLE hFile;
1335
1336 pat=ReadPatternFromRegistry(i,j);
1337 hFile=CreateFileW(filePathName,,,,,,hFile);
1338 SetFilePosition(hFile,pat.offset);
1339 ReadFile(hFile,buf,pat.size,NULL,NULL);
1340 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1341
1342 *pclsid=ReadCLSIDFromRegistry(i);
1343 return S_OK;
1344 }
1345 }
1346 */
1347
1348 /* if the obove strategies fail then search for the extension key in the registry */
1349
1350 /* get the last element (absolute file) in the path name */
1351 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1352 absFile=pathDec[nbElm-1];
1353
1354 /* failed if the path represente a directory and not an absolute file name*/
1355 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1356 return MK_E_INVALIDEXTENSION;
1357
1358 /* get the extension of the file */
1359 length=lstrlenW(absFile);
1360 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1361
1362 /* get the progId associated to the extension */
1363 progId=CoTaskMemAlloc(sizeProgId);
1364
1365 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1366
1367 if (res==ERROR_MORE_DATA){
1368
1369 progId = CoTaskMemRealloc(progId,sizeProgId);
1370 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1371 }
1372 if (res==ERROR_SUCCESS)
1373 /* return the clsid associated to the progId */
1374 res= CLSIDFromProgID(progId,pclsid);
1375
1376 for(i=0; pathDec[i]!=NULL;i++)
1377 CoTaskMemFree(pathDec[i]);
1378 CoTaskMemFree(pathDec);
1379
1380 CoTaskMemFree(progId);
1381
1382 if (res==ERROR_SUCCESS)
1383 return res;
1384
1385 return MK_E_INVALIDEXTENSION;
1386}
1387#ifndef __WIN32OS2__
1388/******************************************************************************
1389 * CoRegisterMessageFilter16 [COMPOBJ.27]
1390 */
1391HRESULT WINAPI CoRegisterMessageFilter16(
1392 LPMESSAGEFILTER lpMessageFilter,
1393 LPMESSAGEFILTER *lplpMessageFilter
1394) {
1395 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1396 return 0;
1397}
1398#endif
1399
1400/***********************************************************************
1401 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1402 */
1403HRESULT WINAPI CoCreateInstance(
1404 REFCLSID rclsid,
1405 LPUNKNOWN pUnkOuter,
1406 DWORD dwClsContext,
1407 REFIID iid,
1408 LPVOID *ppv)
1409{
1410 HRESULT hres;
1411 LPCLASSFACTORY lpclf = 0;
1412
1413 /*
1414 * Sanity check
1415 */
1416 if (ppv==0)
1417 return E_POINTER;
1418
1419 /*
1420 * Initialize the "out" parameter
1421 */
1422 *ppv = 0;
1423
1424 /*
1425 * Get a class factory to construct the object we want.
1426 */
1427 hres = CoGetClassObject(rclsid,
1428 dwClsContext,
1429 NULL,
1430 &IID_IClassFactory,
1431 (LPVOID)&lpclf);
1432
1433 if (FAILED(hres)) {
1434 FIXME("no instance created for %s, hres is 0x%08lx\n",debugstr_guid(iid),hres);
1435 return hres;
1436 }
1437
1438 /*
1439 * Create the object and don't forget to release the factory
1440 */
1441 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1442 IClassFactory_Release(lpclf);
1443
1444 return hres;
1445}
1446
1447/***********************************************************************
1448 * CoCreateInstanceEx [OLE32.165]
1449 */
1450HRESULT WINAPI CoCreateInstanceEx(
1451 REFCLSID rclsid,
1452 LPUNKNOWN pUnkOuter,
1453 DWORD dwClsContext,
1454 COSERVERINFO* pServerInfo,
1455 ULONG cmq,
1456 MULTI_QI* pResults)
1457{
1458 IUnknown* pUnk = NULL;
1459 HRESULT hr;
1460 ULONG index;
1461 int successCount = 0;
1462
1463 /*
1464 * Sanity check
1465 */
1466 if ( (cmq==0) || (pResults==NULL))
1467 return E_INVALIDARG;
1468
1469 if (pServerInfo!=NULL)
1470 FIXME("() non-NULL pServerInfo not supported!\n");
1471
1472 /*
1473 * Initialize all the "out" parameters.
1474 */
1475 for (index = 0; index < cmq; index++)
1476 {
1477 pResults[index].pItf = NULL;
1478 pResults[index].hr = E_NOINTERFACE;
1479 }
1480
1481 /*
1482 * Get the object and get its IUnknown pointer.
1483 */
1484 hr = CoCreateInstance(rclsid,
1485 pUnkOuter,
1486 dwClsContext,
1487 &IID_IUnknown,
1488 (VOID**)&pUnk);
1489
1490 if (hr)
1491 return hr;
1492
1493 /*
1494 * Then, query for all the interfaces requested.
1495 */
1496 for (index = 0; index < cmq; index++)
1497 {
1498 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1499 pResults[index].pIID,
1500 (VOID**)&(pResults[index].pItf));
1501
1502 if (pResults[index].hr == S_OK)
1503 successCount++;
1504 }
1505
1506 /*
1507 * Release our temporary unknown pointer.
1508 */
1509 IUnknown_Release(pUnk);
1510
1511 if (successCount == 0)
1512 return E_NOINTERFACE;
1513
1514 if (successCount!=cmq)
1515 return CO_S_NOTALLINTERFACES;
1516
1517 return S_OK;
1518}
1519
1520/***********************************************************************
1521 * CoFreeLibrary [COMPOBJ.13]
1522 */
1523void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1524{
1525 OpenDll *ptr, *prev;
1526 OpenDll *tmp;
1527
1528 /* lookup library in linked list */
1529 prev = NULL;
1530 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1531 if (ptr->hLibrary == hLibrary) {
1532 break;
1533 }
1534 prev = ptr;
1535 }
1536
1537 if (ptr == NULL) {
1538 /* shouldn't happen if user passed in a valid hLibrary */
1539 return;
1540 }
1541 /* assert: ptr points to the library entry to free */
1542
1543 /* free library and remove node from list */
1544 FreeLibrary(hLibrary);
1545 if (ptr == openDllList) {
1546 tmp = openDllList->next;
1547 HeapFree(GetProcessHeap(), 0, openDllList);
1548 openDllList = tmp;
1549 } else {
1550 tmp = ptr->next;
1551 HeapFree(GetProcessHeap(), 0, ptr);
1552 prev->next = tmp;
1553 }
1554
1555}
1556
1557
1558/***********************************************************************
1559 * CoFreeAllLibraries [COMPOBJ.12]
1560 */
1561void WINAPI CoFreeAllLibraries(void)
1562{
1563 OpenDll *ptr, *tmp;
1564
1565 for (ptr = openDllList; ptr != NULL; ) {
1566 tmp=ptr->next;
1567 CoFreeLibrary(ptr->hLibrary);
1568 ptr = tmp;
1569 }
1570}
1571
1572
1573
1574/***********************************************************************
1575 * CoFreeUnusedLibraries [COMPOBJ.17]
1576 */
1577void WINAPI CoFreeUnusedLibraries(void)
1578{
1579 OpenDll *ptr, *tmp;
1580 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1581 DllCanUnloadNowFunc DllCanUnloadNow;
1582
1583 for (ptr = openDllList; ptr != NULL; ) {
1584 DllCanUnloadNow = (DllCanUnloadNowFunc)
1585 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1586
1587 if ( (DllCanUnloadNow != NULL) &&
1588 (DllCanUnloadNow() == S_OK) ) {
1589 tmp=ptr->next;
1590 CoFreeLibrary(ptr->hLibrary);
1591 ptr = tmp;
1592 } else {
1593 ptr=ptr->next;
1594 }
1595 }
1596}
1597
1598/***********************************************************************
1599 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1600 * RETURNS
1601 * the current system time in lpFileTime
1602 */
1603HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime ) /* [out] the current time */
1604{
1605 GetSystemTimeAsFileTime( lpFileTime );
1606 return S_OK;
1607}
1608
1609/***********************************************************************
1610 * CoTaskMemAlloc (OLE32.43)
1611 * RETURNS
1612 * pointer to newly allocated block
1613 */
1614LPVOID WINAPI CoTaskMemAlloc(
1615 ULONG size /* [in] size of memoryblock to be allocated */
1616) {
1617 LPMALLOC lpmalloc;
1618 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1619
1620 if (FAILED(ret))
1621 return NULL;
1622
1623 return IMalloc_Alloc(lpmalloc,size);
1624}
1625/***********************************************************************
1626 * CoTaskMemFree (OLE32.44)
1627 */
1628VOID WINAPI CoTaskMemFree(
1629 LPVOID ptr /* [in] pointer to be freed */
1630) {
1631 LPMALLOC lpmalloc;
1632 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1633
1634 if (FAILED(ret))
1635 return;
1636
1637 IMalloc_Free(lpmalloc, ptr);
1638}
1639
1640/***********************************************************************
1641 * CoTaskMemRealloc (OLE32.45)
1642 * RETURNS
1643 * pointer to newly allocated block
1644 */
1645LPVOID WINAPI CoTaskMemRealloc(
1646 LPVOID pvOld,
1647 ULONG size) /* [in] size of memoryblock to be allocated */
1648{
1649 LPMALLOC lpmalloc;
1650 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1651
1652 if (FAILED(ret))
1653 return NULL;
1654
1655 return IMalloc_Realloc(lpmalloc, pvOld, size);
1656}
1657
1658/***********************************************************************
1659 * CoLoadLibrary (OLE32.30)
1660 */
1661HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1662{
1663 HINSTANCE hLibrary;
1664 OpenDll *ptr;
1665 OpenDll *tmp;
1666
1667 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1668
1669 hLibrary = LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1670
1671 if (!bAutoFree)
1672 return hLibrary;
1673
1674 if (openDllList == NULL) {
1675 /* empty list -- add first node */
1676 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1677 openDllList->hLibrary=hLibrary;
1678 openDllList->next = NULL;
1679 } else {
1680 /* search for this dll */
1681 int found = FALSE;
1682 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1683 if (ptr->hLibrary == hLibrary) {
1684 found = TRUE;
1685 break;
1686 }
1687 }
1688 if (!found) {
1689 /* dll not found, add it */
1690 tmp = openDllList;
1691 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1692 openDllList->hLibrary = hLibrary;
1693 openDllList->next = tmp;
1694 }
1695 }
1696
1697 return hLibrary;
1698}
1699
1700/***********************************************************************
1701 * CoInitializeWOW (OLE32.27)
1702 */
1703HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1704 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1705 return 0;
1706}
1707
1708#ifndef __WIN32OS2__
1709/******************************************************************************
1710 * CoLockObjectExternal16 [COMPOBJ.63]
1711 */
1712HRESULT WINAPI CoLockObjectExternal16(
1713 LPUNKNOWN pUnk, /* [in] object to be locked */
1714 BOOL16 fLock, /* [in] do lock */
1715 BOOL16 fLastUnlockReleases /* [in] ? */
1716) {
1717 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1718 return S_OK;
1719}
1720#endif
1721
1722/******************************************************************************
1723 * CoLockObjectExternal [OLE32.31]
1724 */
1725HRESULT WINAPI CoLockObjectExternal(
1726 LPUNKNOWN pUnk, /* [in] object to be locked */
1727 BOOL fLock, /* [in] do lock */
1728 BOOL fLastUnlockReleases) /* [in] unlock all */
1729{
1730
1731 if (fLock)
1732 {
1733 /*
1734 * Increment the external lock coutner, COM_ExternalLockAddRef also
1735 * increment the object's internal lock counter.
1736 */
1737 COM_ExternalLockAddRef( pUnk);
1738 }
1739 else
1740 {
1741 /*
1742 * Decrement the external lock coutner, COM_ExternalLockRelease also
1743 * decrement the object's internal lock counter.
1744 */
1745 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1746 }
1747
1748 return S_OK;
1749}
1750
1751#ifndef __WIN32OS2__
1752/***********************************************************************
1753 * CoGetState16 [COMPOBJ.115]
1754 */
1755HRESULT WINAPI CoGetState16(LPDWORD state)
1756{
1757 FIXME("(%p),stub!\n", state);
1758 *state = 0;
1759 return S_OK;
1760}
1761/***********************************************************************
1762 * CoSetState [COM32.42]
1763 */
1764HRESULT WINAPI CoSetState(LPDWORD state)
1765{
1766 FIXME("(%p),stub!\n", state);
1767 if (state) *state = 0;
1768 return S_OK;
1769}
1770#endif
1771/***********************************************************************
1772 * CoCreateFreeThreadedMarshaler [OLE32.5]
1773 */
1774HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN* ppunkMarshal)
1775{
1776 FIXME ("(%p %p): stub\n", punkOuter, ppunkMarshal);
1777
1778 return S_OK;
1779}
1780
1781
1782/***********************************************************************
1783 * DllGetClassObject [OLE32.63]
1784 */
1785HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1786{
1787 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1788 *ppv = NULL;
1789 return CLASS_E_CLASSNOTAVAILABLE;
1790}
1791
1792
1793/***
1794 * COM_RevokeAllClasses
1795 *
1796 * This method is called when the COM libraries are uninitialized to
1797 * release all the references to the class objects registered with
1798 * the library
1799 */
1800static void COM_RevokeAllClasses()
1801{
1802 while (firstRegisteredClass!=0)
1803 {
1804 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1805 }
1806}
1807
1808/****************************************************************************
1809 * COM External Lock methods implementation
1810 */
1811
1812/****************************************************************************
1813 * Public - Method that increments the count for a IUnknown* in the linked
1814 * list. The item is inserted if not already in the list.
1815 */
1816static void COM_ExternalLockAddRef(
1817 IUnknown *pUnk)
1818{
1819 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1820
1821 /*
1822 * Add an external lock to the object. If it was already externally
1823 * locked, just increase the reference count. If it was not.
1824 * add the item to the list.
1825 */
1826 if ( externalLock == EL_NOT_FOUND )
1827 COM_ExternalLockInsert(pUnk);
1828 else
1829 externalLock->uRefCount++;
1830
1831 /*
1832 * Add an internal lock to the object
1833 */
1834 IUnknown_AddRef(pUnk);
1835}
1836
1837/****************************************************************************
1838 * Public - Method that decrements the count for a IUnknown* in the linked
1839 * list. The item is removed from the list if its count end up at zero or if
1840 * bRelAll is TRUE.
1841 */
1842static void COM_ExternalLockRelease(
1843 IUnknown *pUnk,
1844 BOOL bRelAll)
1845{
1846 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1847
1848 if ( externalLock != EL_NOT_FOUND )
1849 {
1850 do
1851 {
1852 externalLock->uRefCount--; /* release external locks */
1853 IUnknown_Release(pUnk); /* release local locks as well */
1854
1855 if ( bRelAll == FALSE )
1856 break; /* perform single release */
1857
1858 } while ( externalLock->uRefCount > 0 );
1859
1860 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1861 COM_ExternalLockDelete(externalLock);
1862 }
1863}
1864/****************************************************************************
1865 * Public - Method that frees the content of the list.
1866 */
1867static void COM_ExternalLockFreeList()
1868{
1869 COM_ExternalLock *head;
1870
1871 head = elList.head; /* grab it by the head */
1872 while ( head != EL_END_OF_LIST )
1873 {
1874 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1875
1876 head = elList.head; /* get the new head... */
1877 }
1878}
1879
1880/****************************************************************************
1881 * Public - Method that dump the content of the list.
1882 */
1883void COM_ExternalLockDump()
1884{
1885 COM_ExternalLock *current = elList.head;
1886
1887 DPRINTF("\nExternal lock list contains:\n");
1888
1889 while ( current != EL_END_OF_LIST )
1890 {
1891 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
1892
1893 /* Skip to the next item */
1894 current = current->next;
1895 }
1896
1897}
1898
1899/****************************************************************************
1900 * Internal - Find a IUnknown* in the linked list
1901 */
1902static COM_ExternalLock* COM_ExternalLockFind(
1903 IUnknown *pUnk)
1904{
1905 return COM_ExternalLockLocate(elList.head, pUnk);
1906}
1907
1908/****************************************************************************
1909 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1910 */
1911static COM_ExternalLock* COM_ExternalLockLocate(
1912 COM_ExternalLock *element,
1913 IUnknown *pUnk)
1914{
1915 if ( element == EL_END_OF_LIST )
1916 return EL_NOT_FOUND;
1917
1918 else if ( element->pUnk == pUnk ) /* We found it */
1919 return element;
1920
1921 else /* Not the right guy, keep on looking */
1922 return COM_ExternalLockLocate( element->next, pUnk);
1923}
1924
1925/****************************************************************************
1926 * Internal - Insert a new IUnknown* to the linked list
1927 */
1928static BOOL COM_ExternalLockInsert(
1929 IUnknown *pUnk)
1930{
1931 COM_ExternalLock *newLock = NULL;
1932 COM_ExternalLock *previousHead = NULL;
1933
1934 /*
1935 * Allocate space for the new storage object
1936 */
1937 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1938
1939 if (newLock!=NULL)
1940 {
1941 if ( elList.head == EL_END_OF_LIST )
1942 {
1943 elList.head = newLock; /* The list is empty */
1944 }
1945 else
1946 {
1947 /*
1948 * insert does it at the head
1949 */
1950 previousHead = elList.head;
1951 elList.head = newLock;
1952 }
1953
1954 /*
1955 * Set new list item data member
1956 */
1957 newLock->pUnk = pUnk;
1958 newLock->uRefCount = 1;
1959 newLock->next = previousHead;
1960
1961 return TRUE;
1962 }
1963 else
1964 return FALSE;
1965}
1966
1967/****************************************************************************
1968 * Internal - Method that removes an item from the linked list.
1969 */
1970static void COM_ExternalLockDelete(
1971 COM_ExternalLock *itemList)
1972{
1973 COM_ExternalLock *current = elList.head;
1974
1975 if ( current == itemList )
1976 {
1977 /*
1978 * this section handles the deletion of the first node
1979 */
1980 elList.head = itemList->next;
1981 HeapFree( GetProcessHeap(), 0, itemList);
1982 }
1983 else
1984 {
1985 do
1986 {
1987 if ( current->next == itemList ) /* We found the item to free */
1988 {
1989 current->next = itemList->next; /* readjust the list pointers */
1990
1991 HeapFree( GetProcessHeap(), 0, itemList);
1992 break;
1993 }
1994
1995 /* Skip to the next item */
1996 current = current->next;
1997
1998 } while ( current != EL_END_OF_LIST );
1999 }
2000}
2001
2002#ifndef __WIN32OS2__
2003/***********************************************************************
2004 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2005 *
2006 * Initialization code for the COMPOBJ DLL
2007 *
2008 * RETURNS:
2009 */
2010BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2011{
2012 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2013 res1, res2);
2014 switch(Reason)
2015 {
2016 case DLL_PROCESS_ATTACH:
2017 if (!COMPOBJ_Attach++) COMPOBJ_hInstance = hInst;
2018 break;
2019
2020 case DLL_PROCESS_DETACH:
2021 if(!--COMPOBJ_Attach)
2022 COMPOBJ_hInstance = 0;
2023 break;
2024 }
2025 return TRUE;
2026}
2027#endif
2028/******************************************************************************
2029 * OleGetAutoConvert [OLE32.104]
2030 */
2031HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2032{
2033 HKEY hkey = 0;
2034 char buf[200];
2035 WCHAR wbuf[200];
2036 DWORD len;
2037 HRESULT res = S_OK;
2038
2039 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2040 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2041 {
2042 res = REGDB_E_CLASSNOTREG;
2043 goto done;
2044 }
2045 len = 200;
2046 /* we can just query for the default value of AutoConvertTo key like that,
2047 without opening the AutoConvertTo key and querying for NULL (default) */
2048 if (RegQueryValueA(hkey,"AutoConvertTo",buf,&len))
2049 {
2050 res = REGDB_E_KEYMISSING;
2051 goto done;
2052 }
2053 MultiByteToWideChar( CP_ACP, 0, buf, -1, wbuf, sizeof(wbuf)/sizeof(WCHAR) );
2054 CLSIDFromString(wbuf,pClsidNew);
2055done:
2056 if (hkey) RegCloseKey(hkey);
2057
2058 return res;
2059}
2060
2061/******************************************************************************
2062 * OleSetAutoConvert [OLE32.126]
2063 */
2064HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2065{
2066 HKEY hkey = 0, hkeyConvert = 0;
2067 char buf[200], szClsidNew[200];
2068 HRESULT res = S_OK;
2069
2070 TRACE("(%p,%p);\n", clsidOld, clsidNew);
2071 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2072 WINE_StringFromCLSID(clsidNew, szClsidNew);
2073 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2074 {
2075 res = REGDB_E_CLASSNOTREG;
2076 goto done;
2077 }
2078 if (RegCreateKeyA(hkey, "AutoConvertTo", &hkeyConvert))
2079 {
2080 res = REGDB_E_WRITEREGDB;
2081 goto done;
2082 }
2083 if (RegSetValueExA(hkeyConvert, NULL, 0,
2084 REG_SZ, (LPBYTE)szClsidNew, strlen(szClsidNew)+1))
2085 {
2086 res = REGDB_E_WRITEREGDB;
2087 goto done;
2088 }
2089
2090done:
2091 if (hkeyConvert) RegCloseKey(hkeyConvert);
2092 if (hkey) RegCloseKey(hkey);
2093
2094 return res;
2095}
2096
2097/***********************************************************************
2098 * IsEqualGUID [OLE32.76]
2099 *
2100 * Compares two Unique Identifiers.
2101 *
2102 * RETURNS
2103 * TRUE if equal
2104 */
2105#undef IsEqualGUID
2106BOOL WINAPI IsEqualGUID(
2107 REFGUID rguid1, /* [in] unique id 1 */
2108 REFGUID rguid2 /* [in] unique id 2 */
2109 )
2110{
2111 return !memcmp(rguid1,rguid2,sizeof(GUID));
2112}
2113
2114#ifdef __WIN32OS2__
2115// ----------------------------------------------------------------------
2116// CLSIDFromStringA()
2117// @@@PH: this is not a WINE API, but a replacement for CLSIDFromString16
2118// which used to accept ASCII strings instead of OLE strings
2119// ----------------------------------------------------------------------
2120
2121HRESULT WIN32API CLSIDFromStringA(
2122 LPCSTR lpsz, // [in] - ASCII string CLSID
2123 LPCLSID pclsid) // [out] - Binary CLSID
2124{
2125 return CLSIDFromString16(lpsz, pclsid);
2126}
2127#endif
Note: See TracBrowser for help on using the repository browser.