From c4052d3124d7d422e73e60059bac0bba63c0b46d Mon Sep 17 00:00:00 2001 From: abd Date: Fri, 8 Aug 2008 07:18:57 +0000 Subject: [PATCH] Merging with BR_WPdev_For_5_0 --- src/win32pm/setup.py | 10 +++ src/win32pm/win32pm.c | 139 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100755 src/win32pm/setup.py create mode 100755 src/win32pm/win32pm.c 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); +} -- 2.39.2