Reputation: 51
I have this Visual C++ code, plus Python embedded in it, when I try to run external Python code using the below code, I get the error in the Debug mode:
Unhandled exception at 0x77cf15de in CMLAir.exe (my code):
access violation writing location 0x00000014.
The error is happening when PyRun_File function gets called via c++ code.
Here is the C++ function:
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
}
here is the external Python script I am trying to run from C++ code:
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()
Why does the PyRun_File function give the error?
I don't know much about embedding Python inside C++ code, so I'd really appreciate some pointers here. Keep in mind I'm relatively new to Python at all; most of my programming experience is in Visual C++. What would be the best way to fit the two together in this situation?
Upvotes: 2
Views: 1739
Reputation: 51
The error was because of misunderstanding or maybe confusion between fp
of C++ and python version of fp
, so, what I did is I commented out the fp
line, instead I used:
PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r");
and that worked just perfect!
Upvotes: 2