[英]pybind11 conflict with GetModuleFileName
我正在使用 pybind11 让 python 调用现有的 C++ 模块(库)。 但是,在 C++ 库中,连接是通过的,当它在 C++ 中运行时,调用 ::GetModuleFileName (Visual Studio) 以确定加载模块的物理路径。 但是当我通过pybind11从python(Jupyter Notebook)调用库时,会返回python.exe的物理路径。 如何配置或更改以确保获得 C++ 库的物理路径?
C++代码是这样的:Lib.h
#pragma once
void run();
库文件
#include <fstream>
#include <stdexcept>
#include <windows.h>
#include "libloaderapi.h"
#include "Lib.h"
void run()
{
char buf[1024];
::GetModuleFileName(0, buf, sizeof(buf));
std::ofstream of;
of.open("logging.txt");
if (!of.is_open()) {
throw std::runtime_error("Cannot open logging.txt");
}
of << "The loaded module is " << buf << std::endl;
}
pybind11接口代码:Direct.cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include "Lib.h"
namespace py = pybind11;
// wrap c++ function
void wrapper() {
run();
}
PYBIND11_MODULE(run_app, m) {
// optional module docstring
m.doc() = "pybind11 plugin";
m.def("run", &wrapper, "Run C++ Application");
}
pybind11 安装文件:setup.py
#from distutils.core import setup, Extension
#from distutils import sysconfig
from setuptools import setup, Extension
import pybind11
# The following is for GCC compiler only.
#cpp_args = ['-std=c++11', '-stdlib=libc++', '-mmacosx-version-min=10.7']
cpp_args = []
sfc_module = Extension(
'run_app',
sources=['Direct.cpp',
'Lib.cpp'],
include_dirs=[pybind11.get_include(), '.'],
language='c++',
extra_compile_args=cpp_args,
)
setup(
name='run_app',
version='1.0',
description='Python package with RunApp C++ extension (PyBind11)',
ext_modules=[sfc_module],
)
构建:
python setup.py build
调用此库的 python 代码:py_run_app.py
import os
import sys
sys.path.append(os.path.realpath('build\lib.win-amd64-3.7'))
from run_app import run
run()
运行后:
python py_run_app.py
在logging.txt中加载的模块是C:....\python.exe
我想看到的是模块的物理位置。
Windows 用语中的“模块”是 DLL 或加载为进程一部分的可执行文件。 每个模块都有一个模块句柄; 按照惯例,特殊句柄NULL
表示用于创建进程的可执行文件。
GetModuleFileName
需要模块句柄作为第一个参数。 你传递0
,你会得到带有特殊句柄NULL
的模块的名称,即可执行文件。 这是完全可以预料的。
为了获得 DLL 的文件名,您需要找出它的句柄是什么。 您可以找到当前模块的句柄:
HMODULE handle;
static char local;
bool ok = GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCSTR)&local,
&handle);
local
可以是当前模块中的任何 function 或静态/外部变量。 参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.