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

Last change on this file since 10367 was 9400, checked in by sandervl, 23 years ago

Wine resync

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