Re: Running Python script from C++ code(.NET)
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); } } -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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); } } -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
In article [EMAIL PROTECTED], volcano [EMAIL PROTECTED] wrote: . . . 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? Your question confuses me. Do you realize Python can exit with sys.exit(0) or sys.exit(1) for example, to distinguish differenet exit conditions? When you do these, GetLastError() will NOT always yield 0. -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
volcano [EMAIL PROTECTED] wrote: 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/development/cpp/createprocess.html http://msdn.microsoft.com/library/default.asp?url=/library/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. -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930 hth Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930 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++:( -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Running Python script from C++ code(.NET)
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 -- http://mail.python.org/mailman/listinfo/python-list