Python Win32 ProcessIn this article, we will discuss Python win32 process. And also we will discuss its methods one by one. Basically, the Win32 process is a method in Python. Extended Win32 process creation and management capabilities are accessible through this module. The Create method creates process objects (the constructor). It is possible to kill, suspend, resume, and set the priority of processes on objects using additional methods. Windows Management Instrumentation (WMI; formerly WBEM) and WMI extensions for the Windows Driver Model serve as the foundations for manageability in Windows 2019/2016/2012/2008 and Windows 10/7/XP (WDM). The ability to create monitor check procedures based on WMI is offered by ActiveXperts Network Monitor. There are more than a hundred WMI samples that ActiveXperts has gathered. These examples might serve as a starting point for brand-new check routines that you create on your own. Many WMI samples are available on this website. ActiveXperts Network Monitor uses the Win32_Process WMI class to monitor your servers. A series of events on a Windows operating system is represented by the Win32_Process WMI class. A sequence that involves the interaction of one or more processors or interpreters, some executable code, and a set of inputs, such as a client program running on a Windows system, is a descendent or member of this class. Now the question arises what is Python win32? So the Python win32 and Win32 application programming interface (API) capabilities can be used with Python by using the PyWin32 library of extensions for Windows. Let's take a small introduction to the win32api Module. The win32api module offers various extra methods for controlling processes. These give you the ability to carry out many of the usual steps needed to launch new processes, but they still fall short of offering the highest level of low-level control. In contrast to the os.system function, which was previously explained, the WinExec function makes various accommodations for GUI programs. For example, no console is established, and the function doesn't wait until the new process has finished. The function requires these two inputs:
Let's take a small introduction to the win32api.ShellExecute. In addition, the win32api module offers another beneficial feature for starting new processes. In contrast to starting random processes, opening documents is the main purpose of the ShellExecute function. You may instruct ShellExecute to "open MyDocument.doc," for instance. Windows chooses which process to launch on your behalf in order to open.doc files. The click (or double-click) on an a.doc file causes Windows Explorer to perform the same action. A program that is being run is referred to as a process (processed). A process need not be one that the user runs manually; it could instead be a system process that the operating system spawns. Any program that runs on an operating system must first generate a separate process before it can begin to operate. The majority of processes in a typical OS installation are background programs and operating system services that are used to keep the hardware, software, and operating system in good working order. This post will look at a few alternative Python methods for getting a list of a Windows OS's currently active processes. To get the desired outcome, we will first describe a Python method. We will then examine a command from the Windows Command Processor to accomplish the same thing. pip install wmi Copy this above code in the terminal. ExampleOutput: The WMI() function of the wmi library is first initialized. This enables us to access its internal functions, such as WMI.Win32_Service, WMI.Win32_Process, and WMI.Win32_Printjob, each of which is intended to carry out a certain duty. To obtain a list of the system's active processes, we would use the WMI.Win32_Process function. After that, we iterated through all the running processes and placed them in the variable process by calling the function WMI.Win32_Process(). The corresponding attributes were then used to derive the process's ProcessID (pid) and ProcessName (name). To add padding to the output and properly align it, we used F-strings for the output. Now let's go through different methods of module Win32process. 1. STARTUPINFOIn this method, we create a new STARTUPINFO object. Let's understand how to create this, which is given below: win32process.STARTUPINFO PySTARTUPINFO = STARTUPINFO() 2. beginthreadexIn this method, we create a new thread. Let's understand how to create this, which is given below: win32process.beginthreadex PyHANDLE, int = beginthreadex(sa, stackSize , entryPoint , args , flags ) Let's understand its parameters is given below Parameters
CREATE_SUSPENDED is an option for delaying the start of a thread. The thread handle and thread ID are returned as a tuple as the outcome. 3. CreateProcesswin32process.CreateProcess PyHANDLE, PyHANDLE, int, int = CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo ) establishes a new process and the main thread for it. The newly created process runs the designated executable file. Parameters
4. CreateRemoteThreadwin32process.CreateRemoteThread PyHANDLE, int = CreateRemoteThread(hprocess, sa , stackSize , entryPoint , Parameter , flags ) establishes a thread that executes in another process's virtual address space. Parameters
The thread handle and thread ID are returned as a tuple as the outcome. 5. CreateProcessAsUserwin32process.CreateProcessAsUser creates a new process with the provided user as its context. PyHANDLE, PyHANDLE, int, int = CreateProcessAsUser(hToken, appName , commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo ) Parameters
Consequently, a tuple of (hProcess, hThread, dwProcessId, dwThreadId) 6. GetCurrentProcesswin32process.GetCurrentProcess obtains a fictitious handle for the active process. int = GetCurrentProcess() 7. GetCurrentProcessIdwin32process.GetCurrentProcessId reveals the caller process's unique process identification. int = GetCurrentProcessId() 8. GetProcessVersionwin32process.GetProcessVersion reveals the system's main and minor version numbers, which are needed to conduct a specific process. int = GetProcessVersion(processId) Parameters
9. GetCurrentProcessIdwin32process.GetCurrentProcessId reveals the caller process's unique process identification. int = GetCurrentProcessId() 10. GetStartupInfowin32process.GetStartupInfo reveals the STARTUPINFO structure's contents, which were supplied when the caller process was established. PySTARTUPINFO = GetStartupInfo() 11. GetPriorityClasswin32process.GetPriorityClass int = GetPriorityClass(handle) Parameters
12. GetExitCodeThreadwin32process.GetExitCodeThread int = GetExitCodeThread(handle) Parameters
13. GetExitCodeProcesswin32process.GetExitCodeProcess int = GetExitCodeProcess(handle) Parameters
14. GetWindowThreadProcessIdwin32process.GetWindowThreadProcessId returns the thread and process IDs that were responsible for the provided window's creation. int, int = GetWindowThreadProcessId(hwnd) Parameters
Consequently, a tuple of (threadId, processId) 15. SetThreadPrioritywin32process.SetThreadPriority SetThreadPriority(handle, nPriority) Parameters
16. GetThreadPrioritywin32process.GetThreadPriority int = GetThreadPriority(handle) Parameters
17. GetProcessPriorityBoostwin32process.GetProcessPriorityBoost determines whether a process's dynamic priority adjustment is enabled. bool = GetProcessPriorityBoost(Process) Parameters
18. SetProcessPriorityBoostwin32process.SetProcessPriorityBoost enables or disables a process's dynamic priority adjustment. SetProcessPriorityBoost(Process, DisablePriorityBoost) Parameters
19. GetThreadPriorityBoostwin32process.GetThreadPriorityBoost determines whether a thread's dynamic priority adjustment is enabled. bool = GetThreadPriorityBoost(Thread) Parameters
20. SetThreadPriorityBoostwin32process.SetThreadPriorityBoost enables or disables a thread's dynamic priority adjustment. SetThreadPriorityBoost(Thread, DisablePriorityBoost) Parameters
21. GetThreadIOPendingFlagwin32process.GetThreadIOPendingFlag determines whether a thread has any open IO requests. bool = GetThreadIOPendingFlag(Thread) Parameters
22. GetThreadTimeswin32process.GetThreadTimes It returns the time statistics for a thread. dict = GetThreadTimes(Thread) Parameters
23. GetProcessIdint = GetProcessId(Process) It returns the Pid for a process handle. Parameters
24. SetPriorityClasswin32process.SetPriorityClass SetPriorityClass(handle, dwPriorityClass) Parameters
25. AttachThreadInputwin32process.AttachThreadInput connects and disconnects the input of two threads. AttachThreadInput(idAttach, idAttachTo, Attach) Parameters
26. SetThreadIdealProcessorwin32process.SetThreadIdealProcessor Syntax Parameters
Return type This method return the int value 27. GetProcessAffinityMaskwin32process.GetProcessAffinityMask Syntax Parameters
Return type This method returns a tuple of ( process affinity mask, system affinity mask ). 28. SetProcessAffinityMaskwin32process.SetProcessAffinityMask Syntax Sets a processor affinity mask for a specified process. Parameters
Note: Some platforms do not have this feature.29. SetThreadAffinityMaskwin32process.SetThreadAffinityMask Syntax Parameters
Return type This method returns an int value. 30. SuspendThreadwin32process.SuspendThread Syntax Suspends the specified thread. Parameters
Return value The return value is the thread's previous suspend count 31. ResumeThreadwin32process.ResumeThread Syntax Resumes the specified thread. When the suspend count is decremented to zero, the execution of the thread is resumed. Parameters
Return value The return value is the thread's previous suspend count 32. TerminateProcesswin32process.TerminateProcess Syntax Parameters
33. xitProcesswin32process.ExitProcess
Parameters
The best way to stop a process is with ExitProcess. A clean process shutdown is provided by this function. This includes contacting each associated dynamic-link library's (DLL) entry-point function with a value indicating that the process is separating from the DLL. The DLLs associated with the process are not informed of the process termination if a process terminates by invoking win32process::TerminateProcess. 34. EnumProcesseswin32process.EnumProcesses Syntax Provides Pids for activities that are actually running. 35. EnumProcessModuleswin32process.EnumProcessModules Syntax Lists loaded modules for a process handle Parameters
36. EnumProcessModulesExwin32process.EnumProcessModulesEx Syntax lists the 32- or 64-bit modules that a process has loaded. Parameters
37. GetModuleFileNameExwin32process.GetModuleFileNameEx Syntax Parameters
38. GetProcessMemoryInfowin32process.GetProcessMemoryInfo Syntax A dict representing a PROCESS_MEMORY_COUNTERS struct is returned as the process memory statistics. Parameters
39. GetProcessTimeswin32process.GetProcessTimes Syntax Obtain time statistics for a process using its handle. (In 100 nanosecond units for UserTime and KernelTime) Parameters
40. GetProcessIoCounterswin32process.GetProcessIoCounters Syntax I/O statistics for a process are returned as a dictionary corresponding to an IO_COUNTERS struct. Parameters
41. GetProcessWindowStationwin32process.GetProcessWindowStation Syntax Returns a handle to the window station for the calling process. 42. GetProcessWorkingSetSizewin32process.GetProcessWorkingSetSize Syntax A process's minimum and maximum working set sizes are returned. Parameters
43. SetProcessWorkingSetSizewin32process.SetProcessWorkingSetSize Syntax Sets minimum and maximum working set sizes for a process. Parameters
NOTE: To entirely swap out the procedure, set both min and max to -1.44. GetProcessShutdownParameterswin32process.GetProcessShutdownParameters Syntax Reveals the process's current termination level and triggers. The range is 000-0FF. windows reserved, Last, 200-2FF Middle, First, 300-3FF, and Fourth, 400-4FF Windows reserves. 45. SetProcessShutdownParameterswin32process.SetProcessShutdownParameters Syntax Sets the process's flags and termination priority. Parameters
The range is 000-0FF. 100-1FF Last, 200-2FF Middle, 300-3FF First, 400-4FF, and reserved by windows window reserved. 46. GetGuiResourceswin32process.GetGuiResources Syntax Gives the amount of GDI or user object handles that a process is holding. Parameters
47. IsWow64Processwin32process.IsWow64Process Syntax Identifies whether WOW64 is currently running the specified process. Parameters
Let's see its Return Value. The return value is False if the operating system does not provide this function (ie, a NotImplemented exception will never be thrown). However, a win32process.error exception to this is normally thrown if the function is available but ineffective. ConclusionIn this article, we have discussed Python win32 process. And also, we have discussed the different types of methods and their parameters and return values one by one. |