From 66fe4e28e5cf72bbad572c102579ec3259e4c73c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
Date: Fri, 15 Jan 2016 12:16:32 +0100
Subject: ole32: Implement CoGetApartmentType.

---
 .../api-ms-win-core-com-l1-1-0.spec                |  2 +-
 .../api-ms-win-downlevel-ole32-l1-1-0.spec         |  2 +-
 dlls/combase/combase.spec                          |  2 +-
 dlls/ole32/compobj.c                               | 29 +++++++++++++
 dlls/ole32/ole32.spec                              |  1 +
 dlls/ole32/tests/compobj.c                         | 50 ++++++++++++++++++++++
 include/objidl.idl                                 |  9 ++++
 7 files changed, 92 insertions(+), 3 deletions(-)

diff --git a/dlls/api-ms-win-core-com-l1-1-0/api-ms-win-core-com-l1-1-0.spec b/dlls/api-ms-win-core-com-l1-1-0/api-ms-win-core-com-l1-1-0.spec
index f8026db..f645c9d 100644
--- a/dlls/api-ms-win-core-com-l1-1-0/api-ms-win-core-com-l1-1-0.spec
+++ b/dlls/api-ms-win-core-com-l1-1-0/api-ms-win-core-com-l1-1-0.spec
@@ -17,7 +17,7 @@
 @ stub CoEnableCallCancellation
 @ stdcall CoFreeUnusedLibraries() ole32.CoFreeUnusedLibraries
 @ stdcall CoFreeUnusedLibrariesEx(long long) ole32.CoFreeUnusedLibrariesEx
-@ stub CoGetApartmentType
+@ stdcall CoGetApartmentType(ptr ptr) ole32.CoGetApartmentType
 @ stdcall CoGetCallContext(ptr ptr) ole32.CoGetCallContext
 @ stdcall CoGetCallerTID(ptr) ole32.CoGetCallerTID
 @ stub CoGetCancelObject
diff --git a/dlls/api-ms-win-downlevel-ole32-l1-1-0/api-ms-win-downlevel-ole32-l1-1-0.spec b/dlls/api-ms-win-downlevel-ole32-l1-1-0/api-ms-win-downlevel-ole32-l1-1-0.spec
index a0eabe9..82ff0f1 100644
--- a/dlls/api-ms-win-downlevel-ole32-l1-1-0/api-ms-win-downlevel-ole32-l1-1-0.spec
+++ b/dlls/api-ms-win-downlevel-ole32-l1-1-0/api-ms-win-downlevel-ole32-l1-1-0.spec
@@ -8,7 +8,7 @@
 @ stdcall CoDisconnectObject(ptr long) ole32.CoDisconnectObject
 @ stdcall CoFreeUnusedLibraries() ole32.CoFreeUnusedLibraries
 @ stdcall CoFreeUnusedLibrariesEx(long long) ole32.CoFreeUnusedLibrariesEx
-@ stub CoGetApartmentType
+@ stdcall CoGetApartmentType(ptr ptr) ole32.CoGetApartmentType
 @ stdcall CoGetClassObject(ptr long ptr ptr ptr) ole32.CoGetClassObject
 @ stdcall CoGetCurrentLogicalThreadId(ptr) ole32.CoGetCurrentLogicalThreadId
 @ stdcall CoGetInterfaceAndReleaseStream(ptr ptr ptr) ole32.CoGetInterfaceAndReleaseStream
diff --git a/dlls/combase/combase.spec b/dlls/combase/combase.spec
index 5082f39..075d904 100644
--- a/dlls/combase/combase.spec
+++ b/dlls/combase/combase.spec
@@ -90,7 +90,7 @@
 @ stdcall CoFreeUnusedLibrariesEx(long long) ole32.CoFreeUnusedLibrariesEx
 @ stdcall CoGetActivationState(int128 long ptr) ole32.CoGetActivationState
 @ stub CoGetApartmentID
