summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorArnavion <arnavion@gmail.com>2015-02-10 22:14:22 -0800
committerArnavion <arnavion@gmail.com>2015-02-10 22:14:22 -0800
commit5a87d814cf87e817390cdf40aff3fe7a6ea4948f (patch)
tree274273b6329781ba2d44c50f6765fcd7a5b78849
parentac54a2ed457d37a40be193bb0fea684f1dd0e197 (diff)
Call CoInitialize, CoInitializeSecurity and CoUninitialize from the main process, not from a DLL.
They're process-level functions and aren't meant to be called from DLLs. CoInitSecurity in particular fails with RPC_E_TOO_LATE even if no other call to CoCreateInstance has been made yet.

Fixes sysinfo's WMI calls on Windows 8.1 and above.
-rw-r--r--plugins/sysinfo/sysinfo.c16
-rw-r--r--src/common/hexchat.c19
2 files changed, 20 insertions, 15 deletions
diff --git a/plugins/sysinfo/sysinfo.c b/plugins/sysinfo/sysinfo.c
index 48dd16fc..c243de42 100644
--- a/plugins/sysinfo/sysinfo.c
+++ b/plugins/sysinfo/sysinfo.c
@@ -235,21 +235,10 @@ static char *query_wmi (QueryWmiType type)
 	int i;
 	gboolean atleast_one_appended = FALSE;
 
-	hr = CoInitializeEx (0, COINIT_APARTMENTTHREADED);
-	if (FAILED (hr))
-	{
-		goto exit;
-	}
-
-	/* If this is called after some other call to CoCreateInstance somewhere else in the process, this will fail with RPC_E_TOO_LATE.
-	 * However if not, it *is* required to be called, so call it here but ignore any error returned.
-	 */
-	CoInitializeSecurity (NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
-
 	hr = CoCreateInstance (&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *) &locator);
 	if (FAILED (hr))
 	{
-		goto couninitialize;
+		goto exit;
 	}
 
 	namespaceName = SysAllocString (L"root\\CIMV2");
@@ -369,9 +358,6 @@ release_locator:
 	locator->lpVtbl->Release (locator);
 	SysFreeString (namespaceName);
 
-couninitialize:
-	CoUninitialize ();
-
 exit:
 	if (result == NULL)
 	{
diff --git a/src/common/hexchat.c b/src/common/hexchat.c
index e304d5ff..a76db332 100644
--- a/src/common/hexchat.c
+++ b/src/common/hexchat.c
@@ -996,6 +996,10 @@ main (int argc, char *argv[])
 	int i;
 	int ret;
 
+#ifdef WIN32
+	HRESULT coinit_result;
+#endif
+
 	srand ((unsigned int) time (NULL)); /* CL: do this only once! */
 
 	/* We must check for the config dir parameter, otherwise load_config() will behave incorrectly.
@@ -1058,6 +1062,14 @@ main (int argc, char *argv[])
 	libproxy_factory = px_proxy_factory_new();
 #endif
 
+#ifdef WIN32
+	coinit_result = CoInitializeEx (NULL, COINIT_APARTMENTTHREADED);
+	if (SUCCEEDED (coinit_result))
+	{
+		CoInitializeSecurity (NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
+	}
+#endif
+
 	fe_init ();
 
 	/* This is done here because cfgfiles.c is too early in
@@ -1085,6 +1097,13 @@ main (int argc, char *argv[])
 
 	fe_main ();
 
+#ifdef WIN32
+	if (SUCCEEDED (coinit_result))
+	{
+		CoUninitialize ();
+	}
+#endif
+
 #ifdef USE_LIBPROXY
 	px_proxy_factory_free(libproxy_factory);
 #endif