blob: c87c51eb559f9734692e444e760c8da73f46075a [file] [log] [blame]
Guido van Rossum6dbd1901996-08-21 15:03:37 +00001/*
2
3Entry point for the Windows NT DLL.
4
5About the only reason for having this, is so initall() can automatically
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006be called, removing that burden (and possible source of frustration if
Guido van Rossum6dbd1901996-08-21 15:03:37 +00007forgotten) from the programmer.
8
9*/
Guido van Rossum6dbd1901996-08-21 15:03:37 +000010
Guido van Rossuma1ebdbd1997-05-05 22:18:50 +000011#include "Python.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000012#include "windows.h"
Guido van Rossum6dbd1901996-08-21 15:03:37 +000013
Christian Heimesd59c64c2007-11-30 19:27:20 +000014#ifdef Py_ENABLE_SHARED
Steve Dower25879522015-01-15 09:10:16 -080015#ifdef MS_DLL_ID
16// The string is available at build, so fill the buffer immediately
17char dllVersionBuffer[16] = MS_DLL_ID;
18#else
Guido van Rossumec680921997-09-29 23:37:12 +000019char dllVersionBuffer[16] = ""; // a private buffer
Steve Dower25879522015-01-15 09:10:16 -080020#endif
Guido van Rossumec680921997-09-29 23:37:12 +000021
22// Python Globals
Guido van Rossum6dbd1901996-08-21 15:03:37 +000023HMODULE PyWin_DLLhModule = NULL;
Guido van Rossumec680921997-09-29 23:37:12 +000024const char *PyWin_DLLVersionString = dllVersionBuffer;
25
Brian Curtin401f9f32012-05-13 11:19:23 -050026#if HAVE_SXS
27// Windows "Activation Context" work.
Mark Hammond9844a1f2009-01-27 23:46:57 +000028// Our .pyd extension modules are generally built without a manifest (ie,
29// those included with Python and those built with a default distutils.
30// This requires we perform some "activation context" magic when loading our
31// extensions. In summary:
32// * As our DLL loads we save the context being used.
33// * Before loading our extensions we re-activate our saved context.
34// * After extension load is complete we restore the old context.
35// As an added complication, this magic only works on XP or later - we simply
36// use the existence (or not) of the relevant function pointers from kernel32.
37// See bug 4566 (http://python.org/sf/4566) for more details.
Brian Curtin401f9f32012-05-13 11:19:23 -050038// In Visual Studio 2010, side by side assemblies are no longer used by
39// default.
Mark Hammond9844a1f2009-01-27 23:46:57 +000040
41typedef BOOL (WINAPI * PFN_GETCURRENTACTCTX)(HANDLE *);
42typedef BOOL (WINAPI * PFN_ACTIVATEACTCTX)(HANDLE, ULONG_PTR *);
43typedef BOOL (WINAPI * PFN_DEACTIVATEACTCTX)(DWORD, ULONG_PTR);
44typedef BOOL (WINAPI * PFN_ADDREFACTCTX)(HANDLE);
45typedef BOOL (WINAPI * PFN_RELEASEACTCTX)(HANDLE);
46
47// locals and function pointers for this activation context magic.
48static HANDLE PyWin_DLLhActivationContext = NULL; // one day it might be public
49static PFN_GETCURRENTACTCTX pfnGetCurrentActCtx = NULL;
50static PFN_ACTIVATEACTCTX pfnActivateActCtx = NULL;
51static PFN_DEACTIVATEACTCTX pfnDeactivateActCtx = NULL;
52static PFN_ADDREFACTCTX pfnAddRefActCtx = NULL;
53static PFN_RELEASEACTCTX pfnReleaseActCtx = NULL;
54
55void _LoadActCtxPointers()
56{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 HINSTANCE hKernel32 = GetModuleHandleW(L"kernel32.dll");
58 if (hKernel32)
59 pfnGetCurrentActCtx = (PFN_GETCURRENTACTCTX) GetProcAddress(hKernel32, "GetCurrentActCtx");
60 // If we can't load GetCurrentActCtx (ie, pre XP) , don't bother with the rest.
61 if (pfnGetCurrentActCtx) {
62 pfnActivateActCtx = (PFN_ACTIVATEACTCTX) GetProcAddress(hKernel32, "ActivateActCtx");
63 pfnDeactivateActCtx = (PFN_DEACTIVATEACTCTX) GetProcAddress(hKernel32, "DeactivateActCtx");
64 pfnAddRefActCtx = (PFN_ADDREFACTCTX) GetProcAddress(hKernel32, "AddRefActCtx");
65 pfnReleaseActCtx = (PFN_RELEASEACTCTX) GetProcAddress(hKernel32, "ReleaseActCtx");
66 }
Mark Hammond9844a1f2009-01-27 23:46:57 +000067}
68
69ULONG_PTR _Py_ActivateActCtx()
70{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 ULONG_PTR ret = 0;
72 if (PyWin_DLLhActivationContext && pfnActivateActCtx)
73 if (!(*pfnActivateActCtx)(PyWin_DLLhActivationContext, &ret)) {
74 OutputDebugString("Python failed to activate the activation context before loading a DLL\n");
75 ret = 0; // no promise the failing function didn't change it!
76 }
77 return ret;
Mark Hammond9844a1f2009-01-27 23:46:57 +000078}
79
80void _Py_DeactivateActCtx(ULONG_PTR cookie)
81{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (cookie && pfnDeactivateActCtx)
83 if (!(*pfnDeactivateActCtx)(0, cookie))
84 OutputDebugString("Python failed to de-activate the activation context\n");
Mark Hammond9844a1f2009-01-27 23:46:57 +000085}
Brian Curtin401f9f32012-05-13 11:19:23 -050086#endif /* HAVE_SXS */
Guido van Rossum6dbd1901996-08-21 15:03:37 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088BOOL WINAPI DllMain (HANDLE hInst,
89 ULONG ul_reason_for_call,
90 LPVOID lpReserved)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 switch (ul_reason_for_call)
93 {
94 case DLL_PROCESS_ATTACH:
95 PyWin_DLLhModule = hInst;
Steve Dower25879522015-01-15 09:10:16 -080096#ifndef MS_DLL_ID
97 // If we have MS_DLL_ID, we don't need to load the string.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 // 1000 is a magic number I picked out of the air. Could do with a #define, I spose...
99 LoadString(hInst, 1000, dllVersionBuffer, sizeof(dllVersionBuffer));
Steve Dower25879522015-01-15 09:10:16 -0800100#endif
Mark Hammond9844a1f2009-01-27 23:46:57 +0000101
Brian Curtin401f9f32012-05-13 11:19:23 -0500102#if HAVE_SXS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 // and capture our activation context for use when loading extensions.
104 _LoadActCtxPointers();
105 if (pfnGetCurrentActCtx && pfnAddRefActCtx)
106 if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext))
107 if (!(*pfnAddRefActCtx)(PyWin_DLLhActivationContext))
108 OutputDebugString("Python failed to load the default activation context\n");
Brian Curtin401f9f32012-05-13 11:19:23 -0500109#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 break;
Mark Hammond9844a1f2009-01-27 23:46:57 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 case DLL_PROCESS_DETACH:
Brian Curtin401f9f32012-05-13 11:19:23 -0500113#if HAVE_SXS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (pfnReleaseActCtx)
115 (*pfnReleaseActCtx)(PyWin_DLLhActivationContext);
Brian Curtin401f9f32012-05-13 11:19:23 -0500116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 break;
118 }
119 return TRUE;
Guido van Rossum6dbd1901996-08-21 15:03:37 +0000120}
Christian Heimesd59c64c2007-11-30 19:27:20 +0000121
122#endif /* Py_ENABLE_SHARED */