-@ stub CoGetApartmentType
+@ stdcall CoGetApartmentType(ptr ptr) ole32.CoGetApartmentType
 @ stdcall CoGetCallContext(ptr ptr) ole32.CoGetCallContext
 @ stdcall CoGetCallState(long ptr) ole32.CoGetCallState
 @ stdcall CoGetCallerTID(ptr) ole32.CoGetCallerTID
diff --git a/dlls/ole32/compobj.c b/dlls/ole32/compobj.c
index 90b049a..d8d618e 100644
--- a/dlls/ole32/compobj.c
+++ b/dlls/ole32/compobj.c
@@ -5027,6 +5027,35 @@ HRESULT Handler_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
 }
 
 /***********************************************************************
+ *           CoGetApartmentType [OLE32.@]
+ */
+HRESULT WINAPI CoGetApartmentType(APTTYPE *type, APTTYPEQUALIFIER *qualifier)
+{
+    struct oletls *info = COM_CurrentInfo();
+
+    FIXME("(%p %p): semi-stub\n", type, qualifier);
+
+    if (!type || !qualifier)
+        return E_INVALIDARG;
+
+    if (!info)
+        return E_OUTOFMEMORY;
+
+    if (!info->apt)
+        *type = APTTYPE_CURRENT;
+    else if (info->apt->multi_threaded)
+        *type = APTTYPE_MTA;
+    else if (info->apt->main)
+        *type = APTTYPE_MAINSTA;
+    else
+        *type = APTTYPE_STA;
+
+    *qualifier = APTTYPEQUALIFIER_NONE;
+
+    return info->apt ? ERROR_SUCCESS : CO_E_NOTINITIALIZED;
+}
+
+/***********************************************************************
  *		DllMain (OLE32.@)
  */
 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID reserved)
diff --git a/dlls/ole32/ole32.spec b/dlls/ole32/ole32.spec
index 04c9f1e..c836176 100644
--- a/dlls/ole32/ole32.spec
+++ b/dlls/ole32/ole32.spec
@@ -23,6 +23,7 @@
 @ stdcall CoFreeUnusedLibraries()
 @ stdcall CoFreeUnusedLibrariesEx(long long)
 @ stdcall CoGetActivationState(int128 long ptr)
+@ stdcall CoGetApartmentType(ptr ptr)
 @ stdcall CoGetCallContext(ptr ptr)
 @ stdcall CoGetCallState(long ptr)
 @ stdcall CoGetCallerTID(ptr)
diff --git a/dlls/ole32/tests/compobj.c b/dlls/ole32/tests/compobj.c
index 5fe9876..2ca5a45 100644
--- a/dlls/ole32/tests/compobj.c
+++ b/dlls/ole32/tests/compobj.c
@@ -70,6 +70,7 @@ static HRESULT (WINAPI * pCoSwitchCallContext)(IUnknown *pObject, IUnknown **ppO
 static HRESULT (WINAPI * pCoGetTreatAsClass)(REFCLSID clsidOld, LPCLSID pClsidNew);
 static HRESULT (WINAPI * pCoTreatAsClass)(REFCLSID clsidOld, REFCLSID pClsidNew);
 static HRESULT (WINAPI * pCoGetContextToken)(ULONG_PTR *token);
+static HRESULT (WINAPI * pCoGetApartmentType)(APTTYPE *type, APTTYPEQUALIFIER *qualifier);
 static LONG (WINAPI * pRegDeleteKeyExA)(HKEY, LPCSTR, REGSAM, DWORD);
 static LONG (WINAPI * pRegOverridePredefKey)(HKEY key, HKEY override);
 
@@ -2812,6 +2813,53 @@ if (0) /* crashes on native */
     IMalloc_Release(imalloc);
 }
 
