Problem with PyRun_SimpleFile and MFC Application

S

Susanne

Hello!

In have embedded python into my MFC application.
It works fine using the PyRun_SimpleString method, giving it a string with
some simple commands.

When i am using PyRun_SimpleFile instead, my application crashes.

I have also created a console application. There i also call PyRun_SimpleFile.
The console application executes the given script.

My code looks like the following:

**********************************
MFC-Application:
**********************************
Py_SetProgramName("testclient");
/* Initialize the Python interpreter. Required. */
Py_Initialize();

curMode = DEBUG_MODE;
//**** is running!!!!*****
//CString command = "import CORBA; CORBA.SetTPData();";
//PyRun_SimpleString(command);
//CORBA is my python module, SetTPData a method of this modul
//in SetTPData i open an AfxMessageBox, so thats why i know that
//it works
//path of my file
char * filename2 = "C:\\cvsroot\\OCI-TestClient\\projects\\main\\tao\\test.py";
FILE *fp =fopen(filename2,"r");
int result;
if(fp !=NULL)
{
//does not work :-(
result = PyRun_SimpleFile(fp,filename2);
fclose(fp);
}
Py_Finalize();

****************************************
Now the code of the console application:
****************************************
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
//create instance of my class
PythonExtension * pythi = new PythonExtension();
//init my module (not the same module like in MFC app)
pythi->initspam();

char * filename2 = "C:\\cvsroot\\OCI-TestClient\\projects\\main\\tao\\test.py";
FILE *fp =fopen(filename2,"r");
int result;
//works fine, only says, that there is no modul called CORBA
//see below
result = PyRun_SimpleFile(fp,filename2);
printf("Result: %d",result);

**********************
My python script file:
**********************
import CORBA;
CORBA.SetTPData();

************************

So, does anyone of you have an idea, what i am doing wrong in my mfc app?

Susanne
 
V

Victor Bazarov

Susanne said:
In have embedded python into my MFC application.
It works fine using the PyRun_SimpleString method, giving it a string with
some simple commands.

When i am using PyRun_SimpleFile instead, my application crashes.

I have also created a console application. There i also call PyRun_SimpleFile.
The console application executes the given script.

My code looks like the following:
[...]

So, does anyone of you have an idea, what i am doing wrong in my mfc app?

Somebody in a relevant newsgroup probably does. comp.lang.c++ is
NOT one of them. Perhaps along with the Python one that you used,
an MFC newsgroup can help. comp.os.ms-windows.programmer.tools.mfc
and microsoft.public.vc.mfc are the two that spring to mind.

Victor
 
V

vincent wehren

| Hello!

|
| In have embedded python into my MFC application.
| It works fine using the PyRun_SimpleString method, giving it a string with
| some simple commands.
|
| When i am using PyRun_SimpleFile instead, my application crashes.
|
| I have also created a console application. There i also call
PyRun_SimpleFile.
| The console application executes the given script.
|

<Code snipped> >//works fine, only says, that there is no modul called
CORBA

|
| So, does anyone of you have an idea, what i am doing wrong in my mfc app?


PyRun_SimpleFile() reportedly (and I can second that) when certain compiler
flags are different from the pythonxx(_d).dll. Try setting your Project
Settings -> C/C++, Code Generation -> "Use runtime library" - to "(Debug)
Multithreaded DLL"

(Don't know if or how this affects any of your MFC stuff though)

Hope this helps,

Vincent Wehren

|
| Susanne
 
M

Mark Hammond

vincent said:
| Hello!

|
| In have embedded python into my MFC application.
| It works fine using the PyRun_SimpleString method, giving it a string with
| some simple commands.
|
| When i am using PyRun_SimpleFile instead, my application crashes.
|
| I have also created a console application. There i also call
PyRun_SimpleFile.
| The console application executes the given script.
|

<Code snipped> >//works fine, only says, that there is no modul called
CORBA

|
| So, does anyone of you have an idea, what i am doing wrong in my mfc app?


PyRun_SimpleFile() reportedly (and I can second that) when certain compiler
flags are different from the pythonxx(_d).dll. Try setting your Project
Settings -> C/C++, Code Generation -> "Use runtime library" - to "(Debug)
Multithreaded DLL"

(Don't know if or how this affects any of your MFC stuff though)

Yes, this will be the OPs issue. However, you don't always use the
debug CRT - the important thing is to use the *same* CRT as Python
itself. So a release build of your app/extension *must* be the
non-debug in a DLL, and a debug build must use the debug in a DLL.

Mark.
 
V

Victor Bazarov

Mark Hammond said:
vincent said:
| Hello!

[...]
PyRun_SimpleFile() reportedly (and I can second that) when certain compiler
flags are different from the pythonxx(_d).dll. [...]

(Don't know if or how this affects any of your MFC stuff though)

Yes, this will be the OPs issue. However, you don't always use the
debug CRT - the important thing is [...]

This is all OT in comp.lang.c++. Please trim your cross-posting.
[follow-ups set to Python group only]

Victor
 
S

Susanne

Hello together,

after reading your comments, i looked, which configuration
im using in my console app. Its a release app.
Than i tried to run it as a debug app and now the
programm crashes like my mfc app.

So the hint with the debug dll was good.

But how can i get this dll?
I don't have the python source code and i also
can't find it in the internet.
So there is no way to create it by myself.

Can anyone of you help me again??

Thanks
Susanne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top