- Sync ole32 and oleaut32 regtests to Wine-1.3.
[reactos.git] / rostests / winetests / ole32 / compobj.c
index b2aaa22..9d469b4 100644 (file)
 
 #include "windef.h"
 #include "winbase.h"
+#define USE_COM_CONTEXT_DEF
+#include "initguid.h"
 #include "objbase.h"
 #include "shlguid.h"
 #include "urlmon.h" /* for CLSID_FileProtocol */
 
+#include "ctxtcall.h"
+
 #include "wine/test.h"
 
 /* functions that are not present on all versions of Windows */
 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
 HRESULT (WINAPI * pCoGetObjectContext)(REFIID riid, LPVOID *ppv);
+HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppOldObject);
+HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
+HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
 
 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
 #define ok_more_than_one_lock() ok(cLocks > 0, "Number of locks should be > 0, but actually is %d\n", cLocks)
 #define ok_no_locks() ok(cLocks == 0, "Number of locks should be 0, but actually is %d\n", cLocks)
 
 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
-static const CLSID CLSID_CDeviceMoniker = { 0x4315d437, 0x5b8c, 0x11d0, { 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86 } };
-static WCHAR devicedotone[] = {'d','e','v','i','c','e','.','1',0};
+static const CLSID CLSID_StdFont = { 0x0be35203, 0x8f91, 0x11ce, { 0x9d, 0xe3, 0x00, 0xaa, 0x00, 0x4b, 0xb8, 0x51 } };
+static WCHAR stdfont[] = {'S','t','d','F','o','n','t',0};
 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
