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

2024-04-26 18:12:00 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个c++程序,它依赖于一个dll(我们称之为mtmanapi64.dll)和一个头文件。然后我用c++制作了一个python模块增强.python这样我就可以在python中调用头文件函数。你知道吗

问题是每次为了调用c++函数,我都需要将mtmanapi64.dll文件复制到python项目的根文件夹中。我想知道我是否可以在python模块项目中编写代码,这样当用户安装模块(或导入模块)时,它会自动将dll复制到项目根文件夹(或在python站点包文件夹中查找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();
    }

Tags: 模块path项目py文件夹iffactorymanager