By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a

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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.