Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从visualc+运行Python+;代码(嵌入式Python) 我有这个VisualC++代码,加上Python嵌入其中,当我尝试使用下面的代码运行外部Python代码时,我在调试模式中得到了错误: Unhandled exception at 0x77cf15de in CMLAir.exe (my code): access violation writing location 0x00000014. 错误发生在PyRungField函数通过C++代码调用时发生。 这里是C++函数: void CRailtestDoc::OnPreprocessorRunscript() { #ifdef USE_PYTHON //for now, pull up Open dialog for user to specify script CString strPythonScriptPath; CString strTitle = "Run Python Script"; CFileDialog dlg(TRUE, ".py", NULL); dlg.m_ofn.Flags |= OFN_PATHMUSTEXIST; dlg.m_ofn.lpstrTitle = (LPCTSTR)strTitle; if (dlg.DoModal() == IDOK) { strPythonScriptPath = dlg.GetFileName( ); } else { return; } FILE* fp; fp = fopen((LPCSTR)strPythonScriptPath, "r+"); if (fp == NULL) { CString strErrMsg; strErrMsg.Format("troble opening python file: %s", (LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); return; } // // TODO: we need to make sure that we don't call Py_Initialize more than once // see Python/C API Reference Manual section 1.4 // m_bRunningScript = TRUE; Py_Initialize(); PycString_IMPORT; //Macro for importing cStringIO objects //start up our own modules? Is this needed here? initcml(); initresults(); PyObject* localDict; PyObject* mainModule; PyObject* mainModuleDict; mainModule = PyImport_AddModule("__main__"); //mainModule = PyImport_AddModule("__builtins__"); if(mainModule==NULL) { AfxMessageBox("Problems running script"); m_bRunningScript = FALSE; return; } mainModuleDict = PyModule_GetDict(mainModule); localDict = PyDict_New(); //Now run the selected script PyObject* tempPyResult = PyRun_File(fp, strPythonScriptPath, Py_file_input, mainModuleDict, mainModuleDict); //<=== where the code exit with unhandled exception at 0x77cf15de UNREFERENCED_PARAMETER(tempPyResult); // See if an exception was raised and not handled. // If so, return traceback to user as MsgBox } void CRailtestDoc::OnPreprocessorRunscript() { #ifdef使用PYTHON //现在,拉上“打开”对话框供用户指定脚本 CString strPythonScriptPath; CString strTitle=“运行Python脚本”; CFileDialog dlg(TRUE,.py,.NULL); dlg.m_of n.Flags |=of n_路径必须存在; dlg.m_of n.lpstrTitle=(LPCTSTR)strTitle; if(dlg.DoModal()==IDOK) { strPythonScriptPath=dlg.GetFileName(); } 其他的 { 返回; } 文件*fp; fp=fopen((LPCSTR)strPythonScriptPath,“r+”); 如果(fp==NULL) { CString-strErrMsg; Format(“troble打开python文件:%s”,(LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); 返回; } // //TODO:我们需要确保不会多次调用Py_Initialize //请参阅Python/C API参考手册第1.4节 // m_bRunningScript=真; Py_初始化(); PycString_IMPORT;//用于导入cStringIO对象的宏 //启动我们自己的模块?这里需要吗? initcml(); initresults(); PyObject*localDict; PyObject*main模块; PyObject*main模块指令; mainModule=PyImport\u AddModule(“\uuuu main\uuuu”); //mainModule=PyImport\u AddModule(“\uuuu内置模块”); if(mainModule==NULL) { AfxMessageBox(“运行脚本时出现问题”); m_bRunningScript=FALSE; 返回; } mainmoduledit=PyModule\u GetDict(mainModule); localDict=PyDict_New(); //现在运行所选脚本 PyObject*tempPyResult= PyRun_文件(fp、strpythonscript路径、Py_文件输入、mainModuleDict、mainModuleDict); //p>错误是因为C++的代码> FP和Python版本的代码> FP之间的混淆,所以,我所做的是注释 FP >行,而不是用: PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");_Python_Visual C++_Python C Api_Python Embedding - Fatal编程技术网 错误是因为C++的代码> FP和Python版本的代码> FP之间的混淆,所以,我所做的是注释 FP >行,而不是用: PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");,python,visual-c++,python-c-api,python-embedding,Python,Visual C++,Python C Api,Python Embedding" /> 错误是因为C++的代码> FP和Python版本的代码> FP之间的混淆,所以,我所做的是注释 FP >行,而不是用: PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");,python,visual-c++,python-c-api,python-embedding,Python,Visual C++,Python C Api,Python Embedding" />

从visualc+运行Python+;代码(嵌入式Python) 我有这个VisualC++代码,加上Python嵌入其中,当我尝试使用下面的代码运行外部Python代码时,我在调试模式中得到了错误: Unhandled exception at 0x77cf15de in CMLAir.exe (my code): access violation writing location 0x00000014. 错误发生在PyRungField函数通过C++代码调用时发生。 这里是C++函数: void CRailtestDoc::OnPreprocessorRunscript() { #ifdef USE_PYTHON //for now, pull up Open dialog for user to specify script CString strPythonScriptPath; CString strTitle = "Run Python Script"; CFileDialog dlg(TRUE, ".py", NULL); dlg.m_ofn.Flags |= OFN_PATHMUSTEXIST; dlg.m_ofn.lpstrTitle = (LPCTSTR)strTitle; if (dlg.DoModal() == IDOK) { strPythonScriptPath = dlg.GetFileName( ); } else { return; } FILE* fp; fp = fopen((LPCSTR)strPythonScriptPath, "r+"); if (fp == NULL) { CString strErrMsg; strErrMsg.Format("troble opening python file: %s", (LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); return; } // // TODO: we need to make sure that we don't call Py_Initialize more than once // see Python/C API Reference Manual section 1.4 // m_bRunningScript = TRUE; Py_Initialize(); PycString_IMPORT; //Macro for importing cStringIO objects //start up our own modules? Is this needed here? initcml(); initresults(); PyObject* localDict; PyObject* mainModule; PyObject* mainModuleDict; mainModule = PyImport_AddModule("__main__"); //mainModule = PyImport_AddModule("__builtins__"); if(mainModule==NULL) { AfxMessageBox("Problems running script"); m_bRunningScript = FALSE; return; } mainModuleDict = PyModule_GetDict(mainModule); localDict = PyDict_New(); //Now run the selected script PyObject* tempPyResult = PyRun_File(fp, strPythonScriptPath, Py_file_input, mainModuleDict, mainModuleDict); //<=== where the code exit with unhandled exception at 0x77cf15de UNREFERENCED_PARAMETER(tempPyResult); // See if an exception was raised and not handled. // If so, return traceback to user as MsgBox } void CRailtestDoc::OnPreprocessorRunscript() { #ifdef使用PYTHON //现在,拉上“打开”对话框供用户指定脚本 CString strPythonScriptPath; CString strTitle=“运行Python脚本”; CFileDialog dlg(TRUE,.py,.NULL); dlg.m_of n.Flags |=of n_路径必须存在; dlg.m_of n.lpstrTitle=(LPCTSTR)strTitle; if(dlg.DoModal()==IDOK) { strPythonScriptPath=dlg.GetFileName(); } 其他的 { 返回; } 文件*fp; fp=fopen((LPCSTR)strPythonScriptPath,“r+”); 如果(fp==NULL) { CString-strErrMsg; Format(“troble打开python文件:%s”,(LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); 返回; } // //TODO:我们需要确保不会多次调用Py_Initialize //请参阅Python/C API参考手册第1.4节 // m_bRunningScript=真; Py_初始化(); PycString_IMPORT;//用于导入cStringIO对象的宏 //启动我们自己的模块?这里需要吗? initcml(); initresults(); PyObject*localDict; PyObject*main模块; PyObject*main模块指令; mainModule=PyImport\u AddModule(“\uuuu main\uuuu”); //mainModule=PyImport\u AddModule(“\uuuu内置模块”); if(mainModule==NULL) { AfxMessageBox(“运行脚本时出现问题”); m_bRunningScript=FALSE; 返回; } mainmoduledit=PyModule\u GetDict(mainModule); localDict=PyDict_New(); //现在运行所选脚本 PyObject*tempPyResult= PyRun_文件(fp、strpythonscript路径、Py_文件输入、mainModuleDict、mainModuleDict); //p>错误是因为C++的代码> FP和Python版本的代码> FP之间的混淆,所以,我所做的是注释 FP >行,而不是用: PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");

从visualc+运行Python+;代码(嵌入式Python) 我有这个VisualC++代码,加上Python嵌入其中,当我尝试使用下面的代码运行外部Python代码时,我在调试模式中得到了错误: Unhandled exception at 0x77cf15de in CMLAir.exe (my code): access violation writing location 0x00000014. 错误发生在PyRungField函数通过C++代码调用时发生。 这里是C++函数: void CRailtestDoc::OnPreprocessorRunscript() { #ifdef USE_PYTHON //for now, pull up Open dialog for user to specify script CString strPythonScriptPath; CString strTitle = "Run Python Script"; CFileDialog dlg(TRUE, ".py", NULL); dlg.m_ofn.Flags |= OFN_PATHMUSTEXIST; dlg.m_ofn.lpstrTitle = (LPCTSTR)strTitle; if (dlg.DoModal() == IDOK) { strPythonScriptPath = dlg.GetFileName( ); } else { return; } FILE* fp; fp = fopen((LPCSTR)strPythonScriptPath, "r+"); if (fp == NULL) { CString strErrMsg; strErrMsg.Format("troble opening python file: %s", (LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); return; } // // TODO: we need to make sure that we don't call Py_Initialize more than once // see Python/C API Reference Manual section 1.4 // m_bRunningScript = TRUE; Py_Initialize(); PycString_IMPORT; //Macro for importing cStringIO objects //start up our own modules? Is this needed here? initcml(); initresults(); PyObject* localDict; PyObject* mainModule; PyObject* mainModuleDict; mainModule = PyImport_AddModule("__main__"); //mainModule = PyImport_AddModule("__builtins__"); if(mainModule==NULL) { AfxMessageBox("Problems running script"); m_bRunningScript = FALSE; return; } mainModuleDict = PyModule_GetDict(mainModule); localDict = PyDict_New(); //Now run the selected script PyObject* tempPyResult = PyRun_File(fp, strPythonScriptPath, Py_file_input, mainModuleDict, mainModuleDict); //<=== where the code exit with unhandled exception at 0x77cf15de UNREFERENCED_PARAMETER(tempPyResult); // See if an exception was raised and not handled. // If so, return traceback to user as MsgBox } void CRailtestDoc::OnPreprocessorRunscript() { #ifdef使用PYTHON //现在,拉上“打开”对话框供用户指定脚本 CString strPythonScriptPath; CString strTitle=“运行Python脚本”; CFileDialog dlg(TRUE,.py,.NULL); dlg.m_of n.Flags |=of n_路径必须存在; dlg.m_of n.lpstrTitle=(LPCTSTR)strTitle; if(dlg.DoModal()==IDOK) { strPythonScriptPath=dlg.GetFileName(); } 其他的 { 返回; } 文件*fp; fp=fopen((LPCSTR)strPythonScriptPath,“r+”); 如果(fp==NULL) { CString-strErrMsg; Format(“troble打开python文件:%s”,(LPCSTR)strPythonScriptPath); AfxMessageBox(strErrMsg); 返回; } // //TODO:我们需要确保不会多次调用Py_Initialize //请参阅Python/C API参考手册第1.4节 // m_bRunningScript=真; Py_初始化(); PycString_IMPORT;//用于导入cStringIO对象的宏 //启动我们自己的模块?这里需要吗? initcml(); initresults(); PyObject*localDict; PyObject*main模块; PyObject*main模块指令; mainModule=PyImport\u AddModule(“\uuuu main\uuuu”); //mainModule=PyImport\u AddModule(“\uuuu内置模块”); if(mainModule==NULL) { AfxMessageBox(“运行脚本时出现问题”); m_bRunningScript=FALSE; 返回; } mainmoduledit=PyModule\u GetDict(mainModule); localDict=PyDict_New(); //现在运行所选脚本 PyObject*tempPyResult= PyRun_文件(fp、strpythonscript路径、Py_文件输入、mainModuleDict、mainModuleDict); //p>错误是因为C++的代码> FP和Python版本的代码> FP之间的混淆,所以,我所做的是注释 FP >行,而不是用: PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");,python,visual-c++,python-c-api,python-embedding,Python,Visual C++,Python C Api,Python Embedding,而且效果非常好 PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");

而且效果非常好

PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");