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
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.
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.
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
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
43 #include "wine/unicode.h"
45 #include "ole32_main.h"
46 #include "compobj_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 /****************************************************************************
53 * This section defines variables internal to the COM module.
55 * TODO: Most of these things will have to be made thread-safe.
57 HINSTANCE COMPOBJ_hInstance32 = 0;
59 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid, DWORD dwClsContext, LPUNKNOWN* ppUnk);
60 static void COM_RevokeAllClasses();
61 static void COM_ExternalLockFreeList();
63 const CLSID CLSID_StdGlobalInterfaceTable = { 0x00000323, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
68 static CRITICAL_SECTION csApartment = CRITICAL_SECTION_INIT("csApartment");
71 * This lock count counts the number of times CoInitialize is called. It is
72 * decreased every time CoUninitialize is called. When it hits 0, the COM
75 static LONG s_COMLockCount = 0;
78 * This linked list contains the list of registered class objects. These
79 * are mostly used to register the factories for out-of-proc servers of OLE
82 * TODO: Make this data structure aware of inter-process communication. This
83 * means that parts of this will be exported to the Wine Server.
85 typedef struct tagRegisteredClass
87 CLSID classIdentifier;
88 LPUNKNOWN classObject;
92 HANDLE hThread; /* only for localserver */
93 struct tagRegisteredClass* nextClass;
96 static CRITICAL_SECTION csRegisteredClassList = CRITICAL_SECTION_INIT("csRegisteredClassList");
97 static RegisteredClass* firstRegisteredClass = NULL;
99 /*****************************************************************************
100 * This section contains OpenDllList definitions
102 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
103 * other functions what do LoadLibrary _without_ giving back a HMODULE.
104 * Without this list these handles would be freed never.
106 * FIXME: a DLL what says OK whenn asked for unloading is unloaded in the
107 * next unload-call but not before 600 sec.
110 typedef struct tagOpenDll {
112 struct tagOpenDll *next;
115 static CRITICAL_SECTION csOpenDllList = CRITICAL_SECTION_INIT("csOpenDllList");
116 static OpenDll *openDllList = NULL; /* linked list of open dlls */
118 static const char aptWinClass[] = "WINE_OLE32_APT_CLASS";
119 static LRESULT CALLBACK COM_AptWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
121 static void COMPOBJ_DLLList_Add(HANDLE hLibrary);
122 static void COMPOBJ_DllList_FreeUnused(int Timeout);
125 /******************************************************************************
126 * Initialize/Unitialize threading stuff.
128 void COMPOBJ_InitProcess( void )
132 memset(&wclass, 0, sizeof(wclass));
133 wclass.lpfnWndProc = &COM_AptWndProc;
134 wclass.hInstance = OLE32_hInstance;
135 wclass.lpszClassName = aptWinClass;
136 RegisterClassA(&wclass);
139 void COMPOBJ_UninitProcess( void )
141 UnregisterClassA(aptWinClass, OLE32_hInstance);
144 /******************************************************************************
147 static void COM_InitMTA(void)
149 /* FIXME: how does windoze create OXIDs?
150 * this method will only work for local RPC */
151 MTA.oxid = ((OXID)GetCurrentProcessId() << 32);
152 InitializeCriticalSection(&MTA.cs);
155 static void COM_UninitMTA(void)
157 DeleteCriticalSection(&MTA.cs);
161 static APARTMENT* COM_CreateApartment(DWORD model)
165 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(APARTMENT));
167 apt->tid = GetCurrentThreadId();
168 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
169 GetCurrentProcess(), &apt->thread,
170 THREAD_ALL_ACCESS, FALSE, 0);
171 if (model & COINIT_APARTMENTTHREADED) {
172 /* FIXME: how does windoze create OXIDs? */
173 apt->oxid = MTA.oxid | GetCurrentThreadId();
174 apt->win = CreateWindowA(aptWinClass, NULL, 0,
176 0, 0, OLE32_hInstance, NULL);
177 InitializeCriticalSection(&apt->cs);
181 apt->oxid = MTA.oxid;
183 EnterCriticalSection(&csApartment);
184 if (apts) apts->prev = apt;
187 LeaveCriticalSection(&csApartment);
188 NtCurrentTeb()->ErrorInfo = apt;
192 static void COM_DestroyApartment(APARTMENT *apt)
194 EnterCriticalSection(&csApartment);
195 if (apt->prev) apt->prev->next = apt->next;
196 if (apt->next) apt->next->prev = apt->prev;
197 if (apts == apt) apts = apt->next;
198 apt->prev = NULL; apt->next = NULL;
199 LeaveCriticalSection(&csApartment);
200 if (apt->model & COINIT_APARTMENTTHREADED) {
201 if (apt->win) DestroyWindow(apt->win);
202 DeleteCriticalSection(&apt->cs);
204 CloseHandle(apt->thread);
205 HeapFree(GetProcessHeap(), 0, apt);
208 HWND COM_GetApartmentWin(OXID oxid)
213 EnterCriticalSection(&csApartment);
215 while (apt && apt->oxid != oxid) apt = apt->next;
216 if (apt) win = apt->win;
217 LeaveCriticalSection(&csApartment);
221 static LRESULT CALLBACK COM_AptWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
223 return DefWindowProcA(hWnd, msg, wParam, lParam);
226 /*****************************************************************************
227 * This section contains OpenDllList implemantation
230 static void COMPOBJ_DLLList_Add(HANDLE hLibrary)
237 EnterCriticalSection( &csOpenDllList );
239 if (openDllList == NULL) {
240 /* empty list -- add first node */
241 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
242 openDllList->hLibrary=hLibrary;
243 openDllList->next = NULL;
245 /* search for this dll */
247 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
248 if (ptr->hLibrary == hLibrary) {
254 /* dll not found, add it */
256 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
257 openDllList->hLibrary = hLibrary;
258 openDllList->next = tmp;
262 LeaveCriticalSection( &csOpenDllList );
265 static void COMPOBJ_DllList_FreeUnused(int Timeout)
267 OpenDll *curr, *next, *prev = NULL;
268 typedef HRESULT(*DllCanUnloadNowFunc)(void);
269 DllCanUnloadNowFunc DllCanUnloadNow;
273 EnterCriticalSection( &csOpenDllList );
275 for (curr = openDllList; curr != NULL; ) {
276 DllCanUnloadNow = (DllCanUnloadNowFunc) GetProcAddress(curr->hLibrary, "DllCanUnloadNow");
278 if ( (DllCanUnloadNow != NULL) && (DllCanUnloadNow() == S_OK) ) {
281 TRACE("freeing %p\n", curr->hLibrary);
282 FreeLibrary(curr->hLibrary);
284 HeapFree(GetProcessHeap(), 0, curr);
285 if (curr == openDllList) {
298 LeaveCriticalSection( &csOpenDllList );
301 /******************************************************************************
302 * CoBuildVersion [COMPOBJ.1]
303 * CoBuildVersion [OLE32.4]
306 * Current build version, hiword is majornumber, loword is minornumber
308 DWORD WINAPI CoBuildVersion(void)
310 TRACE("Returning version %d, build %d.\n", rmm, rup);
311 return (rmm<<16)+rup;
314 /******************************************************************************
315 * CoInitialize [OLE32.26]
317 * Initializes the COM libraries.
321 HRESULT WINAPI CoInitialize(
322 LPVOID lpReserved /* [in] pointer to win32 malloc interface
323 (obsolete, should be NULL) */
327 * Just delegate to the newer method.
329 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
332 /******************************************************************************
333 * CoInitializeEx [OLE32.163]
335 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
336 * used for memory management is obsolete.
339 * S_OK if successful,
340 * S_FALSE if this function was called already.
341 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
344 HRESULT WINAPI CoInitializeEx(
345 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
346 (obsolete, should be NULL) */
347 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
353 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
355 if (lpReserved!=NULL)
357 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
360 apt = NtCurrentTeb()->ErrorInfo;
361 if (apt && dwCoInit != apt->model) return RPC_E_CHANGED_MODE;
362 hr = apt ? S_FALSE : S_OK;
365 * Check the lock count. If this is the first time going through the initialize
366 * process, we have to initialize the libraries.
368 * And crank-up that lock count.
370 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
373 * Initialize the various COM libraries and data structures.
375 TRACE("() - Initializing the COM libraries\n");
379 RunningObjectTableImpl_Initialize();
382 if (!apt) apt = COM_CreateApartment(dwCoInit);
384 InterlockedIncrement(&apt->inits);
385 if (hr == S_OK) NtCurrentTeb()->ErrorInfo = apt;
390 /***********************************************************************
391 * CoUninitialize [OLE32.47]
393 * This method will release the COM libraries.
395 * See the windows documentation for more details.
397 void WINAPI CoUninitialize(void)
404 apt = NtCurrentTeb()->ErrorInfo;
406 if (InterlockedDecrement(&apt->inits)==0) {
407 NtCurrentTeb()->ErrorInfo = NULL;
408 COM_DestroyApartment(apt);
413 * Decrease the reference count.
414 * If we are back to 0 locks on the COM library, make sure we free
415 * all the associated data structures.
417 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
421 * Release the various COM libraries and data structures.
423 TRACE("() - Releasing the COM libraries\n");
425 RunningObjectTableImpl_UnInitialize();
427 * Release the references to the registered class objects.
429 COM_RevokeAllClasses();
432 * This will free the loaded COM Dlls.
434 CoFreeAllLibraries();
437 * This will free list of external references to COM objects.
439 COM_ExternalLockFreeList();
443 else if (lCOMRefCnt<1) {
444 ERR( "CoUninitialize() - not CoInitialized.\n" );
445 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
449 /******************************************************************************
450 * CoDisconnectObject [COMPOBJ.15]
451 * CoDisconnectObject [OLE32.8]
453 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
455 TRACE("(%p, %lx)\n",lpUnk,reserved);
459 /******************************************************************************
460 * CoCreateGuid[OLE32.6]
463 HRESULT WINAPI CoCreateGuid(
464 GUID *pguid /* [out] points to the GUID to initialize */
466 return UuidCreate(pguid);
469 /******************************************************************************
470 * CLSIDFromString [OLE32.3]
471 * IIDFromString [OLE32.74]
472 * Converts a unique identifier from its string representation into
476 * If idstr is not a valid CLSID string then it gets treated as a ProgID
481 HRESULT WINAPI __CLSIDFromStringA(
482 LPCSTR idstr, /* [in] string representation of guid */
483 CLSID *id) /* [out] GUID converted from string */
485 BYTE *s = (BYTE *) idstr;
490 s = "{00000000-0000-0000-0000-000000000000}";
491 else { /* validate the CLSID string */
494 return CO_E_CLASSSTRING;
496 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
497 return CO_E_CLASSSTRING;
499 for (i=1; i<37; i++) {
500 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
501 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
502 ((s[i] >= 'a') && (s[i] <= 'f')) ||
503 ((s[i] >= 'A') && (s[i] <= 'F'))))
504 return CO_E_CLASSSTRING;
508 TRACE("%s -> %p\n", s, id);
510 /* quick lookup table */
511 memset(table, 0, 256);
513 for (i = 0; i < 10; i++) {
516 for (i = 0; i < 6; i++) {
517 table['A' + i] = i+10;
518 table['a' + i] = i+10;
521 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
523 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
524 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
525 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
526 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
528 /* these are just sequential bytes */
529 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
530 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
531 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
532 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
533 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
534 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
535 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
536 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
541 /*****************************************************************************/
543 HRESULT WINAPI CLSIDFromString(
544 LPCOLESTR idstr, /* [in] string representation of GUID */
545 CLSID *id ) /* [out] GUID represented by above string */
550 if (!WideCharToMultiByte( CP_ACP, 0, idstr, -1, xid, sizeof(xid), NULL, NULL ))
551 return CO_E_CLASSSTRING;
554 ret = __CLSIDFromStringA(xid,id);
555 if(ret != S_OK) { /* It appears a ProgID is also valid */
556 ret = CLSIDFromProgID(idstr, id);
561 /******************************************************************************
562 * WINE_StringFromCLSID [Internal]
563 * Converts a GUID into the respective string representation.
568 * the string representation and HRESULT
570 HRESULT WINE_StringFromCLSID(
571 const CLSID *id, /* [in] GUID to be converted */
572 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
574 static const char *hex = "0123456789ABCDEF";
579 { ERR("called with id=Null\n");
584 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
585 id->Data1, id->Data2, id->Data3,
586 id->Data4[0], id->Data4[1]);
590 for (i = 2; i < 8; i++) {
591 *s++ = hex[id->Data4[i]>>4];
592 *s++ = hex[id->Data4[i] & 0xf];
598 TRACE("%p->%s\n", id, idstr);
604 /******************************************************************************
605 * StringFromCLSID [OLE32.151]
606 * StringFromIID [OLE32.153]
607 * Converts a GUID into the respective string representation.
608 * The target string is allocated using the OLE IMalloc.
610 * the string representation and HRESULT
612 HRESULT WINAPI StringFromCLSID(
613 REFCLSID id, /* [in] the GUID to be converted */
614 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
620 if ((ret=CoGetMalloc(0,&mllc)))
623 ret=WINE_StringFromCLSID(id,buf);
625 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
626 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
627 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
632 /******************************************************************************
633 * StringFromGUID2 [COMPOBJ.76]
634 * StringFromGUID2 [OLE32.152]
636 * Converts a global unique identifier into a string of an API-
637 * specified fixed format. (The usual {.....} stuff.)
640 * The (UNICODE) string representation of the GUID in 'str'
641 * The length of the resulting string, 0 if there was any problem.
644 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
648 if (WINE_StringFromCLSID(id,xguid))
650 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
653 /******************************************************************************
654 * ProgIDFromCLSID [OLE32.133]
655 * Converts a class id into the respective Program ID. (By using a registry lookup)
656 * RETURNS S_OK on success
657 * riid associated with the progid
660 HRESULT WINAPI ProgIDFromCLSID(
661 REFCLSID clsid, /* [in] class id as found in registry */
662 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
665 char strCLSID[50], *buf, *buf2;
671 WINE_StringFromCLSID(clsid, strCLSID);
673 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
674 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
675 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
676 ret = REGDB_E_CLASSNOTREG;
678 HeapFree(GetProcessHeap(), 0, buf);
682 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
684 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
685 ret = REGDB_E_CLASSNOTREG;
689 if (CoGetMalloc(0,&mllc))
693 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf2, -1, NULL, 0 );
694 *lplpszProgID = IMalloc_Alloc(mllc, len * sizeof(WCHAR) );
695 MultiByteToWideChar( CP_ACP, 0, buf2, -1, *lplpszProgID, len );
698 HeapFree(GetProcessHeap(), 0, buf2);
705 /******************************************************************************
706 * CLSIDFromProgID [OLE32.2]
707 * Converts a program id into the respective GUID. (By using a registry lookup)
709 * riid associated with the progid
711 HRESULT WINAPI CLSIDFromProgID(
712 LPCOLESTR progid, /* [in] program id as found in registry */
713 LPCLSID riid ) /* [out] associated CLSID */
715 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
717 DWORD buf2len = sizeof(buf2);
720 WCHAR *buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
721 strcpyW( buf, progid );
722 strcatW( buf, clsidW );
723 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
725 HeapFree(GetProcessHeap(),0,buf);
726 return CO_E_CLASSSTRING;
728 HeapFree(GetProcessHeap(),0,buf);
730 if (RegQueryValueA(xhkey,NULL,buf2,&buf2len))
733 return CO_E_CLASSSTRING;
736 return __CLSIDFromStringA(buf2,riid);
741 /*****************************************************************************
742 * CoGetPSClsid [OLE32.22]
744 * This function returns the CLSID of the DLL that implements the proxy and stub
745 * for the specified interface.
747 * It determines this by searching the
748 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32 in the registry
749 * and any interface id registered by CoRegisterPSClsid within the current process.
751 * FIXME: We only search the registry, not ids registered with CoRegisterPSClsid.
753 HRESULT WINAPI CoGetPSClsid(
754 REFIID riid, /* [in] Interface whose proxy/stub CLSID is to be returned */
755 CLSID *pclsid ) /* [out] Where to store returned proxy/stub CLSID */
761 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
763 /* Get the input iid as a string */
764 WINE_StringFromCLSID(riid, buf2);
765 /* Allocate memory for the registry key we will construct.
766 (length of iid string plus constant length of static text */
767 buf = HeapAlloc(GetProcessHeap(), 0, strlen(buf2)+27);
770 return (E_OUTOFMEMORY);
773 /* Construct the registry key we want */
774 sprintf(buf,"Interface\\%s\\ProxyStubClsid32", buf2);
777 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
779 HeapFree(GetProcessHeap(),0,buf);
780 return (E_INVALIDARG);
782 HeapFree(GetProcessHeap(),0,buf);
784 /* ... Once we have the key, query the registry to get the
785 value of CLSID as a string, and convert it into a
786 proper CLSID structure to be passed back to the app */
787 buf2len = sizeof(buf2);
788 if ( (RegQueryValueA(xhkey,NULL,buf2,&buf2len)) )
795 /* We have the CLSid we want back from the registry as a string, so
796 lets convert it into a CLSID structure */
797 if ( (__CLSIDFromStringA(buf2,pclsid)) != NOERROR) {
801 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
807 /***********************************************************************
808 * WriteClassStm (OLE32.159)
810 * This function write a CLSID on stream
812 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
814 TRACE("(%p,%p)\n",pStm,rclsid);
819 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
822 /***********************************************************************
823 * ReadClassStm (OLE32.135)
825 * This function read a CLSID from a stream
827 HRESULT WINAPI ReadClassStm(IStream *pStm,CLSID *pclsid)
832 TRACE("(%p,%p)\n",pStm,pclsid);
837 res = IStream_Read(pStm,(void*)pclsid,sizeof(CLSID),&nbByte);
842 if (nbByte != sizeof(CLSID))
850 * COM_GetRegisteredClassObject
852 * This internal method is used to scan the registered class list to
853 * find a class object.
856 * rclsid Class ID of the class to find.
857 * dwClsContext Class context to match.
858 * ppv [out] returns a pointer to the class object. Complying
859 * to normal COM usage, this method will increase the
860 * reference count on this object.
862 static HRESULT COM_GetRegisteredClassObject(
867 HRESULT hr = S_FALSE;
868 RegisteredClass* curClass;
870 EnterCriticalSection( &csRegisteredClassList );
878 * Iterate through the whole list and try to match the class ID.
880 curClass = firstRegisteredClass;
882 while (curClass != 0)
885 * Check if we have a match on the class ID.
887 if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
890 * Since we don't do out-of process or DCOM just right away, let's ignore the
895 * We have a match, return the pointer to the class object.
897 *ppUnk = curClass->classObject;
899 IUnknown_AddRef(curClass->classObject);
906 * Step to the next class in the list.
908 curClass = curClass->nextClass;
912 LeaveCriticalSection( &csRegisteredClassList );
914 * If we get to here, we haven't found our class.
920 _LocalServerThread(LPVOID param) {
923 RegisteredClass *newClass = (RegisteredClass*)param;
927 unsigned char *buffer;
929 IClassFactory *classfac;
930 LARGE_INTEGER seekto;
931 ULARGE_INTEGER newpos;
934 TRACE("Starting threader for %s.\n",debugstr_guid(&newClass->classIdentifier));
935 strcpy(pipefn,PIPEPREF);
936 WINE_StringFromCLSID(&newClass->classIdentifier,pipefn+strlen(PIPEPREF));
938 hres = IUnknown_QueryInterface(newClass->classObject,&IID_IClassFactory,(LPVOID*)&classfac);
939 if (hres) return hres;
941 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
943 FIXME("Failed to create stream on hglobal.\n");
946 hres = CoMarshalInterface(pStm,&IID_IClassFactory,(LPVOID)classfac,0,NULL,0);
948 FIXME("CoMarshalInterface failed, %lx!\n",hres);
951 hres = IStream_Stat(pStm,&ststg,0);
952 if (hres) return hres;
954 buflen = ststg.cbSize.s.LowPart;
955 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
956 seekto.s.LowPart = 0;
957 seekto.s.HighPart = 0;
958 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
960 FIXME("IStream_Seek failed, %lx\n",hres);
963 hres = IStream_Read(pStm,buffer,buflen,&res);
965 FIXME("Stream Read failed, %lx\n",hres);
968 IStream_Release(pStm);
971 hPipe = CreateNamedPipeA(
974 PIPE_TYPE_BYTE|PIPE_WAIT,
975 PIPE_UNLIMITED_INSTANCES,
978 NMPWAIT_USE_DEFAULT_WAIT,
981 if (hPipe == INVALID_HANDLE_VALUE) {
982 FIXME("pipe creation failed for %s, le is %lx\n",pipefn,GetLastError());
985 if (!ConnectNamedPipe(hPipe,NULL)) {
986 ERR("Failure during ConnectNamedPipe %lx, ABORT!\n",GetLastError());
990 WriteFile(hPipe,buffer,buflen,&res,NULL);
996 /******************************************************************************
997 * CoRegisterClassObject [OLE32.36]
999 * This method will register the class object for a given class ID.
1001 * See the Windows documentation for more details.
1003 HRESULT WINAPI CoRegisterClassObject(
1006 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1007 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1008 LPDWORD lpdwRegister
1011 RegisteredClass* newClass;
1012 LPUNKNOWN foundObject;
1015 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1016 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1018 if ( (lpdwRegister==0) || (pUnk==0) )
1019 return E_INVALIDARG;
1024 * First, check if the class is already registered.
1025 * If it is, this should cause an error.
1027 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1029 IUnknown_Release(foundObject);
1030 return CO_E_OBJISREG;
1033 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1034 if ( newClass == NULL )
1035 return E_OUTOFMEMORY;
1037 EnterCriticalSection( &csRegisteredClassList );
1039 newClass->classIdentifier = *rclsid;
1040 newClass->runContext = dwClsContext;
1041 newClass->connectFlags = flags;
1043 * Use the address of the chain node as the cookie since we are sure it's
1046 newClass->dwCookie = (DWORD)newClass;
1047 newClass->nextClass = firstRegisteredClass;
1050 * Since we're making a copy of the object pointer, we have to increase its
1053 newClass->classObject = pUnk;
1054 IUnknown_AddRef(newClass->classObject);
1056 firstRegisteredClass = newClass;
1057 LeaveCriticalSection( &csRegisteredClassList );
1059 *lpdwRegister = newClass->dwCookie;
1061 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
1065 newClass->hThread=CreateThread(NULL,0,_LocalServerThread,newClass,0,&tid);
1070 /***********************************************************************
1071 * CoRevokeClassObject [OLE32.40]
1073 * This method will remove a class object from the class registry
1075 * See the Windows documentation for more details.
1077 HRESULT WINAPI CoRevokeClassObject(
1080 HRESULT hr = E_INVALIDARG;
1081 RegisteredClass** prevClassLink;
1082 RegisteredClass* curClass;
1084 TRACE("(%08lx)\n",dwRegister);
1086 EnterCriticalSection( &csRegisteredClassList );
1089 * Iterate through the whole list and try to match the cookie.
1091 curClass = firstRegisteredClass;
1092 prevClassLink = &firstRegisteredClass;
1094 while (curClass != 0)
1097 * Check if we have a match on the cookie.
1099 if (curClass->dwCookie == dwRegister)
1102 * Remove the class from the chain.
1104 *prevClassLink = curClass->nextClass;
1107 * Release the reference to the class object.
1109 IUnknown_Release(curClass->classObject);
1112 * Free the memory used by the chain node.
1114 HeapFree(GetProcessHeap(), 0, curClass);
1121 * Step to the next class in the list.
1123 prevClassLink = &(curClass->nextClass);
1124 curClass = curClass->nextClass;
1128 LeaveCriticalSection( &csRegisteredClassList );
1130 * If we get to here, we haven't found our class.
1135 /***********************************************************************
1136 * compobj_RegReadPath [internal]
1138 * Reads a registry value and expands it when nessesary
1140 HRESULT compobj_RegReadPath(char * keyname, char * valuename, char * dst, int dstlen)
1146 DWORD dwLength = dstlen;
1148 if((hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
1149 if( (hres = RegQueryValueExA(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
1150 if (keytype == REG_EXPAND_SZ) {
1151 if (dstlen <= ExpandEnvironmentStringsA(src, dst, dstlen)) hres = ERROR_MORE_DATA;
1153 strncpy(dst, src, dstlen);
1161 /***********************************************************************
1162 * CoGetClassObject [COMPOBJ.7]
1163 * CoGetClassObject [OLE32.16]
1165 * FIXME. If request allows of several options and there is a failure
1166 * with one (other than not being registered) do we try the
1167 * others or return failure? (E.g. inprocess is registered but
1168 * the DLL is not found but the server version works)
1170 HRESULT WINAPI CoGetClassObject(
1171 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1172 REFIID iid, LPVOID *ppv
1174 LPUNKNOWN regClassObject;
1175 HRESULT hres = E_UNEXPECTED;
1178 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
1179 DllGetClassObjectFunc DllGetClassObject;
1181 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1183 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1186 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1187 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1191 * First, try and see if we can't match the class ID with one of the
1192 * registered classes.
1194 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
1197 * Get the required interface from the retrieved pointer.
1199 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1202 * Since QI got another reference on the pointer, we want to release the
1203 * one we already have. If QI was unsuccessful, this will release the object. This
1204 * is good since we are not returning it in the "out" parameter.
1206 IUnknown_Release(regClassObject);
1211 /* first try: in-process */
1212 if ((CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER) & dwClsContext) {
1213 char keyname[MAX_PATH];
1214 char dllpath[MAX_PATH+1];
1216 sprintf(keyname,"CLSID\\%s\\InprocServer32",xclsid);
1218 if ( compobj_RegReadPath(keyname, NULL, dllpath, sizeof(dllpath)) != ERROR_SUCCESS) {
1219 /* failure: CLSID is not found in registry */
1220 WARN("class %s not registred\n", xclsid);
1221 hres = REGDB_E_CLASSNOTREG;
1223 if ((hLibrary = LoadLibraryExA(dllpath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
1224 /* failure: DLL could not be loaded */
1225 ERR("couldn't load InprocServer32 dll %s\n", dllpath);
1226 hres = E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
1227 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
1228 /* failure: the dll did not export DllGetClassObject */
1229 ERR("couldn't find function DllGetClassObject in %s\n", dllpath);
1230 FreeLibrary( hLibrary );
1231 hres = CO_E_DLLNOTFOUND;
1233 /* OK: get the ClassObject */
1234 COMPOBJ_DLLList_Add( hLibrary );
1235 return DllGetClassObject(rclsid, iid, ppv);
1240 /* Next try out of process */
1241 if (CLSCTX_LOCAL_SERVER & dwClsContext)
1243 return create_marshalled_proxy(rclsid,iid,ppv);
1246 /* Finally try remote */
1247 if (CLSCTX_REMOTE_SERVER & dwClsContext)
1249 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
1250 hres = E_NOINTERFACE;
1255 /***********************************************************************
1256 * CoResumeClassObjects (OLE32.173)
1258 * Resumes classobjects registered with REGCLS suspended
1260 HRESULT WINAPI CoResumeClassObjects(void)
1266 /***********************************************************************
1267 * GetClassFile (OLE32.67)
1269 * This function supplies the CLSID associated with the given filename.
1271 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1275 int nbElm, length, i;
1277 LPOLESTR *pathDec=0,absFile=0,progId=0;
1279 static const WCHAR bkslashW[] = {'\\',0};
1280 static const WCHAR dotW[] = {'.',0};
1282 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1284 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1285 if((StgIsStorageFile(filePathName))==S_OK){
1287 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1290 res=ReadClassStg(pstg,pclsid);
1292 IStorage_Release(pstg);
1296 /* if the file is not a storage object then attemps to match various bits in the file against a
1297 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1300 for(i=0;i<nFileTypes;i++)
1302 for(i=0;j<nPatternsForType;j++){
1307 pat=ReadPatternFromRegistry(i,j);
1308 hFile=CreateFileW(filePathName,,,,,,hFile);
1309 SetFilePosition(hFile,pat.offset);
1310 ReadFile(hFile,buf,pat.size,NULL,NULL);
1311 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1313 *pclsid=ReadCLSIDFromRegistry(i);
1319 /* if the obove strategies fail then search for the extension key in the registry */
1321 /* get the last element (absolute file) in the path name */
1322 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1323 absFile=pathDec[nbElm-1];
1325 /* failed if the path represente a directory and not an absolute file name*/
1326 if (!lstrcmpW(absFile, bkslashW))
1327 return MK_E_INVALIDEXTENSION;
1329 /* get the extension of the file */
1331 length=lstrlenW(absFile);
1332 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1335 if (!extension || !lstrcmpW(extension, dotW))
1336 return MK_E_INVALIDEXTENSION;
1338 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1340 /* get the progId associated to the extension */
1341 progId = CoTaskMemAlloc(sizeProgId);
1342 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1344 if (res==ERROR_SUCCESS)
1345 /* return the clsid associated to the progId */
1346 res= CLSIDFromProgID(progId,pclsid);
1348 for(i=0; pathDec[i]!=NULL;i++)
1349 CoTaskMemFree(pathDec[i]);
1350 CoTaskMemFree(pathDec);
1352 CoTaskMemFree(progId);
1354 if (res==ERROR_SUCCESS)
1357 return MK_E_INVALIDEXTENSION;
1359 /***********************************************************************
1360 * CoCreateInstance [COMPOBJ.13]
1361 * CoCreateInstance [OLE32.7]
1363 HRESULT WINAPI CoCreateInstance(
1365 LPUNKNOWN pUnkOuter,
1371 LPCLASSFACTORY lpclf = 0;
1380 * Initialize the "out" parameter
1385 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
1386 * Rather than create a class factory, we can just check for it here
1388 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
1389 if (StdGlobalInterfaceTableInstance == NULL)
1390 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
1391 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
1392 if (hres) return hres;
1394 TRACE("Retrieved GIT (%p)\n", *ppv);
1399 * Get a class factory to construct the object we want.
1401 hres = CoGetClassObject(rclsid,
1408 FIXME("no classfactory created for CLSID %s, hres is 0x%08lx\n",
1409 debugstr_guid(rclsid),hres);
1414 * Create the object and don't forget to release the factory
1416 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1417 IClassFactory_Release(lpclf);
1419 FIXME("no instance created for interface %s of class %s, hres is 0x%08lx\n",
1420 debugstr_guid(iid), debugstr_guid(rclsid),hres);
1425 /***********************************************************************
1426 * CoCreateInstanceEx [OLE32.165]
1428 HRESULT WINAPI CoCreateInstanceEx(
1430 LPUNKNOWN pUnkOuter,
1432 COSERVERINFO* pServerInfo,
1436 IUnknown* pUnk = NULL;
1439 int successCount = 0;
1444 if ( (cmq==0) || (pResults==NULL))
1445 return E_INVALIDARG;
1447 if (pServerInfo!=NULL)
1448 FIXME("() non-NULL pServerInfo not supported!\n");
1451 * Initialize all the "out" parameters.
1453 for (index = 0; index < cmq; index++)
1455 pResults[index].pItf = NULL;
1456 pResults[index].hr = E_NOINTERFACE;
1460 * Get the object and get its IUnknown pointer.
1462 hr = CoCreateInstance(rclsid,
1472 * Then, query for all the interfaces requested.
1474 for (index = 0; index < cmq; index++)
1476 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1477 pResults[index].pIID,
1478 (VOID**)&(pResults[index].pItf));
1480 if (pResults[index].hr == S_OK)
1485 * Release our temporary unknown pointer.
1487 IUnknown_Release(pUnk);
1489 if (successCount == 0)
1490 return E_NOINTERFACE;
1492 if (successCount!=cmq)
1493 return CO_S_NOTALLINTERFACES;
1498 /***********************************************************************
1499 * CoLoadLibrary (OLE32.30)
1501 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1503 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1505 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1508 /***********************************************************************
1509 * CoFreeLibrary [OLE32.13]
1511 * NOTES: don't belive the docu
1513 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1515 FreeLibrary(hLibrary);
1519 /***********************************************************************
1520 * CoFreeAllLibraries [OLE32.12]
1522 * NOTES: don't belive the docu
1524 void WINAPI CoFreeAllLibraries(void)
1530 /***********************************************************************
1531 * CoFreeUnusedLibraries [COMPOBJ.17]
1532 * CoFreeUnusedLibraries [OLE32.14]
1534 * FIXME: Calls to CoFreeUnusedLibraries from any thread always route
1535 * through the main apartment's thread to call DllCanUnloadNow
1537 void WINAPI CoFreeUnusedLibraries(void)
1539 COMPOBJ_DllList_FreeUnused(0);
1542 /***********************************************************************
1543 * CoFileTimeNow [COMPOBJ.82]
1544 * CoFileTimeNow [OLE32.10]
1547 * the current system time in lpFileTime
1549 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime ) /* [out] the current time */
1551 GetSystemTimeAsFileTime( lpFileTime );
1555 /***********************************************************************
1556 * CoLoadLibrary (OLE32.30)
1558 static void COM_RevokeAllClasses()
1560 EnterCriticalSection( &csRegisteredClassList );
1562 while (firstRegisteredClass!=0)
1564 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1567 LeaveCriticalSection( &csRegisteredClassList );
1570 /****************************************************************************
1571 * COM External Lock methods implementation
1573 * This api provides a linked list to managed external references to
1576 * The public interface consists of three calls:
1577 * COM_ExternalLockAddRef
1578 * COM_ExternalLockRelease
1579 * COM_ExternalLockFreeList
1582 #define EL_END_OF_LIST 0
1583 #define EL_NOT_FOUND 0
1586 * Declaration of the static structure that manage the
1587 * external lock to COM objects.
1589 typedef struct COM_ExternalLock COM_ExternalLock;
1590 typedef struct COM_ExternalLockList COM_ExternalLockList;
1592 struct COM_ExternalLock
1594 IUnknown *pUnk; /* IUnknown referenced */
1595 ULONG uRefCount; /* external lock counter to IUnknown object*/
1596 COM_ExternalLock *next; /* Pointer to next element in list */
1599 struct COM_ExternalLockList
1601 COM_ExternalLock *head; /* head of list */
1605 * Declaration and initialization of the static structure that manages
1606 * the external lock to COM objects.
1608 static COM_ExternalLockList elList = { EL_END_OF_LIST };
1611 * Private methods used to managed the linked list
1615 static COM_ExternalLock* COM_ExternalLockLocate(
1616 COM_ExternalLock *element,
1619 /****************************************************************************
1620 * Internal - Insert a new IUnknown* to the linked list
1622 static BOOL COM_ExternalLockInsert(
1625 COM_ExternalLock *newLock = NULL;
1626 COM_ExternalLock *previousHead = NULL;
1629 * Allocate space for the new storage object
1631 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1633 if (newLock!=NULL) {
1634 if ( elList.head == EL_END_OF_LIST ) {
1635 elList.head = newLock; /* The list is empty */
1637 /* insert does it at the head */
1638 previousHead = elList.head;
1639 elList.head = newLock;
1642 /* Set new list item data member */
1643 newLock->pUnk = pUnk;
1644 newLock->uRefCount = 1;
1645 newLock->next = previousHead;
1652 /****************************************************************************
1653 * Internal - Method that removes an item from the linked list.
1655 static void COM_ExternalLockDelete(
1656 COM_ExternalLock *itemList)
1658 COM_ExternalLock *current = elList.head;
1660 if ( current == itemList ) {
1661 /* this section handles the deletion of the first node */
1662 elList.head = itemList->next;
1663 HeapFree( GetProcessHeap(), 0, itemList);
1666 if ( current->next == itemList ){ /* We found the item to free */
1667 current->next = itemList->next; /* readjust the list pointers */
1668 HeapFree( GetProcessHeap(), 0, itemList);
1672 /* Skip to the next item */
1673 current = current->next;
1675 } while ( current != EL_END_OF_LIST );
1679 /****************************************************************************
1680 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1682 * NOTES: how long can the list be ?? (recursive!!!)
1684 static COM_ExternalLock* COM_ExternalLockLocate( COM_ExternalLock *element, IUnknown *pUnk)
1686 if ( element == EL_END_OF_LIST )
1687 return EL_NOT_FOUND;
1688 else if ( element->pUnk == pUnk ) /* We found it */
1690 else /* Not the right guy, keep on looking */
1691 return COM_ExternalLockLocate( element->next, pUnk);
1694 /****************************************************************************
1695 * Public - Method that increments the count for a IUnknown* in the linked
1696 * list. The item is inserted if not already in the list.
1698 static void COM_ExternalLockAddRef(IUnknown *pUnk)
1700 COM_ExternalLock *externalLock = COM_ExternalLockLocate(elList.head, pUnk);
1703 * Add an external lock to the object. If it was already externally
1704 * locked, just increase the reference count. If it was not.
1705 * add the item to the list.
1707 if ( externalLock == EL_NOT_FOUND )
1708 COM_ExternalLockInsert(pUnk);
1710 externalLock->uRefCount++;
1713 * Add an internal lock to the object
1715 IUnknown_AddRef(pUnk);
1718 /****************************************************************************
1719 * Public - Method that decrements the count for a IUnknown* in the linked
1720 * list. The item is removed from the list if its count end up at zero or if
1723 static void COM_ExternalLockRelease(
1727 COM_ExternalLock *externalLock = COM_ExternalLockLocate(elList.head, pUnk);
1729 if ( externalLock != EL_NOT_FOUND ) {
1731 externalLock->uRefCount--; /* release external locks */
1732 IUnknown_Release(pUnk); /* release local locks as well */
1734 if ( bRelAll == FALSE ) break; /* perform single release */
1736 } while ( externalLock->uRefCount > 0 );
1738 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1739 COM_ExternalLockDelete(externalLock);
1742 /****************************************************************************
1743 * Public - Method that frees the content of the list.
1745 static void COM_ExternalLockFreeList()
1747 COM_ExternalLock *head;
1749 head = elList.head; /* grab it by the head */
1750 while ( head != EL_END_OF_LIST ) {
1751 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1752 head = elList.head; /* get the new head... */
1756 /****************************************************************************
1757 * Public - Method that dump the content of the list.
1759 void COM_ExternalLockDump()
1761 COM_ExternalLock *current = elList.head;
1763 DPRINTF("\nExternal lock list contains:\n");
1765 while ( current != EL_END_OF_LIST ) {
1766 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
1768 /* Skip to the next item */
1769 current = current->next;
1773 /******************************************************************************
1774 * CoLockObjectExternal [OLE32.31]
1776 HRESULT WINAPI CoLockObjectExternal(
1777 LPUNKNOWN pUnk, /* [in] object to be locked */
1778 BOOL fLock, /* [in] do lock */
1779 BOOL fLastUnlockReleases) /* [in] unlock all */
1784 * Increment the external lock coutner, COM_ExternalLockAddRef also
1785 * increment the object's internal lock counter.
1787 COM_ExternalLockAddRef( pUnk);
1790 * Decrement the external lock coutner, COM_ExternalLockRelease also
1791 * decrement the object's internal lock counter.
1793 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1799 /***********************************************************************
1800 * CoInitializeWOW (OLE32.27)
1802 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1803 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1807 static IUnknown * pUnkState = 0; /* FIXME: thread local */
1808 static int nStatCounter = 0; /* global */
1809 static HMODULE hOleAut32 = 0; /* global */
1811 /***********************************************************************
1812 * CoGetState [OLE32.24]
1814 * NOTES: might be incomplete
1816 HRESULT WINAPI CoGetState(IUnknown ** ppv)
1821 IUnknown_AddRef(pUnkState);
1823 FIXME("-- %p\n", *ppv);
1831 /***********************************************************************
1832 * CoSetState [OLE32.42]
1834 * NOTES: FIXME: protect this with a crst
1836 HRESULT WINAPI CoSetState(IUnknown * pv)
1838 FIXME("(%p),stub!\n", pv);
1841 IUnknown_AddRef(pv);
1843 if (nStatCounter == 1) LoadLibraryA("OLEAUT32.DLL");
1847 TRACE("-- release %p now\n", pUnkState);
1848 IUnknown_Release(pUnkState);
1850 if (!nStatCounter) FreeLibrary(hOleAut32);
1857 /******************************************************************************
1858 * OleGetAutoConvert [OLE32.104]
1860 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
1868 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
1869 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
1871 res = REGDB_E_CLASSNOTREG;
1875 /* we can just query for the default value of AutoConvertTo key like that,
1876 without opening the AutoConvertTo key and querying for NULL (default) */
1877 if (RegQueryValueA(hkey,"AutoConvertTo",buf,&len))
1879 res = REGDB_E_KEYMISSING;
1882 MultiByteToWideChar( CP_ACP, 0, buf, -1, wbuf, sizeof(wbuf)/sizeof(WCHAR) );
1883 CLSIDFromString(wbuf,pClsidNew);
1885 if (hkey) RegCloseKey(hkey);
1889 /******************************************************************************
1890 * OleSetAutoConvert [OLE32.126]
1892 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
1895 char buf[200], szClsidNew[200];
1898 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
1899 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
1900 WINE_StringFromCLSID(clsidNew, szClsidNew);
1901 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
1903 res = REGDB_E_CLASSNOTREG;
1906 if (RegSetValueA(hkey, "AutoConvertTo", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
1908 res = REGDB_E_WRITEREGDB;
1913 if (hkey) RegCloseKey(hkey);
1917 /******************************************************************************
1918 * OleDoAutoConvert [OLE32.100]
1920 HRESULT WINAPI OleDoAutoConvert(IStorage *pStg, LPCLSID pClsidNew)
1922 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
1926 /******************************************************************************
1927 * CoTreatAsClass [OLE32.46]
1929 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
1932 char buf[200], szClsidNew[200];
1935 FIXME("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
1936 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
1937 WINE_StringFromCLSID(clsidNew, szClsidNew);
1938 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
1940 res = REGDB_E_CLASSNOTREG;
1943 if (RegSetValueA(hkey, "AutoTreatAs", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
1945 res = REGDB_E_WRITEREGDB;
1950 if (hkey) RegCloseKey(hkey);
1954 /******************************************************************************
1955 * CoGetTreatAsClass [OLE32.25]
1957 * Reads the TreatAs value from a class.
1959 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
1962 char buf[200], szClsidNew[200];
1964 LONG len = sizeof(szClsidNew);
1966 FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
1967 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
1968 memcpy(clsidNew,clsidOld,sizeof(CLSID)); /* copy over old value */
1970 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
1972 res = REGDB_E_CLASSNOTREG;
1975 if (RegQueryValueA(hkey, "TreatAs", szClsidNew, &len))
1980 res = __CLSIDFromStringA(szClsidNew,clsidNew);
1982 FIXME("Failed CLSIDFromStringA(%s), hres %lx?\n",szClsidNew,res);
1984 if (hkey) RegCloseKey(hkey);
1989 /***********************************************************************
1990 * IsEqualGUID [OLE32.76]
1992 * Compares two Unique Identifiers.
1998 BOOL WINAPI IsEqualGUID(
1999 REFGUID rguid1, /* [in] unique id 1 */
2000 REFGUID rguid2 /* [in] unique id 2 */
2003 return !memcmp(rguid1,rguid2,sizeof(GUID));
2006 /***********************************************************************
2007 * CoInitializeSecurity [OLE32.164]
2009 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
2010 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
2011 void* pReserved1, DWORD dwAuthnLevel,
2012 DWORD dwImpLevel, void* pReserved2,
2013 DWORD dwCapabilities, void* pReserved3)
2015 FIXME("(%p,%ld,%p,%p,%ld,%ld,%p,%ld,%p) - stub!\n", pSecDesc, cAuthSvc,
2016 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
2017 dwCapabilities, pReserved3);