From: abd Date: Mon, 4 Aug 2008 12:54:42 +0000 (+0000) Subject: Add win32pm package to the KERNEL sources X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=9f466f4da4beb70e58d6b8fb3b0764ebbf40e1e1;p=modules%2Fkernel.git Add win32pm package to the KERNEL sources --- diff --git a/src/win32pm/setup.py b/src/win32pm/setup.py new file mode 100755 index 000000000..c285b9b88 --- /dev/null +++ b/src/win32pm/setup.py @@ -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 index 000000000..7fd227dc1 --- /dev/null +++ b/src/win32pm/win32pm.c @@ -0,0 +1,139 @@ +#include +#include + +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); +}