justou
V2EX  ›  C++

C DLL 中初始化 Python 虚拟机问题

  •  
  •   justou · Nov 17, 2022 · 1815 views
    This topic created in 1296 days ago, the information mentioned may be changed or developed.

    请教一个 C 中调用 Python 函数的问题。A.dll 中导出了int start_python()(调用了Py_Initialize) 和void stop_python()(调用了Py_Finalize)。B.dll 导出了一些 C 函数,这些 C 函数中调用了一些 python 函数, 这些 Python 函数是通过 cython 的 cdef public 声明再由 cython 生成的(Embedding Cython modules in C/C++ applications),目的是通过 dll 将 Python 环境隐藏。应用 a.exe 通过 loadlibrary 的方式加载了 A.dll 和 B.dll (它们都依赖于 python3.dll ):

    1. 调用 A.dll 的start_python启动 python 解释器, 然后调用 B.dll 中导出的 C 函数, 产生 access violation ;
    2. 调用 A.dll 的start_python启动 python 解释器, 尝试在 B.dll 中也调用Py_Initialize启动 python 解释器,都启动成功, 调用 B.dll 中导出的 C 函数, 产生 access violation ;
    3. 仅在 B.dll 中通过Py_Initialize启动 python 解释器,调用 B.dll 中导出的 C 函数, Ok.

    经过 debug 发现 access violation 的出现是 cython 生成的代码里某个函数指针为 NULL,然后调用了它,看上去像是 Python 虚拟机没有得到正确的初始化,导致那个函数指针也没有初始化。请问 1 、2 问题出在哪儿?

    手头暂时没有 Windows 机器,之后我再简化一个可测试的 demo 。

    justou
        1
    justou  
    OP
       Nov 18, 2022
    做了个简易 demo, 所有依赖都在里面, CLion 可以直接打开工程. 其它开发环境可能要用里面的 CMakeLists.txt 生成工程文件 https://drive.google.com/file/d/1OElQ6j8LwA-A2A45G2OROepqywmvh3Hn/view?usp=sharing
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3014 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 04:13 · PVG 12:13 · LAX 21:13 · JFK 00:13
    ♥ Do have faith in what you're doing.