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