Red de conocimiento informático - Conocimiento del nombre de dominio - Cómo usar Python para llamar a la DLL win32 compilada por vc

Cómo usar Python para llamar a la DLL win32 compilada por vc

Python llama a la API en vc

Preparación: instalar pywin32-210.5.win32-py2.5.exe

Ejemplo 1: la función MessageBox más simple

importar win32api, win32gui

importar win32con, winerror, win32event, pywintypes

importar sistema, sistema operativo, hora

win32api.MessageBox( 0, 'hello', 'WYM', win32con.MB_OK)

Ejemplo 2: Monitoreo de procesos

#Mutex.py

mutex=None

mutex=win32event.CreateMutex(None, pywintypes.FALSE, "MutexWym")

if(win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS):

print" Abierto existente objeto mutex", mutex

else:

print "Nuevo mutex creado"

time.sleep(10)

win32api .CloseHandle (mutex)

imprimir "cerrar Mutex"

time.sleep(10)

#MonitorProcess.py

importar win32api, win32gui

importar win32con, winerror, win32event, pywintypes

importar sys, os, time

STANDARD_RIGHTS_REQUIRED = 0x000F0000L

SYNCHRONIZE = 0x00100000L

SYNCHRONIZE = 0x00100000L

p>

MUTANT_QUERY_STATE = 0x0001

MUTEX_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | MUTANT_QUERY_STATE

prueba:

mientras 1:

tiempo. sleep(2)

hMutex=win32event.OpenMutex(MUTEX_ALL_ACCESS, pywintypes.FALSE, "MutexWym")

imprime win32api.GetLastError()

si hMutex es no Ninguno:

imprime "Mutex abierto"

win32api.CloseHandle(hMutex)

más:

imprime "Mutex no abierto" <

/p>

break

excepto pywintypes.error, (errno, object, strerror):

print "Error in", object, ":", strerror

Cómo utilizar la API kernel32

Ejemplo 3:

importar pywintypes

importar winerror, win32con

importar win32api, win32event , win32file, win32pipe, win32process, win32security

importar win32service, win32serviceutil, servicemanager, ntsecuritycon

desde ctypes import * #Este ejemplo incluye este archivo de encabezado

hMutex = windll.kernel32.CreateMutexA(None, 0, "szMutex")

#Lo mismo se aplica a otras API, como windll.kernel32.InitializeCriticalSection(), etc.

a, Python ¿Qué tipos de ventanas están involucradas?

Puede obtener este tipo de datos a través de pywintypes

b. Obtener el identificador de la ventana

Busque el identificador de la ventana correspondiente a través de la API FindWindow y luego envíelo. un mensaje a esta ventana, puede lograr esta función

ct = win32api.GetConsoleTitle()

hd = win32gui.FindWindow(0, ct)

win32gui .ShowWindow(hd,0)