请教python杀死windows进程

请教python杀死windows进程,第1张

别人写的,转一下:

import ctypes

import sys

TH32CS_SNAPPROCESS = 0x00000002

class PROCESSENTRY32(ctypesStructure):

_fields_ = [("dwSize", ctypesc_ulong),

("cntUsage", ctypesc_ulong),

("th32ProcessID", ctypesc_ulong),

("th32DefaultHeapID", ctypesc_ulong),

("th32ModuleID", ctypesc_ulong),

("cntThreads", ctypesc_ulong),

("th32ParentProcessID", ctypesc_ulong),

("pcPriClassBase", ctypesc_ulong),

("dwFlags", ctypesc_ulong),

("szExeFile", ctypesc_char 260)]

def getProcList():

CreateToolhelp32Snapshot = ctypeswindllkernel32CreateToolhelp32Snapshot

Process32First = ctypeswindllkernel32Process32First

Process32Next = ctypeswindllkernel32Process32Next

CloseHandle = ctypeswindllkernel32CloseHandle

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

pe32 = PROCESSENTRY32()

pe32dwSize = ctypessizeof(PROCESSENTRY32)

if Process32First(hProcessSnap,ctypesbyref(pe32)) == False:

print >> sysstderr, "Failed getting first process"

return

while True:

yield pe32

if Process32Next(hProcessSnap,ctypesbyref(pe32)) == False:

break

CloseHandle(hProcessSnap)

def getChildPid(pid):

procList = getProcList()

for proc in procList:

if procth32ParentProcessID == pid:

yield procth32ProcessID

def killPid(pid):

childList = getChildPid(pid)

for childPid in childList:

killPid(childPid)

handle = ctypeswindllkernel32OpenProcess(1, False, pid)

ctypeswindllkernel32TerminateProcess(handle,0)

if __name__ =='__main__':

args = sysargv

if len(args) >1 :

pid = int(args[1])

killPid(pid)

else:

procList = getProcList()

for proc in procList:

print procszExeFile+' '+str(procth32ParentProcessID) + ' '+str(procth32ProcessID)

#----------------------

#

# Usage demo

#

#----------------------

import subprocess

import time

#import winproc

timeout = 2

process = subprocessPopen("cmd /k ping localhost -t",shell = True)

start = int(timetime())

while processpoll()==None:

now = int(timetime())

if int (now - start) >timeout:

pid = processpid

break

winprockillPid(pid)

print "End"

import subprocess

import time

import wmi

c = wmiWMI ()

#

# Kill a process by id

#

notepad = subprocessPopen (["notepadexe"])

timesleep (1)

for process in cWin32_Process (ProcessId=notepadpid):

processTerminate ()

#

# Which process ids correspond to an exe

#

for i in range (5):

subprocessPopen (["notepadexe"])

for process in cWin32_Process (caption="notepadexe"):

print processProcessId

#

# _Some_ (but not all) of the information about each file

#

for process in cWin32_Process (caption="notepadexe"):

print process

processTerminate ()

好好研究下~~

from ctypes import

import os

import win32con,win32clipboard

aString=windlluser32LoadImageW(0,"newbmp",win32conIMAGE_BITMAP,0,0,win32conLR_LOADFROMFILE)

print(aString)

if aString !=0: ## 由于编码问题 载入失败的话 aString 就等于0

win32clipboardOpenClipboard()

win32clipboardEmptyClipboard()

win32clipboardSetClipboardData(win32conCF_BITMAP, aString)

win32clipboardCloseClipboard()

————————————————

可以的,只要把python模块转换成dll模块,利用Python自带的ctypes模块加载调用就行。

ctypes 是Python的外部函数库。它提供了与 C语言兼容的数据类型,并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装。

ctypes导出了cdll对象,在 Windows 系统中还导出了windll和oledll对象用于载入动态链接库。通过 *** 作这些对象的属性,你可以载入外部的动态链接库。cdll载入按标准的cdecl调用协议导出的函数,而windll导入的库按stdcall调用协议调用其中的函数。

扩展资料:

加载调用DLL的相关方法:

1、加载DLL

加载的时候要根据你将要调用的函数是符合什么调用约定的。

stdcall调用约定:两种加载方式

Objdll = ctypeswindllLoadLibrary("dllpath")

Objdll = ctypesWinDLL("dllpath") 

cdecl调用约定:也有两种加载方式

Objdll = ctypescdllLoadLibrary("dllpath")

Objdll = ctypesCDLL("dllpath")

其实windll和cdll分别是WinDLL类和CDll类的对象。

2、调用dll中的方法

加载dll的时候会返回一个DLL对象(假设名字叫Objdll),利用该对象就可以调用dll中的方法。 eg如果dll中有个方法名字叫Add(注意如果经过stdcall声明的方法,如果不是用def文件声明的导出函数或者extern “C” 声明的话,编译器会对函数名进行修改,这个要注意。)

调用:nRet = ObjdllAdd(12, 15) 即完成一次调用。

参考资料来源:百度百科-ctypes

以上就是关于请教python杀死windows进程全部的内容,包括:请教python杀死windows进程、python如何将PIL.Image对象写入到windows剪贴板中、我现在想把自己写的python模块源代码封装成dll,然后在别的python脚本里调用,可以吗等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10138598.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存