Skip to content

Instantly share code, notes, and snippets.

@spddl
Created January 6, 2022 13:48
Show Gist options
  • Save spddl/c9aa16f2252fa35025a6d4b9e1b4b01c to your computer and use it in GitHub Desktop.
Save spddl/c9aa16f2252fa35025a6d4b9e1b4b01c to your computer and use it in GitHub Desktop.
"""
import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello', 'Window title', 0)
"""
########################################################
"""
import ctypes
# https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
OpenProcess = ctypes.windll.kernel32.OpenProcess
CloseHandle = ctypes.windll.kernel32.CloseHandle
GetLastError = ctypes.windll.kernel32.GetLastError
# https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
SetPriorityClass = ctypes.windll.kernel32.SetPriorityClass
# https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getpriorityclass
GetPriorityClass = ctypes.windll.kernel32.GetPriorityClass
pid = 11476
ProcessAllAccess = 0x1F0FFF
PRIORITY_CLASS = {}
PRIORITY_CLASS[0x00000080] = "HIGH"
PRIORITY_CLASS[0x00000020] = "NORMAL"
PRIORITY_CLASS[0x00000040] = "IDLE"
pHndl = OpenProcess(ProcessAllAccess, False, pid)
print("handle", pHndl)
if pHndl == 0:
print("GetLastError", GetLastError())
print("before GetPriorityClass", GetPriorityClass(pHndl), PRIORITY_CLASS[GetPriorityClass(pHndl)])
if PRIORITY_CLASS[GetPriorityClass(pHndl)] == "NORMAL" or PRIORITY_CLASS[GetPriorityClass(pHndl)] == "IDLE":
SetPriorityClass(pHndl, 0x00000080) # HIGH
else:
SetPriorityClass(pHndl, 0x00000040) # IDLE
print("after GetPriorityClass", GetPriorityClass(pHndl), PRIORITY_CLASS[GetPriorityClass(pHndl)])
CloseHandle(pHndl)
# handle 312
# before GetPriorityClass 64 IDLE
# after GetPriorityClass 128 HIGH
"""
########################################################
from ctypes import *
from ctypes.wintypes import *
# https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
OpenProcess = ctypes.windll.kernel32.OpenProcess
CloseHandle = ctypes.windll.kernel32.CloseHandle
GetLastError = ctypes.windll.kernel32.GetLastError
# https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdirestartdevices
SetupDiRestartDevices = ctypes.windll.setupapi.SetupDiRestartDevices
# https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetclassdevsw
SetupDiGetClassDevs = ctypes.windll.setupapi.SetupDiGetClassDevsW
# https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdienumdeviceinfo
SetupDiEnumDeviceInfo = ctypes.windll.setupapi.SetupDiEnumDeviceInfo
# https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdidestroydeviceinfolist
SetupDiDestroyDeviceInfoList = ctypes.windll.setupapi.SetupDiDestroyDeviceInfoList
# copy&paste https://github.com/sstjohn/thundergate/blob/78b46d189e110c41dd80211b2c767a7206a0431d/py/winlib.py#L147
class GUID(Structure):
_fields_ = [("Data1", c_ulong),
("Data2", c_ushort),
("Data3", c_ushort),
("Data4", ARRAY(c_ubyte, 8))]
def __str__(self):
ret = ("%08x-" % self.Data1)
ret += ("%04x-" % self.Data2)
ret += ("%04x-" % self.Data3)
ret += ("%02x%02x-" % (self.Data4[0], self.Data4[1]))
ret += ''.join([("%02x" % b) for b in self.Data4[2:]])
return ret
class SP_DEVINFO_DATA(Structure):
_fields_ = [('cbSize', DWORD),
('ClassGuid', GUID),
('DevInst', DWORD),
('Reserved', POINTER(ULONG))]
DIGCF_PRESENT = 0x00000002
DIGCF_ALLCLASSES = 0x00000004
DIGCF_PROFILE = 0x00000008
handle = SetupDiGetClassDevs(None, None, 0, DIGCF_ALLCLASSES|DIGCF_PRESENT|DIGCF_PROFILE)
if handle == 0:
print("GetLastError", GetLastError())
print("handle", handle)
for deviceIndex in range(60):
devInfoData = SP_DEVINFO_DATA()
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA)
ret = SetupDiEnumDeviceInfo(handle, deviceIndex, pointer(devInfoData))
if ret == 0:
print("GetLastError", GetLastError())
print(deviceIndex, "idata", devInfoData)
# GetLastError 6
# 52 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C759C0>
# GetLastError 6
# 53 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C77A40>
# GetLastError 6
# 54 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C759C0>
# GetLastError 6
# 55 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C77A40>
# GetLastError 6
# 56 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C759C0>
# GetLastError 6
# 57 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C77A40>
# GetLastError 6
# 58 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C759C0>
# GetLastError 6
# 59 idata <__main__.SP_DEVINFO_DATA object at 0x000001CF04C77A40>
SetupDiDestroyDeviceInfoList(handle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment