Windows中的延迟加载

时间:2014-11-26 00:02:22

标签: python c++ windows delay-load pycxx

我试图了解一些代码(直接从PyCXX改编)。它是一个多平台的C ++ Python包装器。

编辑:原始代码here

它似乎适用于仅存在于Windows中的某些特定现象:

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL
:
#else
:
#endif

我将在下面给出完整的文件列表,它很长。

这个PY_WIN32_DELAYLOAD_PYTHON_DLL标记在CPython中不存在,它也没有在PyCXX中定义。因此我只能想象PyCXX打算将它作为可选的编译器标志提供。

我想知道的是:它的目的是什么?它解决了什么问题?为什么这种机制甚至存在?

也许熟悉Windows编程的人可以从代码中找到它?

我想知道它正在解决的问题是否仍然存在于现代Windows中,因为代码是> 15岁。

关键问题是:我可以删除它,或者用更干净的东西替换它吗?

我非常想把它剪掉;但它在现代Windows环境中是否仍然有用?

代码:

#include "Base.hxx" //"IndirectPythonInterface.hxx"

namespace Py
{

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL

#   ifndef MS_WINDOWS
#       error "Can only delay load under Win32"
#   endif
#   include <windows.h> // !!! Is it possible we ever want Windows.h but no delay load? If so, this is wrong...

static HMODULE python_dll;

#   define DECLARE_ERROR( THE_ERROR ) \
        static PyObject* ptr__Exc_##THE_ERROR = nullptr;
ALL_ERRORS( DECLARE_ERROR )


static PyObject* ptr__PyNone        = nullptr;
static PyObject* ptr__PyFalse       = nullptr;
static PyObject* ptr__PyTrue        = nullptr;

#   define DECLARE_TYPE( T ) \
        static PyTypeObject* ptr__##T##_Type = nullptr;
ALL_TYPES( DECLARE_TYPE )


static int* ptr_Py_DebugFlag        = nullptr;
static int* ptr_Py_InteractiveFlag  = nullptr;
static int* ptr_Py_OptimizeFlag     = nullptr;
static int* ptr_Py_NoSiteFlag       = nullptr;
static int* ptr_Py_VerboseFlag      = nullptr;

static char** ptr__Py_PackageContext = nullptr;

#   ifdef Py_REF_DEBUG
int* ptr_Py_RefTotal; // !!! Why not static?
#   endif


//--------------------------------------------------------------------------------
class GetAddressException
{
public:
    GetAddressException( const char* _name )
        : name( _name )
    { }
    virtual ~GetAddressException() { }
    const char* name;
};


//--------------------------------------------------------------------------------

#   define GET_PTR( FUNC, RETURN_TYPE ) \
        static FUNC( const char *name ) \
            { \
                FARPROC addr = GetProcAddress( python_dll, name ); \
                if( addr == nullptr ) \
                    throw GetAddressException( name ); \
                \
                return RETURN_TYPE addr; \
            }

GET_PTR( PyObject *             GetPyObjectPointer_As_PyObjectPointer       , *(PyObject **)        )
GET_PTR( PyObject *                    GetPyObject_As_PyObjectPointer       ,  (PyObject *)         )
GET_PTR( PyTypeObject *     GetPyTypeObjectPointer_As_PyTypeObjectPointer   , *(PyTypeObject**)     )
GET_PTR( PyTypeObject *            GetPyTypeObject_As_PyTypeObjectPointer   ,  (PyTypeObject*)      )
GET_PTR( int *                              GetInt_as_IntPointer            ,  (int*)               )
GET_PTR( char **                    GetCharPointer_as_CharPointerPointer    ,  (char**)             )


#   ifdef _DEBUG
static const char python_dll_name_format[] = "PYTHON%1.1d%1.1d_D.DLL";
#   else
static const char python_dll_name_format[] = "PYTHON%1.1d%1.1d.DLL";
#   endif

//--------------------------------------------------------------------------------
bool InitialisePythonIndirectInterface()
{
    char python_dll_name[sizeof(python_dll_name_format)];

    _snprintf( python_dll_name, sizeof(python_dll_name_format) / sizeof(char) - 1, python_dll_name_format, PY_MAJOR_VERSION, PY_MINOR_VERSION );

    python_dll = LoadLibraryA( python_dll_name );
    if( python_dll == nullptr )
        return false;

    try
    {
#   ifdef Py_REF_DEBUG
        ptr_Py_RefTotal             = GetInt_as_IntPointer( "_Py_RefTotal" );
#   endif
        ptr_Py_DebugFlag            = GetInt_as_IntPointer( "Py_DebugFlag" );
        ptr_Py_InteractiveFlag      = GetInt_as_IntPointer( "Py_InteractiveFlag" );
        ptr_Py_OptimizeFlag         = GetInt_as_IntPointer( "Py_OptimizeFlag" );
        ptr_Py_NoSiteFlag           = GetInt_as_IntPointer( "Py_NoSiteFlag" );
        ptr_Py_VerboseFlag          = GetInt_as_IntPointer( "Py_VerboseFlag" );
        ptr__Py_PackageContext      = GetCharPointer_as_CharPointerPointer( "_Py_PackageContext" );

#   define ASSIGN_PTR( E ) \
        ptr__Exc_##E    = GetPyObjectPointer_As_PyObjectPointer( "PyExc_" #E );
        ALL_ERRORS( ASSIGN_PTR )

        ptr__PyNone                 = GetPyObject_As_PyObjectPointer( "_Py_NoneStruct" );
        ptr__PyFalse                = GetPyObject_As_PyObjectPointer( "_Py_ZeroStruct" );
        ptr__PyTrue                 = GetPyObject_As_PyObjectPointer( "_Py_TrueStruct" );

#   define MAKE_PTR( TYPENAME ) \
        ptr__##TYPENAME##_Type      = GetPyTypeObject_As_PyTypeObjectPointer( "Py" #TYPENAME "_Type" );
        ALL_TYPES( MAKE_PTR )
    }
    catch( GetAddressException &e )
    {
        OutputDebugStringA( python_dll_name );
        OutputDebugStringA( " does not contain symbol " );
        OutputDebugStringA( e.name );
        OutputDebugStringA( "\n" );

        return false;
    }

    return true;
}


//#if 0
//#define Py_INCREF(op) (                         \
//    _Py_INC_REFTOTAL  _Py_REF_DEBUG_COMMA       \
//    ((PyObject*)(op))->ob_refcnt++)
//
//#define Py_DECREF(op)                           \
//    if (_Py_DEC_REFTOTAL  _Py_REF_DEBUG_COMMA   \
//        --((PyObject*)(op))->ob_refcnt != 0)    \
//        _Py_CHECK_REFCNT(op)                    \
//    else                                        \
//        _Py_Dealloc((PyObject *)(op))
//#endif

void _XINCREF( PyObject* op )
{
    // This function must match the contents of Py_XINCREF(op)
    if( op == nullptr )
        return;

#   ifdef Py_REF_DEBUG
    (*ptr_Py_RefTotal)++;
#   endif
    (op)->ob_refcnt++;

}

void _XDECREF( PyObject* op )
{
    // This function must match the contents of Py_XDECREF(op);
    if( op == nullptr )
        return;

#   ifdef Py_REF_DEBUG
    (*ptr_Py_RefTotal)--;
#   endif

    if (--(op)->ob_refcnt == 0)
        _Py_Dealloc((PyObject *)(op));
}


#else // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


//
//    Needed to keep the abstactions for delayload interface
//
// !!! π Py_XDECREF has been deprecated in favour of Py_CLEAR

void _XINCREF( PyObject* op )
{
    Py_XINCREF( op );
}

void _XDECREF( PyObject* op )
{
    Py_XDECREF( op );
}

#endif // PY_WIN32_DELAYLOAD_PYTHON_DLL

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


//
//    Wrap Python-types, type checks, errors, flags, etc as function calls
//

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL
#   define IF_DELAYLOAD_ELSE( A, B ) A
#else
#   define IF_DELAYLOAD_ELSE( A, B ) B
#endif


#define _Foo_Check( TYPENAME ) \
    bool _##TYPENAME##_Check( PyObject *pyob )  \
    { \
        return pyob->ob_type == _##TYPENAME##_Type(); \
    }
ALL_TYPES( _Foo_Check )

#define _Foo_Type( TYPENAME ) \
    PyTypeObject* _##TYPENAME##_Type() \
    { \
        return IF_DELAYLOAD_ELSE( ptr__##TYPENAME##_Type, & Py##TYPENAME##_Type ); \
    }
ALL_TYPES( _Foo_Type )



#define _Exc_Foo( E ) \
    PyObject* _Exc_##E() \
    { \
        return IF_DELAYLOAD_ELSE( ptr__Exc_##E, ::PyExc_##E ); \
    }
ALL_ERRORS( _Exc_Foo )


int& _Py_DebugFlag()                    { return IF_DELAYLOAD_ELSE( *ptr_Py_DebugFlag       , Py_DebugFlag ); }
int& _Py_InteractiveFlag()              { return IF_DELAYLOAD_ELSE( *ptr_Py_InteractiveFlag , Py_InteractiveFlag ); }
int& _Py_OptimizeFlag()                 { return IF_DELAYLOAD_ELSE( *ptr_Py_OptimizeFlag    , Py_OptimizeFlag ); }
int& _Py_NoSiteFlag()                   { return IF_DELAYLOAD_ELSE( *ptr_Py_NoSiteFlag      , Py_NoSiteFlag ); }
int& _Py_VerboseFlag()                  { return IF_DELAYLOAD_ELSE( *ptr_Py_VerboseFlag     , Py_VerboseFlag ); }

char* __Py_PackageContext()             { return IF_DELAYLOAD_ELSE( *ptr__Py_PackageContext , _Py_PackageContext ); }


PyObject* _None()                       { return IF_DELAYLOAD_ELSE( ptr__PyNone             , &::_Py_NoneStruct ); }
PyObject* _False()                      { return IF_DELAYLOAD_ELSE( ptr__PyFalse            , Py_False ); }
PyObject* _True()                       { return IF_DELAYLOAD_ELSE( ptr__PyTrue             , Py_True ); }

} // namespace Py

2 个答案:

答案 0 :(得分:4)

在Win32上,延迟加载是一种允许PE文件引用另一个PE文件的机制,该文件不是加载程序在文件启动时所期望的文件,或者如果它&#优雅地回退39;根本不存在。它看起来好像这适用于嵌入python本身的Windows程序,但不希望让包含python的DLL位于PATH中。

一些谷歌搜索进一步表明,这与避免python和一个由python加载的模块之间的循环有关。

答案 1 :(得分:2)

可以使用预处理器参数定义宏,这就是您无法在任何地方看到它们的原因。使用Microsoft编译器时带有/ D参数。

/ D与源代码文件开头的#define指令具有相同的效果,除了/ D在命令行上删除引号并且#define保留它们。

http://msdn.microsoft.com/en-us/library/hhzbb5c8.aspx

使用-cc检查此参考链接:

https://www.daniweb.com/software-development/c/threads/348802/passing-string-as-d-compiler-option

延迟加载是一种仅在使用它时加载库的机制,而不是在应用程序启动之前执行它的操作系统。它可以节省宝贵的内存和加载时间(取决于代码流)dll可能根本没有加载。

此代码试图自行实现延迟加载如果定义宏,则只需按正常方式执行。 Microsoft链接器可以自动为您完成工作(即您不需要编程任何东西)。请记住,这不是平台的功能,而是链接器功能。

检查此参考: http://en.wikipedia.org/wiki/Dynamic-link_library#Delayed_loading

如果需要,您可以取消代码并指示Microsoft链接器为您添加代码。

您可以使用/ DELAYLOAD参数执行此操作,如本文所述:

http://msdn.microsoft.com/en-us/library/yx9zd12s.aspx

如果找不到dll,请确保捕获正确的异常和挂钩。