471,353 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Problem with PyRun_SimpleFile and MFC Application

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(); initCORBA(); 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
Jul 18 '05 #1
8 2290
"Susanne" <su******@web.de> wrote...
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
Jul 18 '05 #2
"Susanne" <su******@web.de> schrieb im Newsbeitrag
news:18**************************@posting.google.c om...
| 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
Jul 18 '05 #3
vincent wehren wrote:
"Susanne" <su******@web.de> schrieb im Newsbeitrag
news:18**************************@posting.google.c om...
| 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.

Jul 18 '05 #4
"Mark Hammond" <mh******@skippinet.com.au> wrote...
vincent wehren wrote:
"Susanne" <su******@web.de> schrieb im Newsbeitrag
news:18**************************@posting.google.c om...
| 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
Jul 18 '05 #5
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
Jul 18 '05 #6
su******@web.de (Susanne) writes:
[...]
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.

[...]

http://www.python.org/
You have to compile the debug DLL yourself, IIRC.
John
Jul 18 '05 #7
"Susanne" <su******@web.de> schrieb im Newsbeitrag
news:18**************************@posting.google.c om...
| 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

Download the Python source (e.g.
http://www.python.org/ftp/python/2.3.2/Python-2.3.2.tgz or the version you
want to use) and untar it to disk. You will end up with a directory
"Python-2.3.2".

In this directory you will find a directory called "PCBuild" with the
relevant MSVC workspaces already nicely lined up. Look at the pythoncore and
build it in debug mode (python23_d.dll).

If you want to do extensions at some time, make sure you compile a
python_d.exe for debugging purposes too.

Regards
Vincent Wehren

(PS: ~snipped cross-post to comp.lang.c++ to avoid angry neighbors....)




Jul 18 '05 #8
Hello,
i changed Python 2.3 to Python 2.1.

The ftp of python.org contains a *.zip file in the 2.1 directory
which contains python21_d.lib etc.
I downloaded the file, and unpacked it in my python21 directory.

http://www.python.org/ftp/python/2.1...-2.1-Debug.zip

Now PyRun_SimpleFile works !!

Thanks!!!!!

Bye
Susanne
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bill Nguyen | last post: by
5 posts views Thread by lixinyi.23 | last post: by
6 posts views Thread by Tony Johansson | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.