2012-06-15 36 views
2

当我尝试使用下面的代码运行外部Python代码时,我有这个Visual C++代码以及嵌入其中的Python,当我尝试使用以下代码运行外部Python代码时,我在调试模式下得到错误:从Visual C++代码(嵌入式Python)运行Python

Unhandled exception at 0x77cf15de in CMLAir.exe (my code): 
access violation writing location 0x00000014. 

当通过C++代码调用PyRun_File函数时发生错误。
这里是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 

} 

这里是外部的Python脚本我想从C++代码运行:

import cml, results 

    def main(): 
    baseCase = "C:\\Program Files\\CMLAir32\\Examples\\Quick4_Example.cml" 
    outFile = "c:\\output.txt" 
    f = open(outFile, "w") 

    #open our .cml case 
    if cml.OpenCase(baseCase) == 0: 
    return 

    #set initial mesh size 
    meshSize = 177 
    cml.SetGridSizeX(meshSize) 
    cml.SetGridSizeY(meshSize) 
    cml.SaveCaseNoWarn() 
    while meshSize < 400: 
    #run it 
    cml.RunCaseNoWarn() 
    #get results 
    res = results.Results() 
    res.GetResults() 
    if res.HasResults() == 0: 
     cml.MsgBox("error getting results, aborting") 
     return 
    #pull out minimum fly height 
    singleResult = res[0] 
    fh = singleResult.minFH 
    #output current mesh size and minimum fly height 
    dataStr = str(meshSize) + ' ' + str(fh) 
    f.write(dataStr) 
    #increment mesh size 
    meshSize += 16 
    cml.SetGridSizeX(meshSize) 
    cml.SetGridSizeY(meshSize) 
    cml.SaveCaseNoWarn() 

    main() 

为什么PyRun_File功能给出错误?

我对C++代码中嵌入Python知之甚少,因此我非常欣赏这里的一些指针。请记住,我对Python相对来说比较陌生;我的大部分编程经验都在Visual C++中。在这种情况下将两者融合在一起的最佳方式是什么?

回答

2

错误是因为误解或C的fp ++和fp Python版本之间可能混淆,所以,我所做的是我注释掉fp线,而不是我用:

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

和工作刚刚好!