473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Running Python script from C++ code(.NET)

Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!

Sep 23 '06 #1
11 3204

volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
In C#:

http://groups.google.com/group/comp....7b6a89095ef930

hth

Gerard

Sep 23 '06 #2

Gerard Flanagan wrote:
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!

In C#:

http://groups.google.com/group/comp....7b6a89095ef930

hth

Gerard
Thanks for fast response, alas - it did not!
My problem is - application in C++ used to use external application,
which does not work well. So I sort of reproduced the functionality in
Python script, but now I am stuck, unable to run it properly.

Sep 23 '06 #3

volcano wrote:
Gerard Flanagan wrote:
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.
>
And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
In C#:

http://groups.google.com/group/comp....7b6a89095ef930

hth

Gerard

Thanks for fast response, alas - it did not!
My problem is - application in C++ used to use external application,
which does not work well. So I sort of reproduced the functionality in
Python script, but now I am stuck, unable to run it properly.
Maybe my understanding is wrong, but can't managed (.NET) C++ call into
any other managed assembly, in this case (I think) System.Diagnostics?

Gerard

Sep 23 '06 #4

Gerard Flanagan wrote:
volcano wrote:
Gerard Flanagan wrote:
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
>
In C#:
>
http://groups.google.com/group/comp....7b6a89095ef930
>
hth
>
Gerard
Thanks for fast response, alas - it did not!
My problem is - application in C++ used to use external application,
which does not work well. So I sort of reproduced the functionality in
Python script, but now I am stuck, unable to run it properly.

Maybe my understanding is wrong, but can't managed (.NET) C++ call into
any other managed assembly, in this case (I think) System.Diagnostics?

Gerard
My application is written in regular C++:(

Sep 23 '06 #5
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
System::Diagnostics::Process::Start

Sep 24 '06 #6
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
System::Diagnostics::Process::Start

Sep 24 '06 #7
volcano <Ma*********@gmail.comwrote:
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.
In a unix based environment I would use fork + exec*. IIRC in Windows
you should have a CreateProcess function call.

<http://goffconcepts.com/techarticles...teprocess.html
>
<http://msdn.microsoft.com/library/de...y/en-us/dllpro
c/base/createprocess.asp>

You could also try system, but it's generally considered insecure.

--
blog: http://www.akropolix.net/rik0/blogs | Uccidete i filosofi,
site: http://www.akropolix.net/rik0/ | tenetevi riso e
forum: http://www.akropolix.net/forum/ | bacchette per voi.
Sep 24 '06 #8
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!
Thanks a lot to all of you who cared to answer! Eventually it was
"::CreateProcess", and it works!

But here is another question for gurus: sometimes my script fails, and
I cannot figure out why. OK, I can - especially since I terminate it
with "sys.exit()", but I want my app to know too.
"GetLastError" returns 0 - for the obvious reason that this is the
value Python interpreter returns with. But how can I get the script
return value?

Sep 26 '06 #9
In article <11*********************@i3g2000cwc.googlegroups.c om>,
volcano <Ma*********@gmail.comwrote:
Sep 26 '06 #10

volcano wrote:
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!

Thanks a lot to all of you who cared to answer! Eventually it was
"::CreateProcess", and it works!
I used to find http://pinvoke.net a good resource when I was doing
Active Directory stuff. Just for interest's sake, cutting and pasting
from the CreateProcess page, the following arcana will start Python (
again from C#, but here you can see the internals at least):

using System;
using System.Runtime.InteropServices;

class Class1
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct STARTUPINFO
{
Int32 cb;
string lpReserved;
string lpDesktop;
string lpTitle;
Int32 dwX;
Int32 dwY;
Int32 dwXSize;
Int32 dwYSize;
Int32 dwXCountChars;
Int32 dwYCountChars;
Int32 dwFillAttribute;
Int32 dwFlags;
Int16 wShowWindow;
Int16 cbReserved2;
IntPtr lpReserved2;
IntPtr hStdInput;
IntPtr hStdOutput;
IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}

[DllImport("kernel32.dll")]
public static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string
lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

[STAThread]
static void Main(string[] args)
{
const uint NORMAL_PRIORITY_CLASS = 0x0020;

bool retValue;
string Application = @"c:\python\python24\python.exe";
string CommandLine = "";
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);

retValue = CreateProcess(Application,CommandLine,
ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
IntPtr.Zero,null,ref sInfo,out pInfo);

}
}

Sep 27 '06 #11

volcano wrote:
volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS!!!!

Thanks a lot to all of you who cared to answer! Eventually it was
"::CreateProcess", and it works!
I used to find http://pinvoke.net a good resource when I was doing
Active Directory stuff. Just for interest's sake, cutting and pasting
from the CreateProcess page, the following arcana will start Python (
again from C#, but here you can see the internals at least):

using System;
using System.Runtime.InteropServices;

class Class1
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct STARTUPINFO
{
Int32 cb;
string lpReserved;
string lpDesktop;
string lpTitle;
Int32 dwX;
Int32 dwY;
Int32 dwXSize;
Int32 dwYSize;
Int32 dwXCountChars;
Int32 dwYCountChars;
Int32 dwFillAttribute;
Int32 dwFlags;
Int16 wShowWindow;
Int16 cbReserved2;
IntPtr lpReserved2;
IntPtr hStdInput;
IntPtr hStdOutput;
IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}

[DllImport("kernel32.dll")]
public static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string
lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

[STAThread]
static void Main(string[] args)
{
const uint NORMAL_PRIORITY_CLASS = 0x0020;

bool retValue;
string Application = @"c:\python\python24\python.exe";
string CommandLine = "";
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);

retValue = CreateProcess(Application,CommandLine,
ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
IntPtr.Zero,null,ref sInfo,out pInfo);

}
}

Sep 27 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Aahz | last post by:
There seems to be some kind of problem with Starship's DNS. Until it's fixed, please use the direct IP of 217.160.219.194 -- Aahz (aahz@pythoncraft.com) <*> ...
5
by: Samuel Schulenburg | last post by:
Who should I contact about accessing my account on Starship. I need to obtain a new password and access process. I think i am having a memory lapse as it has been over a year sense I have had a...
2
by: Greg | last post by:
The following article from microsoft describes a workaroind for a bug in hte beta version of VISUAL PYTHON DOT NET ...
6
by: Ajay | last post by:
hi! Is it possible for me to have part of my application written using C#.NET and then have it called from within a Python program. thanks cheers
1
by: Harlin | last post by:
Has anyone had any success using py2exe with anything you've written using the Python .NET implementation? (This is the one from the Zope website -- not ActiveState). I seem to be having trouble...
0
by: Oleg Medyanik | last post by:
Hello, All I have a question regarding running .NET application on W2k SP4 server in Terminal Service client, After we installed SP4 we face the exception when closing .NET app in Terminal...
10
by: Tobias Nilsson | last post by:
I have almost exactly the same problem as the guy below. Is it NOT possible running .NET code locally in Internet Explorer WITHOUT a webserver???? / Tobias --------- My company has in the...
4
by: Juba | last post by:
Hello, I've been having trouble running my web application developed in .NET with C# on Macs. It looks normal on PCs but Mac users are complaining that everything looks scrambled on the screen!...
0
by: T. Bryan | last post by:
We are now ready to start re-enabling user accounts on starship.python.net. If you had an account on the machine, then you should have received an e-mail about it already. If you didn't, you...
4
by: Tom Bryan | last post by:
One of the system administrators had to reboot starship.python.net last night, but it appears that the machine did not come back up properly. starship.python.net is currently down while we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.