+static void test_CoGetApartmentType(void)
+{
+    APTTYPEQUALIFIER qualifier;
+    APTTYPE type;
+    HRESULT hr;
+
+    if (!pCoGetApartmentType)
+    {
+        win_skip("CoGetApartmentType not present\n");
+        return;
+    }
+
+    hr = pCoGetApartmentType(NULL, NULL);
+    ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%0x\n", hr);
+
+    hr = pCoGetApartmentType(&type, NULL);
+    ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%0x\n", hr);
+
+    hr = pCoGetApartmentType(NULL, &qualifier);
+    ok(hr == E_INVALIDARG, "CoGetApartmentType succeeded, error: 0x%0x\n", hr);
+
+    hr = pCoGetApartmentType(&type, &qualifier);
+    ok(hr == CO_E_NOTINITIALIZED, "CoGetApartmentType succeeded, error: 0x%0x\n", hr);
+    ok(type == APTTYPE_CURRENT, "Expected APTTYPE_CURRENT, got %u\n", type);
+    ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
+
+    hr = pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
+    ok(!hr, "CoInitializeEx failed, error: 0x%08x\n", hr);
+
+    hr = pCoGetApartmentType(&type, &qualifier);
+    ok(!hr, "CoGetApartmentType failed, error: 0x%08x\n", hr);
+    ok(type == APTTYPE_MAINSTA, "Expected APTTYPE_MAINSTA, got %u\n", type);
+    ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
+
+    CoUninitialize();
+
+    hr = pCoInitializeEx(NULL, COINIT_MULTITHREADED);
+    ok(!hr, "CoInitializeEx failed, error: 0x%08x\n", hr);
+
+    hr = pCoGetApartmentType(&type, &qualifier);
+    ok(!hr, "CoGetApartmentType failed, error: 0x%08x\n", hr);
+    ok(type == APTTYPE_MTA, "Expected APTTYPE_MTA, got %u\n", type);
+    ok(qualifier == APTTYPEQUALIFIER_NONE, "Expected APTTYPEQUALIFIER_NONE, got %u\n", qualifier);
+
+    CoUninitialize();
+}
+
 static void init_funcs(void)
 {
     HMODULE hOle32 = GetModuleHandleA("ole32");
@@ -2823,6 +2871,7 @@ static void init_funcs(void)
     pCoGetTreatAsClass = (void*)GetProcAddress(hOle32,"CoGetTreatAsClass");
     pCoTreatAsClass = (void*)GetProcAddress(hOle32,"CoTreatAsClass");
     pCoGetContextToken = (void*)GetProcAddress(hOle32, "CoGetContextToken");
+    pCoGetApartmentType = (void*)GetProcAddress(hOle32, "CoGetApartmentType");
     pRegDeleteKeyExA = (void*)GetProcAddress(hAdvapi32, "RegDeleteKeyExA");
     pRegOverridePredefKey = (void*)GetProcAddress(hAdvapi32, "RegOverridePredefKey");
     pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx");
@@ -2876,4 +2925,5 @@ START_TEST(compobj)
     test_CoWaitForMultipleHandles();
     test_CoGetMalloc();
     test_OleRegGetUserType();
+    test_CoGetApartmentType();
 }
diff --git a/include/objidl.idl b/include/objidl.idl
index c18442f..859620a 100644
--- a/include/objidl.idl
+++ b/include/objidl.idl
@@ -2327,6 +2327,15 @@ typedef enum _APTTYPE {
     APTTYPE_MAINSTA = 3
 } APTTYPE;
 
+typedef enum _APTTYPEQUALIFIER {
+    APTTYPEQUALIFIER_NONE                = 0,
+    APTTYPEQUALIFIER_IMPLICIT_MTA        = 1,
+    APTTYPEQUALIFIER_NA_ON_MTA           = 2,
+    APTTYPEQUALIFIER_NA_ON_STA           = 3,
+    APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA  = 4,
+    APTTYPEQUALIFIER_NA_ON_MAINSTA       = 5
+} APTTYPEQUALIFIER;
+
 typedef enum _THDTYPE {
     THDTYPE_BLOCKMESSAGES   = 0,
     THDTYPE_PROCESSMESSAGES = 1
-- 
2.6.4

