[英]Calling a Windows app that requires input via a GUI using Python 2.7 script: I/O handling
[英]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, world\n"); // 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运行时库已在[APP_NAME] .exe中检测到致命错误”。),而且是Python即使上面的输入块没有错误运行,标准输入仍然不起作用。
您的程序需要使用与要嵌入的Python版本相同的Microsoft C运行时DLL。 Python 2.7版编译与Visual Studio 2008,并使用MSVCRT90.DLL
,而你正在使用Visual Studio 2010和MSVCRT100.DLL
。 每个DLL都有自己的stdin
, stdout
和stderr
,这就是为什么create_console
函数无效的原因。 这两个DLL也具有不同的FILE *
流内部布局,这就是当您将使用MSVCRT100.DLL
创建的FILE *
流传MSVCRT100.DLL
Python时崩溃的原因。 最终尝试与MSVCRT90.DLL
一起使用它。
基本上要解决此问题,您需要使用Visual Studio 2008编译应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.