如何将嵌入式 Python 连接到 Windows GUI 应用程序中控制台的 I/O?
How can I connect embedded Python to the console's I/O in a Windows GUI app?
我正在Microsoft Visual Studio 2010中用C++制作一个Windows GUI应用程序。我想包括一个可以通过应用程序打开的Python调试控制台。我按照这个答案将C++标准I/O连接到控制台,但当我嵌入Python时,Python似乎无法通过其标准I/O访问控制台:
create_console(); // essentially the function in the answer I linked to above
Py_Initialize(); // initialize Python
printf("hello, worldn"); // works
PyRun_SimpleString("print "hello, world""); // does not work
我试着用以下方法修补它:
PyObject *py_stdin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
PySys_SetObject("stdin", py_stdin);
Py_DECREF(py_stdin); py_stdin = NULL;
PyObject *py_stdout = PyFile_FromFile(stdout, "<stdout>", "w", NULL); // *
PySys_SetObject("stdout", py_stdout);
Py_DECREF(py_stdout); py_stdout = NULL;
PyObject *py_stderr = PyFile_FromFile(stderr, "<stderr>", "w", NULL); // *
PySys_SetObject("stderr", py_stderr);
Py_DECREF(py_stderr); py_stderr = NULL;
但是,上面用星号(*(标记的行不仅会导致运行时错误(错误消息无非是"Microsoft Visual Studio C runtime Library在[APP_NAME].exe中检测到致命错误"(,而且Python的标准输入仍然无法工作,即使上面的输入块运行时没有错误。
您的程序需要使用与您试图嵌入的Python版本相同的Microsoft C运行时DLL。Python 2.7是使用Visual Studio 2008编译的,在使用Visual Studio 2010和MSVCRT100.DLL
时使用MSVCRT90.DLL
。每个DLL都有自己的stdin
、stdout
和stderr
,这就是为什么create_console
函数没有效果的原因。这两个DLL也有不同的FILE *
流内部布局,这就是为什么当您将使用MSVCRT100.DLL
创建的FILE *
流传递给Python时,它会崩溃。它最终尝试将其与MSVCRT90.DLL
一起使用。
基本上要解决这个问题,您需要使用Visual Studio 2008编译应用程序。
相关文章:
- C++控制台应用程序阻止退出
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 如何创建一个使用开源c项目的c++控制台应用程序
- c ++控制台应用程序,如何在控制台外部打印字符
- 将控制台应用程序直接输出到Visual Studio而不是外部Windows控制台中
- 如何在 Win32 控制台应用程序中注册不可见的窗口类?
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 直接输入控制台应用程序
- C++调试控制台应用程序
- C++控制台应用程序调用C++/CLI静态库
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 如何在将Visual Studio C++控制台应用程序连接到Microsoft SQL Server时修复"
- 如何读取windows服务启动的控制台应用程序的输出
- VS2017 中缺少 Win32 控制台应用程序.如何创建C++空项目?
- 将 Visual C++ 6 控制台应用程序作为 Windows 服务运行
- 如何将控制台应用程序放入系统托盘通知C++?
- 如何向控制台应用程序添加图标
- 如何在 Linux/Apache 虚拟主机上运行 win32 控制台应用程序?