Salome HOME
Win32 Porting.
authorabd <abd@opencascade.com>
Fri, 3 Oct 2008 08:52:21 +0000 (08:52 +0000)
committerabd <abd@opencascade.com>
Fri, 3 Oct 2008 08:52:21 +0000 (08:52 +0000)
Adding of win32pm package for support of process management by PIDs on Win32 platform

src/Makefile.am
src/win32pm/setup.py [new file with mode: 0755]
src/win32pm/win32pm.c [new file with mode: 0755]

index 82efa8787d72eda06c27b4150a6b6f15394a72ed..d2e4b2738b0dcbf40995b632ad4ce9e1f8b4331f 100644 (file)
@@ -129,3 +129,5 @@ endif
 if WITH_PACO_PARALLEL
 SUBDIRS += $(SUBDIR_PARALLEL)
 endif
+
+EXTRA_DIST = win32pm
diff --git a/src/win32pm/setup.py b/src/win32pm/setup.py
new file mode 100755 (executable)
index 0000000..c285b9b
--- /dev/null
@@ -0,0 +1,10 @@
+from distutils.core import setup, Extension
+
+module1 = Extension('win32pm',
+                    libraries = ['kernel32'],
+                    sources = ['win32pm.c'])
+
+setup (name = 'win32pm',
+       version = '1.0',
+       description = 'Win32 process managment package',
+       ext_modules = [module1])
diff --git a/src/win32pm/win32pm.c b/src/win32pm/win32pm.c
new file mode 100755 (executable)
index 0000000..7fd227d
--- /dev/null
@@ -0,0 +1,139 @@
+#include <Python.h>
+#include <windows.h>
+
+static PyObject * win32pm_spawnhandle( PyObject *self, PyObject *args )
+{
+  char *argv;
+  char *flags;
+  STARTUPINFO si;
+  PROCESS_INFORMATION pi;
+  DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
+  /* Default value as in Python sources:
+     CREATE_NEW_CONSOLE has been known to
+     cause random failures on win9x.  Specifically a
+     dialog:
+     "Your program accessed mem currently in use at xxx"
+     and a hopeful warning about the stability of your
+     system.
+     Cost is Ctrl+C wont kill children, but anyone
+     who cares can have a go!
+  */
+
+  if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
+    return NULL;
+  if ( flags && !strcmp( flags, "-nc" ) ) // no console
+    dwProcessFlags = 0;
+
+  ZeroMemory( &si, sizeof(si) );
+  si.cb = sizeof(si);
+  ZeroMemory( &pi, sizeof(pi) );
+
+  if ( !CreateProcess( NULL, argv,
+                        NULL,             // Process handle not inheritable. 
+                        NULL,             // Thread handle not inheritable. 
+                        TRUE,
+                        dwProcessFlags,   // Creation flags. 
+                        NULL,             // Use parent's environment block. 
+                        NULL,             // Use parent's starting directory. 
+                        &si,              // Pointer to STARTUPINFO structure.
+                        &pi ) )             // Pointer to PROCESS_INFORMATION structure.
+     return NULL;
+  return Py_BuildValue("i", pi.hProcess);
+}
+
+static PyObject * win32pm_spawnpid( PyObject *self, PyObject *args )
+{
+  char* argv;
+  char *flags;
+  STARTUPINFO si;
+  PROCESS_INFORMATION pi;
+  DWORD dwProcessFlags = CREATE_NEW_CONSOLE;
+  /* Default value as in Python sources:
+     CREATE_NEW_CONSOLE has been known to
+     cause random failures on win9x.  Specifically a
+     dialog:
+     "Your program accessed mem currently in use at xxx"
+     and a hopeful warning about the stability of your
+     system.
+     Cost is Ctrl+C wont kill children, but anyone
+     who cares can have a go!
+  */
+
+  if (!PyArg_ParseTuple(args, "s|s", &argv, &flags))
+    return NULL;
+  if ( flags && !strcmp( flags, "-nc" ) ) // no console
+    dwProcessFlags = 0;
+
+  ZeroMemory( &si, sizeof(si) );
+  si.cb = sizeof(si);
+  ZeroMemory( &pi, sizeof(pi) );
+
+  if ( !CreateProcess( NULL, argv,
+                        NULL,             // Process handle not inheritable. 
+                        NULL,             // Thread handle not inheritable. 
+                        TRUE,
+                        dwProcessFlags,   // Creation flags. 
+                        NULL,             // Use parent's environment block. 
+                        NULL,             // Use parent's starting directory. 
+                        &si,              // Pointer to STARTUPINFO structure.
+                        &pi ) )             // Pointer to PROCESS_INFORMATION structure.
+     return NULL;
+  return Py_BuildValue("i", pi.dwProcessId);
+}
+
+static PyObject * win32pm_handle( PyObject *self, PyObject *args )
+{
+  int argv1 = 0;
+  int argv2 = 0;
+  HANDLE ph = 0;
+  int pid = 0;
+  if (!PyArg_ParseTuple(args, "i|i", &argv1, &argv2))
+    return NULL;
+  ph = OpenProcess( 1, (BOOL)argv2, argv1  );
+  return Py_BuildValue("i", (int)ph );
+}
+
+static PyObject * win32pm_killpid( PyObject *self, PyObject *args )
+{
+  int pid = 0;
+  int exitCode = 0;
+  HANDLE ph = 0;
+  BOOL stat = 0;
+  if (!PyArg_ParseTuple(args, "i|i", &pid, &exitCode))
+    return NULL;
+  ph = OpenProcess( 1, 0, pid );
+  stat = TerminateProcess( ph, exitCode );
+  return Py_BuildValue("i", (int)stat );
+}
+
+static PyObject * win32pm_killhandle( PyObject *self, PyObject *args )
+{
+  int phId = 0;
+  int exitCode = 0;
+  BOOL stat = 0;
+  if (!PyArg_ParseTuple(args, "i|i", &phId, &exitCode))
+    return NULL;
+
+  stat = TerminateProcess( (void*)phId, exitCode );
+  return Py_BuildValue("i", (int)stat );
+}
+
+static PyMethodDef win32pmMethods[] = {
+    {"spawnhandle",  win32pm_spawnhandle, METH_VARARGS,
+     "Creates new process. Returns windows handle of new process."},
+    {"spawnpid",  win32pm_spawnpid, METH_VARARGS,
+     "Creates new process. Returns PID of new process."},
+    {"handle",  win32pm_handle, METH_VARARGS,
+     "Returns windows handle of indicated process PID."},
+    {"killpid",  win32pm_killpid, METH_VARARGS,
+     "Terminate process by PID."},
+    {"killhandle",  win32pm_killhandle, METH_VARARGS,
+     "Terminate process by windows process handle."},
+    {NULL, NULL, 0, NULL}        /* Sentinel */
+};
+
+PyMODINIT_FUNC
+initwin32pm(void)
+{
+    Py_InitModule("win32pm", win32pmMethods);
+}