-static WCHAR wszCLSID_CDeviceMoniker[] =
-{
-    '{',
-    '4','3','1','5','d','4','3','7','-',
-    '5','b','8','c','-',
-    '1','1','d','0','-',
-    'b','d','3','b','-',
-    '0','0','a','0','c','9','1','1','c','e','8','6',
-    '}',0
+static WCHAR wszCLSID_StdFont[] =
+{
+    '{','0','b','e','3','5','2','0','3','-','8','f','9','1','-','1','1','c','e','-',
+    '9','d','e','3','-','0','0','a','a','0','0','4','b','b','8','5','1','}',0
 };
 
 static const IID IID_IWineTest =
@@ -90,7 +92,7 @@ static HRESULT WINAPI Test_IClassFactory_QueryInterface(
     if (IsEqualGUID(riid, &IID_IUnknown) ||
         IsEqualGUID(riid, &IID_IClassFactory))
     {
-        *ppvObj = (LPVOID)iface;
+        *ppvObj = iface;
         IClassFactory_AddRef(iface);
         return S_OK;
     }
@@ -143,11 +145,11 @@ static IClassFactory Test_ClassFactory = { &TestClassFactory_Vtbl };
 static void test_ProgIDFromCLSID(void)
 {
     LPWSTR progid;
-    HRESULT hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, &progid);
+    HRESULT hr = ProgIDFromCLSID(&CLSID_StdFont, &progid);
     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08x\n", hr);
     if (hr == S_OK)
     {
-        ok(!lstrcmpiW(progid, devicedotone), "Didn't get expected prog ID\n");
+        ok(!lstrcmpiW(progid, stdfont), "Didn't get expected prog ID\n");
         CoTaskMemFree(progid);
     }
 
@@ -156,20 +158,20 @@ static void test_ProgIDFromCLSID(void)
     ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08x\n", hr);
     ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
 
-    hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, NULL);
+    hr = ProgIDFromCLSID(&CLSID_StdFont, NULL);
     ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08x\n", hr);
 }
 
 static void test_CLSIDFromProgID(void)
 {
     CLSID clsid;
-    HRESULT hr = CLSIDFromProgID(devicedotone, &clsid);
+    HRESULT hr = CLSIDFromProgID(stdfont, &clsid);
     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08x\n", hr);
-    ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
+    ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
 
-    hr = CLSIDFromString(devicedotone, &clsid);
+    hr = CLSIDFromString(stdfont, &clsid);
     ok_ole_success(hr, "CLSIDFromString");
-    ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
+    ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
 
     /* test some failure cases */
 
@@ -188,9 +190,9 @@ static void test_CLSIDFromProgID(void)
 static void test_CLSIDFromString(void)
 {
     CLSID clsid;
-    HRESULT hr = CLSIDFromString(wszCLSID_CDeviceMoniker, &clsid);
+    HRESULT hr = CLSIDFromString(wszCLSID_StdFont, &clsid);
     ok_ole_success(hr, "CLSIDFromString");
-    ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
+    ok(IsEqualCLSID(&clsid, &CLSID_StdFont), "clsid wasn't equal to CLSID_StdFont\n");
 
     hr = CLSIDFromString(NULL, &clsid);
     ok_ole_success(hr, "CLSIDFromString");
@@ -201,50 +203,173 @@ static void test_StringFromGUID2(void)
 {
   WCHAR str[50];
   int len;
+
+  /* invalid pointer */
+  SetLastError(0xdeadbeef);
+  len = StringFromGUID2(NULL,str,50);
+  ok(len == 0, "len: %d (expected 0)\n", len);
+  ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %x\n", GetLastError());
+
   /* Test corner cases for buffer size */
-  len = StringFromGUID2(&CLSID_CDeviceMoniker,str,50);
+  len = StringFromGUID2(&CLSID_StdFont,str,50);
   ok(len == 39, "len: %d (expected 39)\n", len);
-  ok(!lstrcmpiW(str, wszCLSID_CDeviceMoniker),"string wan't equal for CLSID_CDeviceMoniker\n");
+  ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
 
   memset(str,0,sizeof str);
-  len = StringFromGUID2(&CLSID_CDeviceMoniker,str,39);
+  len = StringFromGUID2(&CLSID_StdFont,str,39);
   ok(len == 39, "len: %d (expected 39)\n", len);
-  ok(!lstrcmpiW(str, wszCLSID_CDeviceMoniker),"string wan't equal for CLSID_CDeviceMoniker\n");
+  ok(!lstrcmpiW(str, wszCLSID_StdFont),"string wasn't equal for CLSID_StdFont\n");
 
-  len = StringFromGUID2(&CLSID_CDeviceMoniker,str,38);
+  len = StringFromGUID2(&CLSID_StdFont,str,38);
   ok(len == 0, "len: %d (expected 0)\n", len);
 
-  len = StringFromGUID2(&CLSID_CDeviceMoniker,str,30);
+  len = StringFromGUID2(&CLSID_StdFont,str,30);
   ok(len == 0, "len: %d (expected 0)\n", len);
 }
 
+struct info
+{
+    HANDLE wait, stop;
+};
+
+static DWORD CALLBACK ole_initialize_thread(LPVOID pv)
+{
+    HRESULT hr;
+    struct info *info = pv;
+
+    hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
+
+    SetEvent(info->wait);
+    WaitForSingleObject(info->stop, 10000);
+
+    CoUninitialize();
+    return hr;
+}
+
 static void test_CoCreateInstance(void)
 {
-    REFCLSID rclsid = &CLSID_MyComputer;
-    IUnknown *pUnk = (IUnknown *)0xdeadbeef;
-    HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
+    HRESULT hr;
+    HANDLE thread;
+    DWORD tid, exitcode;
+    IUnknown *pUnk;
+    struct info info;
+    REFCLSID rclsid = &CLSID_InternetZoneManager;
+
+    pUnk = (IUnknown *)0xdeadbeef;
+    hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
     ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
 
     OleInitialize(NULL);
+
+    /* test errors returned for non-registered clsids */
+    hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
+    ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
+    hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&pUnk);
+    ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered inproc handler should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
+    hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_LOCAL_SERVER, &IID_IUnknown, (void **)&pUnk);
+    ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered local server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
+    hr = CoCreateInstance(&CLSID_non_existent, NULL, CLSCTX_REMOTE_SERVER, &IID_IUnknown, (void **)&pUnk);
+    ok(hr == REGDB_E_CLASSNOTREG, "CoCreateInstance for non-registered remote server should have returned REGDB_E_CLASSNOTREG instead of 0x%08x\n", hr);
+
     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
+    if(hr == REGDB_E_CLASSNOTREG)
+    {
+        skip("IE not installed so can't test CoCreateInstance\n");
+        OleUninitialize();
+        return;
+    }
+
     ok_ole_success(hr, "CoCreateInstance");
     if(pUnk) IUnknown_Release(pUnk);
     OleUninitialize();
 
     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
+
+    /* show that COM doesn't have to be initialized for multi-threaded apartments if another
+       thread has already done so */
+
+    info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
+    ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
+
+    ok( !WaitForSingleObject(info.wait, 10000 ), "wait timed out\n" );
+
+    pUnk = (IUnknown *)0xdeadbeef;
+    hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
+    ok(hr == S_OK, "CoCreateInstance should have returned S_OK instead of 0x%08x\n", hr);
+    if (pUnk) IUnknown_Release(pUnk);
+
+    SetEvent(info.stop);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
+
+    GetExitCodeThread(thread, &exitcode);
+    hr = exitcode;
+    ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
+
+    CloseHandle(thread);
+    CloseHandle(info.wait);
+    CloseHandle(info.stop);
 }
 
 static void test_CoGetClassObject(void)
 {
-    IUnknown *pUnk = (IUnknown *)0xdeadbeef;
-    HRESULT hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
+    HRESULT hr;
+    HANDLE thread;
+    DWORD tid, exitcode;
+    IUnknown *pUnk;
+    struct info info;
+    REFCLSID rclsid = &CLSID_InternetZoneManager;
+
+    hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
     ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
     ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
 
-    hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
-    ok(hr == E_INVALIDARG, "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
+    hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
+    ok(hr == E_INVALIDARG ||
+       broken(hr == CO_E_NOTINITIALIZED), /* win9x */
+       "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08x\n", hr);
+
+    /* show that COM doesn't have to be initialized for multi-threaded apartments if another
+       thread has already done so */
+
+    info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
+    ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
+
+    ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
+
+    pUnk = (IUnknown *)0xdeadbeef;
+    hr = CoGetClassObject(rclsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
+    if(hr == REGDB_E_CLASSNOTREG)
+        skip("IE not installed so can't test CoGetClassObject\n");
+    else
+    {
+        ok(hr == S_OK, "CoGetClassObject should have returned S_OK instead of 0x%08x\n", hr);
+        if (pUnk) IUnknown_Release(pUnk);
+    }
+
+    SetEvent(info.stop);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
+
+    GetExitCodeThread(thread, &exitcode);
+    hr = exitcode;
+    ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
+
+    CloseHandle(thread);
+    CloseHandle(info.wait);
+    CloseHandle(info.stop);
 }
 
 static ATOM register_dummy_class(void)
@@ -286,7 +411,7 @@ static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID
     if (IsEqualGUID(riid, &IID_IUnknown) ||
         IsEqualGUID(riid, &IID_IClassFactory))
     {
-        *ppvObj = (LPVOID)iface;
+        *ppvObj = iface;
         IMessageFilter_AddRef(iface);
         return S_OK;
     }
@@ -397,7 +522,7 @@ static HRESULT WINAPI Test_IUnknown_QueryInterface(
     if (IsEqualIID(riid, &IID_IUnknown) ||
         IsEqualIID(riid, &IID_IWineTest))
     {
-        *ppvObj = (LPVOID)iface;
+        *ppvObj = iface;
         IUnknown_AddRef(iface);
         return S_OK;
     }
@@ -512,7 +637,7 @@ static void test_CoRegisterPSClsid(void)
     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
     ok_ole_success(hr, "CreateStreamOnHGlobal");
 
-    hr = CoMarshalInterface(stream, &IID_IWineTest, (IUnknown *)&Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
+    hr = CoMarshalInterface(stream, &IID_IWineTest, &Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
     ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
     IStream_Release(stream);
 
@@ -723,6 +848,14 @@ static void test_CoRegisterClassObject(void)
     hr = CoRevokeClassObject(cookie);
     ok_ole_success(hr, "CoRevokeClassObject");
 
+    /* test whether an object that doesn't support IClassFactory can be
+     * registered for CLSCTX_LOCAL_SERVER */
+    hr = CoRegisterClassObject(&CLSID_WineOOPTest, &Test_Unknown,
+                               CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie);
+    ok_ole_success(hr, "CoRegisterClassObject");
+    hr = CoRevokeClassObject(cookie);
+    ok_ole_success(hr, "CoRevokeClassObject");
+
     /* test whether registered class becomes invalid when apartment is destroyed */
     hr = CoRegisterClassObject(&CLSID_WineOOPTest, (IUnknown *)&Test_ClassFactory,
                                CLSCTX_INPROC_SERVER, REGCLS_SINGLEUSE, &cookie);
@@ -842,7 +975,7 @@ static void test_registered_object_thread_affinity(void)
 
     thread = CreateThread(NULL, 0, get_class_object_thread, (LPVOID)CLSCTX_INPROC_SERVER, 0, &tid);
     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
-    WaitForSingleObject(thread, INFINITE);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
     GetExitCodeThread(thread, &exitcode);
     hr = exitcode;
     ok(hr == REGDB_E_CLASSNOTREG, "CoGetClassObject on inproc object "
@@ -855,7 +988,7 @@ static void test_registered_object_thread_affinity(void)
 
     thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
-    WaitForSingleObject(thread, INFINITE);
+    ok ( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
     GetExitCodeThread(thread, &exitcode);
     hr = exitcode;
     ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different thread should return S_OK instead of 0x%08x\n", hr);
@@ -871,7 +1004,7 @@ static void test_registered_object_thread_affinity(void)
 
     thread = CreateThread(NULL, 0, get_class_object_proxy_thread, (LPVOID)CLSCTX_LOCAL_SERVER, 0, &tid);
     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
-    while (MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
+    while (MsgWaitForMultipleObjects(1, &thread, FALSE, 10000, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)
     {
         MSG msg;
         while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
@@ -890,9 +1023,9 @@ static void test_registered_object_thread_affinity(void)
     ok(hr == S_OK, "CoGetClassObject on local server object registered in same "
        "thread should return S_OK instead of 0x%08x\n", hr);
 
-    thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)cookie, 0, &tid);
+    thread = CreateThread(NULL, 0, revoke_class_object_thread, (LPVOID)(DWORD_PTR)cookie, 0, &tid);
     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
-    WaitForSingleObject(thread, INFINITE);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
     GetExitCodeThread(thread, &exitcode);
     hr = exitcode;
     ok(hr == RPC_E_WRONG_THREAD, "CoRevokeClassObject called from different "
@@ -900,7 +1033,7 @@ static void test_registered_object_thread_affinity(void)
 
     thread = CreateThread(NULL, 0, register_class_object_thread, NULL, 0, &tid);
     ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
-    WaitForSingleObject(thread, INFINITE);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
     GetExitCodeThread(thread, &exitcode);
     hr = exitcode;
     ok(hr == S_OK, "CoRegisterClassObject with same CLSID but in different "
@@ -934,24 +1067,27 @@ static void test_CoFreeUnusedLibraries(void)
 
     ok(!is_module_loaded("urlmon.dll"), "urlmon.dll shouldn't be loaded\n");
 
-    hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
+    hr = CoCreateInstance(&CLSID_FileProtocol, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void **)&pUnk);
     if (hr == REGDB_E_CLASSNOTREG)
     {
-        trace("IE not installed so can't run CoFreeUnusedLibraries test\n");
+        skip("IE not installed so can't run CoFreeUnusedLibraries test\n");
+        CoUninitialize();
         return;
     }
     ok_ole_success(hr, "CoCreateInstance");
 
     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
 
-    ok(pUnk != NULL, "Expected a valid pointer\n");
+    ok(pUnk != NULL ||
+       broken(pUnk == NULL), /* win9x */
+       "Expected a valid pointer\n");
     if (pUnk)
         IUnknown_Release(pUnk);
 
     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
 
     thread = CreateThread(NULL, 0, free_libraries_thread, NULL, 0, &tid);
-    WaitForSingleObject(thread, INFINITE);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
     CloseHandle(thread);
 
     ok(is_module_loaded("urlmon.dll"), "urlmon.dll should be loaded\n");
@@ -968,8 +1104,13 @@ static void test_CoGetObjectContext(void)
     HRESULT hr;
     ULONG refs;
     IComThreadingInfo *pComThreadingInfo;
+    IContextCallback *pContextCallback;
+    IObjContext *pObjContext;
     APTTYPE apttype;
     THDTYPE thdtype;
+    struct info info;
+    HANDLE thread;
+    DWORD tid, exitcode;
 
     if (!pCoGetObjectContext)
     {
@@ -981,6 +1122,36 @@ static void test_CoGetObjectContext(void)
     ok(hr == CO_E_NOTINITIALIZED, "CoGetObjectContext should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
     ok(pComThreadingInfo == NULL, "pComThreadingInfo should have been set to NULL\n");
 
+    /* show that COM doesn't have to be initialized for multi-threaded apartments if another
+       thread has already done so */
+
+    info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
+    ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
+
+    ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
+
+    pComThreadingInfo = NULL;
+    hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
+    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
+    IComThreadingInfo_Release(pComThreadingInfo);
+
+    SetEvent(info.stop);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
+
+    GetExitCodeThread(thread, &exitcode);
+    hr = exitcode;
+    ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
+
+    CloseHandle(thread);
+    CloseHandle(info.wait);
+    CloseHandle(info.stop);
+
     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 
     hr = pCoGetObjectContext(&IID_IComThreadingInfo, (void **)&pComThreadingInfo);
@@ -997,6 +1168,15 @@ static void test_CoGetObjectContext(void)
     refs = IComThreadingInfo_Release(pComThreadingInfo);
     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
 
+    hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
+    ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
+
+    if (hr == S_OK)
+    {
+        refs = IContextCallback_Release(pContextCallback);
+        ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
+    }
+
     CoUninitialize();
 
     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
@@ -1015,13 +1195,263 @@ static void test_CoGetObjectContext(void)
     refs = IComThreadingInfo_Release(pComThreadingInfo);
     ok(refs == 0, "pComThreadingInfo should have 0 refs instead of %d refs\n", refs);
 
+    hr = pCoGetObjectContext(&IID_IContextCallback, (void **)&pContextCallback);
+    ok_ole_success(hr, "CoGetObjectContext(ContextCallback)");
+
+    if (hr == S_OK)
+    {
+        refs = IContextCallback_Release(pContextCallback);
+        ok(refs == 0, "pContextCallback should have 0 refs instead of %d refs\n", refs);
+    }
+
+    hr = pCoGetObjectContext(&IID_IObjContext, (void **)&pObjContext);
+    ok_ole_success(hr, "CoGetObjectContext");
+
+    refs = IObjContext_Release(pObjContext);
+    ok(refs == 0, "pObjContext should have 0 refs instead of %d refs\n", refs);
+
     CoUninitialize();
 }
 
+typedef struct {
+    const IUnknownVtbl *lpVtbl;
+    LONG refs;
+} Test_CallContext;
+
+static HRESULT WINAPI Test_CallContext_QueryInterface(
+    IUnknown *iface,
+    REFIID riid,
+    LPVOID *ppvObj)
+{
+    if (ppvObj == NULL) return E_POINTER;
+
+    if (IsEqualGUID(riid, &IID_IUnknown))
+    {
+        *ppvObj = iface;
+        IUnknown_AddRef(iface);
+        return S_OK;
+    }
+
+    *ppvObj = NULL;
+    return E_NOINTERFACE;
+}
+
+static ULONG WINAPI Test_CallContext_AddRef(IUnknown *iface)
+{
+    Test_CallContext *This = (Test_CallContext*)iface;
+    return InterlockedIncrement(&This->refs);
+}
+
+static ULONG WINAPI Test_CallContext_Release(IUnknown *iface)
+{
+    Test_CallContext *This = (Test_CallContext*)iface;
+    ULONG refs = InterlockedDecrement(&This->refs);
+    if (!refs)
+        HeapFree(GetProcessHeap(), 0, This);
+    return refs;
+}
+
+static const IUnknownVtbl TestCallContext_Vtbl =
+{
+    Test_CallContext_QueryInterface,
+    Test_CallContext_AddRef,
+    Test_CallContext_Release
+};
+
+static void test_CoGetCallContext(void)
+{
+    HRESULT hr;
+    ULONG refs;
+    IUnknown *pUnk;
+    IUnknown *test_object;
+
+    if (!pCoSwitchCallContext)
+    {
+        skip("CoSwitchCallContext not present\n");
+        return;
+    }
+
+    CoInitialize(NULL);
+
+    test_object = HeapAlloc(GetProcessHeap(), 0, sizeof(Test_CallContext));
+    ((Test_CallContext*)test_object)->lpVtbl = &TestCallContext_Vtbl;
+    ((Test_CallContext*)test_object)->refs = 1;
+
+    hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
+    ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
+
+    pUnk = (IUnknown*)0xdeadbeef;
+    hr = pCoSwitchCallContext(test_object, &pUnk);
+    ok_ole_success(hr, "CoSwitchCallContext");
+    ok(pUnk == NULL, "expected NULL, got %p\n", pUnk);
+    refs = IUnknown_AddRef(test_object);
+    ok(refs == 2, "Expected refcount 2, got %d\n", refs);
+    IUnknown_Release(test_object);
+
+    pUnk = (IUnknown*)0xdeadbeef;
+    hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
+    ok_ole_success(hr, "CoGetCallContext");
+    ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
+    refs = IUnknown_AddRef(test_object);
+    ok(refs == 3, "Expected refcount 3, got %d\n", refs);
+    IUnknown_Release(test_object);
+    IUnknown_Release(pUnk);
+
+    pUnk = (IUnknown*)0xdeadbeef;
+    hr = pCoSwitchCallContext(NULL, &pUnk);
+    ok_ole_success(hr, "CoSwitchCallContext");
+    ok(pUnk == test_object, "expected %p, got %p\n", test_object, pUnk);
+    refs = IUnknown_AddRef(test_object);
+    ok(refs == 2, "Expected refcount 2, got %d\n", refs);
+    IUnknown_Release(test_object);
+
+    hr = CoGetCallContext(&IID_IUnknown, (void**)&pUnk);
+    ok(hr == RPC_E_CALL_COMPLETE, "Expected RPC_E_CALL_COMPLETE, got 0x%08x\n", hr);
+
+    IUnknown_Release(test_object);
+
+    CoUninitialize();
+}
+
+static void test_CoGetContextToken(void)
+{
+    HRESULT hr;
+    ULONG refs;
+    ULONG_PTR token;
+    IObjContext *ctx;
+    struct info info;
+    HANDLE thread;
+    DWORD tid, exitcode;
+
+    if (!pCoGetContextToken)
+    {
+        win_skip("CoGetContextToken not present\n");
+        return;
+    }
+
+    token = 0xdeadbeef;
+    hr = pCoGetContextToken(&token);
+    ok(hr == CO_E_NOTINITIALIZED, "Expected CO_E_NOTINITIALIZED, got 0x%08x\n", hr);
+    ok(token == 0xdeadbeef, "Expected 0, got 0x%lx\n", token);
+
+    /* show that COM doesn't have to be initialized for multi-threaded apartments if another
+       thread has already done so */
+
+    info.wait = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.wait != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    info.stop = CreateEvent(NULL, TRUE, FALSE, NULL);
+    ok(info.stop != NULL, "CreateEvent failed with error %d\n", GetLastError());
+
+    thread = CreateThread(NULL, 0, ole_initialize_thread, &info, 0, &tid);
+    ok(thread != NULL, "CreateThread failed with error %d\n", GetLastError());
+
+    ok( !WaitForSingleObject(info.wait, 10000), "wait timed out\n" );
+
+    token = 0;
+    hr = pCoGetContextToken(&token);
+    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
+
+    SetEvent(info.stop);
+    ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
+
+    GetExitCodeThread(thread, &exitcode);
+    hr = exitcode;
+    ok(hr == S_OK, "thread should have returned S_OK instead of 0x%08x\n", hr);
+
+    CloseHandle(thread);
+    CloseHandle(info.wait);
+    CloseHandle(info.stop);
+
+    CoInitialize(NULL);
+
+    hr = pCoGetContextToken(NULL);
+    ok(hr == E_POINTER, "Expected E_POINTER, got 0x%08x\n", hr);
+
+    token = 0;
+    hr = pCoGetContextToken(&token);
+    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
+    ok(token, "Expected token != 0\n");
+
+    refs = IUnknown_AddRef((IUnknown *)token);
+    todo_wine ok(refs == 1, "Expected 1, got %u\n", refs);
+
+    hr = pCoGetObjectContext(&IID_IObjContext, (void **)&ctx);
+    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
+    todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
+
+    refs = IUnknown_AddRef((IUnknown *)ctx);
+    todo_wine ok(refs == 3, "Expected 3, got %u\n", refs);
+
+    refs = IUnknown_Release((IUnknown *)ctx);
+    todo_wine ok(refs == 2, "Expected 2, got %u\n", refs);
+
+    refs = IUnknown_Release((IUnknown *)token);
+    ok(refs == 1, "Expected 1, got %u\n", refs);
+
+    /* CoGetContextToken does not add a reference */
+    token = 0;
+    hr = pCoGetContextToken(&token);
+    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
+    ok(token, "Expected token != 0\n");
+    todo_wine ok(ctx == (IObjContext *)token, "Expected interface pointers to be the same\n");
+
+    refs = IUnknown_AddRef((IUnknown *)ctx);
+    ok(refs == 2, "Expected 1, got %u\n", refs);
+
+    refs = IUnknown_Release((IUnknown *)ctx);
+    ok(refs == 1, "Expected 0, got %u\n", refs);
+
+    refs = IUnknown_Release((IUnknown *)ctx);
+    ok(refs == 0, "Expected 0, got %u\n", refs);
+
+    CoUninitialize();
+}
+
+static void test_CoGetTreatAsClass(void)
+{
+    HRESULT hr;
+    CLSID out;
+    static GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
+
+    if (!pCoGetTreatAsClass)
+    {
+        win_skip("CoGetTreatAsClass not present\n");
+        return;
+    }
+    hr = pCoGetTreatAsClass(&deadbeef,&out);
+    ok (hr == S_FALSE, "expected S_FALSE got %x\n",hr);
+    ok (IsEqualGUID(&out,&deadbeef), "expected to get same clsid back\n");
+}
+
+static void test_CoInitializeEx(void)
+{
+    HRESULT hr;
+
+    hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
+    ok(hr == S_OK, "CoInitializeEx failed with error 0x%08x\n", hr);
+
+    /* Calling OleInitialize for the first time should yield S_OK even with
+     * apartment already initialized by previous CoInitialize(Ex) calls. */
+    hr = OleInitialize(NULL);
+    ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
+
+    /* Subsequent calls to OleInitialize should return S_FALSE */
+    hr = OleInitialize(NULL);
+    ok(hr == S_FALSE, "Expected S_FALSE, hr = 0x%08x\n", hr);
+
+    /* Cleanup */
+    CoUninitialize();
+    OleUninitialize();
+}
+
 START_TEST(compobj)
 {
     HMODULE hOle32 = GetModuleHandle("ole32");
     pCoGetObjectContext = (void*)GetProcAddress(hOle32, "CoGetObjectContext");
+    pCoSwitchCallContext = (void*)GetProcAddress(hOle32, "CoSwitchCallContext");
+    pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
+    pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
     {
         trace("You need DCOM95 installed to run this test\n");
@@ -1046,4 +1476,8 @@ START_TEST(compobj)
     test_registered_object_thread_affinity();
     test_CoFreeUnusedLibraries();
     test_CoGetObjectContext();
+    test_CoGetCallContext();
+    test_CoGetContextToken();
+    test_CoGetTreatAsClass();
+    test_CoInitializeEx();
 }