如何制作自动将dll复制到用户项目根文件夹的python模块?

时间:2019-02-26 17:22:19

标签: python c++

所以我有一个c ++程序,它依赖于dll(可称为mtmanapi64.dll)和头文件。然后,我使用c ++ boost.python创建了一个python模块,以便可以在python中调用头文件函数。

问题是每次为了让我调用c ++函数时,我实际上需要将该mtmanapi64.dll文件复制到python项目的根文件夹中。我想知道我是否可以在python模块项目中编写该文件,以便当用户安装模块(或导入模块)时,它将自动将dll复制到项目根文件夹(或在python site-package中查找dll)文件夹?)

python模块项目结构:

|   .gitignore
|   example.py
|   install.bat
|   MANIFEST.in
|   mtmanapi64.dll
|   requirements.txt
|   setup.py
|   symbols.raw
|   symgroups.raw
+---.idea          
+---env            
+---logs
\---py_mt4manapi
    |   boost_locale-vc141-mt-x64-1_67.dll
    |   boost_python36-vc141-mt-x64-1_67.dll
    |   boost_system-vc141-mt-x64-1_67.dll
    |   cppMt4ManagerApi.exp
    |   cppMt4ManagerApi.iobj
    |   cppMt4ManagerApi.ipdb
    |   cppMt4ManagerApi.lib
    |   cppMt4ManagerApi.pdb
    |   cppMt4ManagerApi.pyd
    |   cppMt4ManagerApi.pyd.lastcodeanalysissucceeded
    |   demo.py
    |   mt4_creds.ini
    |   python36.dll
    |   py_mt4manapi.py
    |   py_mt4_position_helper.py
    |   __init__.py
    |   

根目录中dll文件的C ++代码请求:

class CManager
{
    CManagerFactory   m_factory;
    CManagerInterface *m_manager;
#ifndef _WIN64
    LPCSTR dll_path = "mtmanapi.dll";
#else
    LPCSTR dll_path = "mtmanapi64.dll";
#endif


public:
    CManager() : m_factory(dll_path), m_manager(NULL)
    {

        char cCurrentPath[FILENAME_MAX];
        if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
        {
            printf("current path error.");
        }
        cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
        printf("The current working directory is %s\n", cCurrentPath);
        m_factory.WinsockStartup();
        if (m_factory.IsValid() == FALSE || (m_manager = m_factory.Create(ManAPIVersion)) == NULL)
        {
            printf("Failed to create MetaTrader 4 Manager API interface\n");
            return;
        }
    }

    ~CManager()
    {
        if (m_manager != NULL)
        {
            if (m_manager->IsConnected())
                m_manager->Disconnect();
            m_manager->Release();
            m_manager = NULL;
        }
        m_factory.WinsockCleanup();
    }

0 个答案:

没有答案