Salome HOME
tag 5.1.2 --> 5.1.3
[modules/kernel.git] / src / win32pm / win32pm.c
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include <Python.h>
23 #include <windows.h>
24
25 static PyObject * win32pm_spawnhandle( PyObject *self, PyObject *args )
26 {
27   char *argv;
28   char *flags;
29   STARTUPINFO si;
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
35      dialog:
36      "Your program accessed mem currently in use at xxx"
37      and a hopeful warning about the stability of your
38      system.
39      Cost is Ctrl+C wont kill children, but anyone
40      who cares can have a go!
41   */
42
43   if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
44     return NULL;
45   if ( flags && !strcmp( flags, "-nc" ) ) // no console
46     dwProcessFlags = 0;
47
48   ZeroMemory( &si, sizeof(si) );
49   si.cb = sizeof(si);
50   ZeroMemory( &pi, sizeof(pi) );
51
52   if ( !CreateProcess( NULL, argv,
53                         NULL,             // Process handle not inheritable. 
54                         NULL,             // Thread handle not inheritable. 
55                         TRUE,
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.
61      return NULL;
62   return Py_BuildValue("i", pi.hProcess);
63 }
64
65 static PyObject * win32pm_spawnpid( PyObject *self, PyObject *args )
66 {
67   char* argv;
68   char *flags;
69   STARTUPINFO si;
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
75      dialog:
76      "Your program accessed mem currently in use at xxx"
77      and a hopeful warning about the stability of your
78      system.
79      Cost is Ctrl+C wont kill children, but anyone
80      who cares can have a go!
81   */
82
83   if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
84     return NULL;
85   if ( flags && !strcmp( flags, "-nc" ) ) // no console
86     dwProcessFlags = 0;
87
88   ZeroMemory( &si, sizeof(si) );
89   si.cb = sizeof(si);
90   ZeroMemory( &pi, sizeof(pi) );
91
92   if ( !CreateProcess( NULL, argv,
93                         NULL,             // Process handle not inheritable. 
94                         NULL,             // Thread handle not inheritable. 
95                         TRUE,
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.
101      return NULL;
102   return Py_BuildValue("i", pi.dwProcessId);
103 }
104
105 static PyObject * win32pm_handle( PyObject *self, PyObject *args )
106 {
107   int argv1 = 0;
108   int argv2 = 0;
109   HANDLE ph = 0;
110   int pid = 0;
111   if (!PyArg_ParseTuple(args, "i|i", &argv1, &argv2))
112     return NULL;
113   ph = OpenProcess( 1, (BOOL)argv2, argv1  );
114   return Py_BuildValue("i", (int)ph );
115 }
116
117 static PyObject * win32pm_killpid( PyObject *self, PyObject *args )
118 {
119   int pid = 0;
120   int exitCode = 0;
121   HANDLE ph = 0;
122   BOOL stat = 0;
123   if (!PyArg_ParseTuple(args, "i|i", &pid, &exitCode))
124     return NULL;
125   ph = OpenProcess( 1, 0, pid );
126   stat = TerminateProcess( ph, exitCode );
127   return Py_BuildValue("i", (int)stat );
128 }
129
130 static PyObject * win32pm_killhandle( PyObject *self, PyObject *args )
131 {
132   int phId = 0;
133   int exitCode = 0;
134   BOOL stat = 0;
135   if (!PyArg_ParseTuple(args, "i|i", &phId, &exitCode))
136     return NULL;
137
138   stat = TerminateProcess( (void*)phId, exitCode );
139   return Py_BuildValue("i", (int)stat );
140 }
141
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 */
154 };
155
156 PyMODINIT_FUNC
157 initwin32pm(void)
158 {
159     Py_InitModule("win32pm", win32pmMethods);
160 }