Back to index

salome-kernel  6.5.0
win32pm.c
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include <Python.h>
00021 #include <windows.h>
00022 
00023 static PyObject * win32pm_spawnhandle( PyObject *self, PyObject *args )
00024 {
00025   char *argv;
00026   char *flags;
00027   STARTUPINFO si;
00028   PROCESS_INFORMATION pi;
00029   DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
00030   /* Default value as in Python sources:
00031      CREATE_NEW_CONSOLE has been known to
00032      cause random failures on win9x.  Specifically a
00033      dialog:
00034      "Your program accessed mem currently in use at xxx"
00035      and a hopeful warning about the stability of your
00036      system.
00037      Cost is Ctrl+C wont kill children, but anyone
00038      who cares can have a go!
00039   */
00040 
00041   if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
00042     return NULL;
00043   if ( flags && !strcmp( flags, "-nc" ) ) // no console
00044     dwProcessFlags = 0;
00045 
00046   ZeroMemory( &si, sizeof(si) );
00047   si.cb = sizeof(si);
00048   ZeroMemory( &pi, sizeof(pi) );
00049 
00050   if ( !CreateProcess( NULL, argv,
00051                         NULL,             // Process handle not inheritable. 
00052                         NULL,             // Thread handle not inheritable. 
00053                         TRUE,
00054                         dwProcessFlags,   // Creation flags. 
00055                         NULL,             // Use parent's environment block. 
00056                         NULL,             // Use parent's starting directory. 
00057                         &si,              // Pointer to STARTUPINFO structure.
00058                         &pi ) )             // Pointer to PROCESS_INFORMATION structure.
00059      return NULL;
00060   return Py_BuildValue("i", pi.hProcess);
00061 }
00062 
00063 static PyObject * win32pm_spawnpid( PyObject *self, PyObject *args )
00064 {
00065   char* argv;
00066   char *flags;
00067   STARTUPINFO si;
00068   PROCESS_INFORMATION pi;
00069   DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
00070   /* Default value as in Python sources:
00071      CREATE_NEW_CONSOLE has been known to
00072      cause random failures on win9x.  Specifically a
00073      dialog:
00074      "Your program accessed mem currently in use at xxx"
00075      and a hopeful warning about the stability of your
00076      system.
00077      Cost is Ctrl+C wont kill children, but anyone
00078      who cares can have a go!
00079   */
00080 
00081   if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
00082     return NULL;
00083   if ( flags && !strcmp( flags, "-nc" ) ) // no console
00084     dwProcessFlags = 0;
00085 
00086   ZeroMemory( &si, sizeof(si) );
00087   si.cb = sizeof(si);
00088   ZeroMemory( &pi, sizeof(pi) );
00089 
00090   if ( !CreateProcess( NULL, argv,
00091                         NULL,             // Process handle not inheritable. 
00092                         NULL,             // Thread handle not inheritable. 
00093                         TRUE,
00094                         dwProcessFlags,   // Creation flags. 
00095                         NULL,             // Use parent's environment block. 
00096                         NULL,             // Use parent's starting directory. 
00097                         &si,              // Pointer to STARTUPINFO structure.
00098                         &pi ) )             // Pointer to PROCESS_INFORMATION structure.
00099      return NULL;
00100   return Py_BuildValue("i", pi.dwProcessId);
00101 }
00102 
00103 static PyObject * win32pm_handle( PyObject *self, PyObject *args )
00104 {
00105   int argv1 = 0;
00106   int argv2 = 0;
00107   HANDLE ph = 0;
00108   int pid = 0;
00109   if (!PyArg_ParseTuple(args, "i|i", &argv1, &argv2))
00110     return NULL;
00111   ph = OpenProcess( 1, (BOOL)argv2, argv1  );
00112   return Py_BuildValue("i", (int)ph );
00113 }
00114 
00115 static PyObject * win32pm_killpid( PyObject *self, PyObject *args )
00116 {
00117   int pid = 0;
00118   int exitCode = 0;
00119   HANDLE ph = 0;
00120   BOOL stat = 0;
00121   if (!PyArg_ParseTuple(args, "i|i", &pid, &exitCode))
00122     return NULL;
00123   ph = OpenProcess( 1, 0, pid );
00124   stat = TerminateProcess( ph, exitCode );
00125   return Py_BuildValue("i", (int)stat );
00126 }
00127 
00128 static PyObject * win32pm_killhandle( PyObject *self, PyObject *args )
00129 {
00130   int phId = 0;
00131   int exitCode = 0;
00132   BOOL stat = 0;
00133   if (!PyArg_ParseTuple(args, "i|i", &phId, &exitCode))
00134     return NULL;
00135 
00136   stat = TerminateProcess( (void*)phId, exitCode );
00137   return Py_BuildValue("i", (int)stat );
00138 }
00139 
00140 static PyMethodDef win32pmMethods[] = {
00141     {"spawnhandle",  win32pm_spawnhandle, METH_VARARGS,
00142      "Creates new process. Returns windows handle of new process."},
00143     {"spawnpid",  win32pm_spawnpid, METH_VARARGS,
00144      "Creates new process. Returns PID of new process."},
00145     {"handle",  win32pm_handle, METH_VARARGS,
00146      "Returns windows handle of indicated process PID."},
00147     {"killpid",  win32pm_killpid, METH_VARARGS,
00148      "Terminate process by PID."},
00149     {"killhandle",  win32pm_killhandle, METH_VARARGS,
00150      "Terminate process by windows process handle."},
00151     {NULL, NULL, 0, NULL}        /* Sentinel */
00152 };
00153 
00154 PyMODINIT_FUNC
00155 initwin32pm(void)
00156 {
00157     Py_InitModule("win32pm", win32pmMethods);
00158 }