This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes can't load a c++ dll if the dll calls COM on Windows 8
Type: behavior Stage:
Components: ctypes Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: yinkaisheng
Priority: normal Keywords:

Created on 2013-11-26 06:38 by yinkaisheng, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
TestDll.rar yinkaisheng, 2013-11-26 06:38 a dll project created by VS2012 on Windows 8
Messages (4)
msg204454 - (view) Author: yinkaisheng (yinkaisheng) Date: 2013-11-26 06:38
I created a c++ dll project that calls COM.
cyptes works well with the dll on Windows XP(SP3) and Windows 7.
But it can't load the dll on windows 8. 
The function ctypes.cdll.LoadLibrary doesn't return when I call it to load the dll on Windows 8.

The version of my Python is 2.7.6
The version of my Windows 8 is 6.2.9200.16384

I uploaded a sample dll project created by VS2012 as the attachment.
Here are the c++ codes

// dllmain.cpp :
#include "stdafx.h"
#include <Windows.h>

#include <UIAutomation.h>
IUIAutomation *g_pAutomation = NULL;

#define DLL_EXPORT __declspec(dllexport)

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		{
			if (NULL == g_pAutomation)
			{
				CoInitialize(NULL);
				HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), (void**)&g_pAutomation);
			}
			break;
		}
	case DLL_THREAD_ATTACH:
		{
			break;
		}
	case DLL_THREAD_DETACH:
		{
			break;
		}
	case DLL_PROCESS_DETACH:
		{
			if (g_pAutomation)
			{
				g_pAutomation->Release();
				CoUninitialize();
			}
			break;
		}
	}
	return TRUE;
}


#ifdef __cplusplus
extern "C"
{
#endif

	DLL_EXPORT int AddTwoNumber(int a, int b)
	{
		return a + b;
	}

#ifdef __cplusplus
}
#endif
msg204455 - (view) Author: yinkaisheng (yinkaisheng) Date: 2013-11-26 06:43
The version of my Python is 2.7.5.6
msg204709 - (view) Author: yinkaisheng (yinkaisheng) Date: 2013-11-29 03:08
After debugging, I found the reason.
when ctypes loads the dll, CoCreateInstance blocks the flow.

If I don't call CoCreateInstance in function DllMain. ctypes can load the dll.

I should call function CoCreateInstance after DllMain was called. 

The problem was fixed.
msg204778 - (view) Author: yinkaisheng (yinkaisheng) Date: 2013-11-30 05:46
http://support.microsoft.com/kb/305723

COM application hangs when you call CoCreateInstance from DllMain

SYMPTOMS
When you call the CoCreateInstance function from the DllMain function, the Component Object Model (COM) application hangs.

CAUSE
CoCreateInstance may start a thread that tries to call the DllMain function of all dynamic-link libraries (DLLs) that exist in the process by passing a DLL_THREAD_ATTACH value. Because the call originates from DllMain, and because DllMain is not a reentrant function, the call to CoCreateInstance never returns.

RESOLUTION
To resolve this problem, do not call CoCreateInstance from DllMain. You can move the call to an initialization function or to any other function that is called later.

STATUS
This behavior is by design.
History
Date User Action Args
2022-04-11 14:57:54adminsetgithub: 63989
2013-11-30 05:46:58yinkaishengsetmessages: + msg204778
2013-11-29 03:08:06yinkaishengsetstatus: open -> closed
resolution: wont fix
messages: + msg204709
2013-11-26 06:43:26yinkaishengsetmessages: + msg204455
2013-11-26 06:38:32yinkaishengcreate