1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 static PyObject * win32pm_spawnhandle( PyObject *self, PyObject *args )
28 PROCESS_INFORMATION pi;
29 DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
30 /* Default value as in Python sources:
31 CREATE_NEW_CONSOLE has been known to
32 cause random failures on win9x. Specifically a
34 "Your program accessed mem currently in use at xxx"
35 and a hopeful warning about the stability of your
37 Cost is Ctrl+C wont kill children, but anyone
38 who cares can have a go!
41 if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
43 if ( flags && !strcmp( flags, "-nc" ) ) // no console
46 ZeroMemory( &si, sizeof(si) );
48 ZeroMemory( &pi, sizeof(pi) );
50 if ( !CreateProcess( NULL, argv,
51 NULL, // Process handle not inheritable.
52 NULL, // Thread handle not inheritable.
54 dwProcessFlags, // Creation flags.
55 NULL, // Use parent's environment block.
56 NULL, // Use parent's starting directory.
57 &si, // Pointer to STARTUPINFO structure.
58 &pi ) ) // Pointer to PROCESS_INFORMATION structure.
60 return Py_BuildValue("i", pi.hProcess);
63 static PyObject * win32pm_spawnpid( PyObject *self, PyObject *args )
68 PROCESS_INFORMATION pi;
69 DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
70 /* Default value as in Python sources:
71 CREATE_NEW_CONSOLE has been known to
72 cause random failures on win9x. Specifically a
74 "Your program accessed mem currently in use at xxx"
75 and a hopeful warning about the stability of your
77 Cost is Ctrl+C wont kill children, but anyone
78 who cares can have a go!
81 if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
83 if ( flags && !strcmp( flags, "-nc" ) ) // no console
86 ZeroMemory( &si, sizeof(si) );
88 ZeroMemory( &pi, sizeof(pi) );
90 if ( !CreateProcess( NULL, argv,
91 NULL, // Process handle not inheritable.
92 NULL, // Thread handle not inheritable.
94 dwProcessFlags, // Creation flags.
95 NULL, // Use parent's environment block.
96 NULL, // Use parent's starting directory.
97 &si, // Pointer to STARTUPINFO structure.
98 &pi ) ) // Pointer to PROCESS_INFORMATION structure.
100 return Py_BuildValue("i", pi.dwProcessId);
103 static PyObject * win32pm_handle( PyObject *self, PyObject *args )
109 if (!PyArg_ParseTuple(args, "i|i", &argv1, &argv2))
111 ph = OpenProcess( 1, (BOOL)argv2, argv1 );
112 return Py_BuildValue("i", (int)ph );
115 static PyObject * win32pm_killpid( PyObject *self, PyObject *args )
121 if (!PyArg_ParseTuple(args, "i|i", &pid, &exitCode))
123 ph = OpenProcess( 1, 0, pid );
124 stat = TerminateProcess( ph, exitCode );
125 return Py_BuildValue("i", (int)stat );
128 static PyObject * win32pm_killhandle( PyObject *self, PyObject *args )
133 if (!PyArg_ParseTuple(args, "i|i", &phId, &exitCode))
136 stat = TerminateProcess( (void*)phId, exitCode );
137 return Py_BuildValue("i", (int)stat );
140 static PyMethodDef win32pmMethods[] = {
141 {"spawnhandle", win32pm_spawnhandle, METH_VARARGS,
142 "Creates new process. Returns windows handle of new process."},
143 {"spawnpid", win32pm_spawnpid, METH_VARARGS,
144 "Creates new process. Returns PID of new process."},
145 {"handle", win32pm_handle, METH_VARARGS,
146 "Returns windows handle of indicated process PID."},
147 {"killpid", win32pm_killpid, METH_VARARGS,
148 "Terminate process by PID."},
149 {"killhandle", win32pm_killhandle, METH_VARARGS,
150 "Terminate process by windows process handle."},
151 {NULL, NULL, 0, NULL} /* Sentinel */
157 Py_InitModule("win32pm", win32pmMethods);