1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 static PyObject * win32pm_spawnhandle( PyObject *self, PyObject *args )
30 PROCESS_INFORMATION pi;
31 DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
32 /* Default value as in Python sources:
33 CREATE_NEW_CONSOLE has been known to
34 cause random failures on win9x. Specifically a
36 "Your program accessed mem currently in use at xxx"
37 and a hopeful warning about the stability of your
39 Cost is Ctrl+C wont kill children, but anyone
40 who cares can have a go!
43 if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
45 if ( flags && !strcmp( flags, "-nc" ) ) // no console
48 ZeroMemory( &si, sizeof(si) );
50 ZeroMemory( &pi, sizeof(pi) );
52 if ( !CreateProcess( NULL, argv,
53 NULL, // Process handle not inheritable.
54 NULL, // Thread handle not inheritable.
56 dwProcessFlags, // Creation flags.
57 NULL, // Use parent's environment block.
58 NULL, // Use parent's starting directory.
59 &si, // Pointer to STARTUPINFO structure.
60 &pi ) ) // Pointer to PROCESS_INFORMATION structure.
62 return Py_BuildValue("i", pi.hProcess);
65 static PyObject * win32pm_spawnpid( PyObject *self, PyObject *args )
70 PROCESS_INFORMATION pi;
71 DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
72 /* Default value as in Python sources:
73 CREATE_NEW_CONSOLE has been known to
74 cause random failures on win9x. Specifically a
76 "Your program accessed mem currently in use at xxx"
77 and a hopeful warning about the stability of your
79 Cost is Ctrl+C wont kill children, but anyone
80 who cares can have a go!
83 if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
85 if ( flags && !strcmp( flags, "-nc" ) ) // no console
88 ZeroMemory( &si, sizeof(si) );
90 ZeroMemory( &pi, sizeof(pi) );
92 if ( !CreateProcess( NULL, argv,
93 NULL, // Process handle not inheritable.
94 NULL, // Thread handle not inheritable.
96 dwProcessFlags, // Creation flags.
97 NULL, // Use parent's environment block.
98 NULL, // Use parent's starting directory.
99 &si, // Pointer to STARTUPINFO structure.
100 &pi ) ) // Pointer to PROCESS_INFORMATION structure.
102 return Py_BuildValue("i", pi.dwProcessId);
105 static PyObject * win32pm_handle( PyObject *self, PyObject *args )
111 if (!PyArg_ParseTuple(args, "i|i", &argv1, &argv2))
113 ph = OpenProcess( 1, (BOOL)argv2, argv1 );
114 return Py_BuildValue("i", (int)ph );
117 static PyObject * win32pm_killpid( PyObject *self, PyObject *args )
123 if (!PyArg_ParseTuple(args, "i|i", &pid, &exitCode))
125 ph = OpenProcess( 1, 0, pid );
126 stat = TerminateProcess( ph, exitCode );
127 return Py_BuildValue("i", (int)stat );
130 static PyObject * win32pm_killhandle( PyObject *self, PyObject *args )
135 if (!PyArg_ParseTuple(args, "i|i", &phId, &exitCode))
138 stat = TerminateProcess( (void*)phId, exitCode );
139 return Py_BuildValue("i", (int)stat );
142 static PyMethodDef win32pmMethods[] = {
143 {"spawnhandle", win32pm_spawnhandle, METH_VARARGS,
144 "Creates new process. Returns windows handle of new process."},
145 {"spawnpid", win32pm_spawnpid, METH_VARARGS,
146 "Creates new process. Returns PID of new process."},
147 {"handle", win32pm_handle, METH_VARARGS,
148 "Returns windows handle of indicated process PID."},
149 {"killpid", win32pm_killpid, METH_VARARGS,
150 "Terminate process by PID."},
151 {"killhandle", win32pm_killhandle, METH_VARARGS,
152 "Terminate process by windows process handle."},
153 {NULL, NULL, 0, NULL} /* Sentinel */
159 Py_InitModule("win32pm", win32pmMethods);