| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "Python.h" |
| 12 |
#include "windows.h" |
| 13 |
|
| 14 |
#ifdef Py_ENABLE_SHARED |
| 15 |
char dllVersionBuffer[16] = ""; |
| 16 |
|
| 17 |
|
| 18 |
HMODULE PyWin_DLLhModule = NULL; |
| 19 |
const char *PyWin_DLLVersionString = dllVersionBuffer; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
typedef BOOL (WINAPI * PFN_GETCURRENTACTCTX)(HANDLE *); |
| 34 |
typedef BOOL (WINAPI * PFN_ACTIVATEACTCTX)(HANDLE, ULONG_PTR *); |
| 35 |
typedef BOOL (WINAPI * PFN_DEACTIVATEACTCTX)(DWORD, ULONG_PTR); |
| 36 |
typedef void (WINAPI * PFN_ADDREFACTCTX)(HANDLE); |
| 37 |
typedef void (WINAPI * PFN_RELEASEACTCTX)(HANDLE); |
| 38 |
|
| 39 |
|
| 40 |
static HANDLE PyWin_DLLhActivationContext = NULL; |
| 41 |
static PFN_GETCURRENTACTCTX pfnGetCurrentActCtx = NULL; |
| 42 |
static PFN_ACTIVATEACTCTX pfnActivateActCtx = NULL; |
| 43 |
static PFN_DEACTIVATEACTCTX pfnDeactivateActCtx = NULL; |
| 44 |
static PFN_ADDREFACTCTX pfnAddRefActCtx = NULL; |
| 45 |
static PFN_RELEASEACTCTX pfnReleaseActCtx = NULL; |
| 46 |
|
| 47 |
void _LoadActCtxPointers() |
| 48 |
{ |
| 49 |
HINSTANCE hKernel32 = GetModuleHandleW(L"kernel32.dll"); |
| 50 |
if (hKernel32) |
| 51 |
pfnGetCurrentActCtx = (PFN_GETCURRENTACTCTX) GetProcAddress(hKernel32, "GetCurrentActCtx"); |
| 52 |
|
| 53 |
if (pfnGetCurrentActCtx) { |
| 54 |
pfnActivateActCtx = (PFN_ACTIVATEACTCTX) GetProcAddress(hKernel32, "ActivateActCtx"); |
| 55 |
pfnDeactivateActCtx = (PFN_DEACTIVATEACTCTX) GetProcAddress(hKernel32, "DeactivateActCtx"); |
| 56 |
pfnAddRefActCtx = (PFN_ADDREFACTCTX) GetProcAddress(hKernel32, "AddRefActCtx"); |
| 57 |
pfnReleaseActCtx = (PFN_RELEASEACTCTX) GetProcAddress(hKernel32, "ReleaseActCtx"); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
ULONG_PTR _Py_ActivateActCtx() |
| 62 |
{ |
| 63 |
ULONG_PTR ret = 0; |
| 64 |
if (PyWin_DLLhActivationContext && pfnActivateActCtx) |
| 65 |
if (!(*pfnActivateActCtx)(PyWin_DLLhActivationContext, &ret)) { |
| 66 |
OutputDebugString("Python failed to activate the activation context before loading a DLL\n"); |
| 67 |
ret = 0; |
| 68 |
} |
| 69 |
return ret; |
| 70 |
} |
| 71 |
|
| 72 |
void _Py_DeactivateActCtx(ULONG_PTR cookie) |
| 73 |
{ |
| 74 |
if (cookie && pfnDeactivateActCtx) |
| 75 |
if (!(*pfnDeactivateActCtx)(0, cookie)) |
| 76 |
OutputDebugString("Python failed to de-activate the activation context\n"); |
| 77 |
} |
| 78 |
|
| 79 |
BOOL WINAPI DllMain (HANDLE hInst, |
| 80 |
ULONG ul_reason_for_call, |
| 81 |
LPVOID lpReserved) |
| 82 |
{ |
| 83 |
switch (ul_reason_for_call) |
| 84 |
{ |
| 85 |
case DLL_PROCESS_ATTACH: |
| 86 |
PyWin_DLLhModule = hInst; |
| 87 |
|
| 88 |
LoadString(hInst, 1000, dllVersionBuffer, sizeof(dllVersionBuffer)); |
| 89 |
|
| 90 |
|
| 91 |
_LoadActCtxPointers(); |
| 92 |
if (pfnGetCurrentActCtx && pfnAddRefActCtx) |
| 93 |
if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext)) { |
| 94 |
(*pfnAddRefActCtx)(PyWin_DLLhActivationContext); |
| 95 |
} |
| 96 |
else { |
| 97 |
OutputDebugString("Python failed to load the default " |
| 98 |
"activation context\n"); |
| 99 |
return FALSE; |
| 100 |
} |
| 101 |
break; |
| 102 |
|
| 103 |
case DLL_PROCESS_DETACH: |
| 104 |
if (pfnReleaseActCtx) |
| 105 |
(*pfnReleaseActCtx)(PyWin_DLLhActivationContext); |
| 106 |
break; |
| 107 |
} |
| 108 |
return TRUE; |
| 109 |
} |
| 110 |
|
| 111 |
#endif |