def _create_event():
"""
Creates a Win32 unnamed Event .
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx
"""
return windll.kernel32.CreateEventA(pointer(SECURITY_ATTRIBUTES()), BOOL(True), BOOL(False), None)
python类BOOL的实例源码
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def allow_interruption(*callbacks):
if sys.platform == 'win32':
from ctypes import WINFUNCTYPE, windll
from ctypes.wintypes import BOOL, DWORD
kernel32 = windll.LoadLibrary('kernel32')
phandler_routine = WINFUNCTYPE(BOOL, DWORD)
setconsolectrlhandler = kernel32.SetConsoleCtrlHandler
setconsolectrlhandler.argtypes = (phandler_routine, BOOL)
setconsolectrlhandler.restype = BOOL
@phandler_routine
def shutdown(event):
if event == 0:
for loop, cb in callbacks:
loop.call_soon_threadsafe(cb)
return 1
return 0
if setconsolectrlhandler(shutdown, 1) == 0:
raise WindowsError()
else:
def handler(*args):
for loop, cb in callbacks:
loop.call_soon_threadsafe(cb)
signal.signal(signal.SIGINT, handler)
try:
yield
finally:
if sys.platform == 'win32':
if setconsolectrlhandler(shutdown, 0) == 0:
raise WindowsError()
else:
signal.signal(signal.SIGINT, signal.SIG_DFL)
def ConnectNamedPipe(named_pipe, overlapped):
"""See: ConnectNamedPipe function
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365146(v=vs.85).aspx
"""
ConnectNamedPipe_Fn = windll.kernel32.ConnectNamedPipe
ConnectNamedPipe_Fn.argtypes = [
wintypes.HANDLE, # _In_ HANDLE hNamedPipe,
LPOVERLAPPED # _Inout_opt_ LPOVERLAPPED lpOverlapped
]
ConnectNamedPipe_Fn.restype = wintypes.BOOL
ret = wintypes.BOOL(ConnectNamedPipe_Fn(
named_pipe,
overlapped
))
return ret
def ReadFile(file, buffer, number_of_bytes_to_read, number_of_bytes_read, overlapped):
"""See: ReadFile function
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx
"""
ReadFile_Fn = windll.kernel32.ReadFile
ReadFile_Fn.argtypes = [
wintypes.HANDLE, # _In_ HANDLE hFile,
LPVOID, # _Out_ LPVOID lpBuffer,
wintypes.DWORD, # _In_ DWORD nNumberOfBytesToRead,
LPDWORD, # _Out_opt_ LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED # _Inout_opt_ LPOVERLAPPED lpOverlapped
]
ReadFile_Fn.restype = wintypes.BOOL
ret = wintypes.BOOL(ReadFile_Fn(
file,
buffer,
number_of_bytes_to_read,
number_of_bytes_read,
overlapped
))
def send_ioctl(self, ioctl, inbuf, inbufsiz, outbuf, outbufsiz):
"""See: DeviceIoControl function
http://msdn.microsoft.com/en-us/library/aa363216(v=vs.85).aspx
"""
DeviceIoControl_Fn = windll.kernel32.DeviceIoControl
DeviceIoControl_Fn.argtypes = [
wintypes.HANDLE, # _In_ HANDLE hDevice
wintypes.DWORD, # _In_ DWORD dwIoControlCode
wintypes.LPVOID, # _In_opt_ LPVOID lpInBuffer
wintypes.DWORD, # _In_ DWORD nInBufferSize
wintypes.LPVOID, # _Out_opt_ LPVOID lpOutBuffer
wintypes.DWORD, # _In_ DWORD nOutBufferSize
LPDWORD, # _Out_opt_ LPDWORD lpBytesReturned
LPOVERLAPPED] # _Inout_opt_ LPOVERLAPPED lpOverlapped
DeviceIoControl_Fn.restype = wintypes.BOOL
print ioctl
# allocate a DWORD, and take its reference
dwBytesReturned = wintypes.DWORD(0)
lpBytesReturned = ctypes.byref(dwBytesReturned)
status = DeviceIoControl_Fn(self.handle,
ioctl,
inbuf,
inbufsiz,
outbuf,
outbufsiz,
lpBytesReturned,
None)
return status, dwBytesReturned
def delete_service(service_handle):
"""See: DeleteService function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682562(v=vs.85).aspx
"""
DeleteService_Fn = windll.Advapi32.DeleteService #BOOL WINAPI DeleteService(
DeleteService_Fn.argtypes = [ #
wintypes.SC_HANDLE # _In_ SC_HANDLE hService
]
DeleteService_Fn.restype = wintypes.BOOL
bool = DeleteService_Fn(
service_handle
)
return bool
def _link(src, dst, linktype=LINK_HARD):
if linktype == LINK_HARD:
if on_win:
from ctypes import windll, wintypes
CreateHardLink = windll.kernel32.CreateHardLinkW
CreateHardLink.restype = wintypes.BOOL
CreateHardLink.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR,
wintypes.LPVOID]
if not CreateHardLink(dst, src, None):
raise OSError('win32 hard link failed')
else:
os.link(src, dst)
elif linktype == LINK_COPY:
# copy relative symlinks as symlinks
if islink(src) and not os.readlink(src).startswith(os.path.sep):
os.symlink(os.readlink(src), dst)
else:
shutil.copy2(src, dst)
else:
raise Exception("Did not expect linktype=%r" % linktype)
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def _create_event():
"""
Creates a Win32 unnamed Event .
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx
"""
return windll.kernel32.CreateEventA(
pointer(SECURITY_ATTRIBUTES()), BOOL(True), BOOL(False), None)
def _create_event():
"""
Creates a Win32 unnamed Event .
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx
"""
return windll.kernel32.CreateEventA(pointer(SECURITY_ATTRIBUTES()), BOOL(True), BOOL(False), None)
def ErrCheckBool(result, func, args):
"""errcheck function for Windows functions that return a BOOL True
on success"""
if not result:
raise WinError()
return args
# AutoHANDLE
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
windowCount = 0
@WINFUNCTYPE(BOOL, HWND, LPARAM)
def EnumWindowsCallbackFunc(hwnd, lParam):
global windowCount
windowCount += 1
return True #Allow windows to keep enumerating
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def send_ioctl(self, ioctl, inbuf, inbufsiz, outbuf, outbufsiz):
"""See: DeviceIoControl function
http://msdn.microsoft.com/en-us/library/aa363216(v=vs.85).aspx
"""
DeviceIoControl_Fn = windll.kernel32.DeviceIoControl
DeviceIoControl_Fn.argtypes = [
wintypes.HANDLE, # _In_ HANDLE hDevice
wintypes.DWORD, # _In_ DWORD dwIoControlCode
wintypes.LPVOID, # _In_opt_ LPVOID lpInBuffer
wintypes.DWORD, # _In_ DWORD nInBufferSize
wintypes.LPVOID, # _Out_opt_ LPVOID lpOutBuffer
wintypes.DWORD, # _In_ DWORD nOutBufferSize
LPDWORD, # _Out_opt_ LPDWORD lpBytesReturned
LPOVERLAPPED] # _Inout_opt_ LPOVERLAPPED lpOverlapped
DeviceIoControl_Fn.restype = wintypes.BOOL
# allocate a DWORD, and take its reference
dwBytesReturned = wintypes.DWORD(0)
lpBytesReturned = ctypes.byref(dwBytesReturned)
status = DeviceIoControl_Fn(self.handle,
ioctl,
inbuf,
inbufsiz,
outbuf,
outbufsiz,
lpBytesReturned,
None)
return status, dwBytesReturned
def close_service_handle(service_handle):
"""See: CloseServiceHandle function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682028(v=vs.85).aspx
"""
CloseServiceHandle_Fn = windll.Advapi32.CloseServiceHandle #BOOL WINAPI CloseServiceHandle(
CloseServiceHandle_Fn.argtypes = [
wintypes.SC_HANDLE # _In_ SC_HANDLE hSCObject
]
CloseServiceHandle_Fn.restype = wintypes.BOOL
bool = CloseServiceHandle_Fn(
service_handle
)
return bool
def delete_service(service_handle):
"""See: DeleteService function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682562(v=vs.85).aspx
"""
DeleteService_Fn = windll.Advapi32.DeleteService #BOOL WINAPI DeleteService(
DeleteService_Fn.argtypes = [ #
wintypes.SC_HANDLE # _In_ SC_HANDLE hService
]
DeleteService_Fn.restype = wintypes.BOOL
bool = DeleteService_Fn(
service_handle
)
return bool
def notify_win(title, text):
try:
from servo.win32_toast import WindowsToast
w = WindowsToast()
w.balloon_tip(title, text)
except:
from ctypes import Structure, windll, POINTER, sizeof
from ctypes.wintypes import DWORD, HANDLE, WINFUNCTYPE, BOOL, UINT
class FLASHWINDOW(Structure):
_fields_ = [("cbSize", UINT),
("hwnd", HANDLE),
("dwFlags", DWORD),
("uCount", UINT),
("dwTimeout", DWORD)]
FlashWindowExProto = WINFUNCTYPE(BOOL, POINTER(FLASHWINDOW))
FlashWindowEx = FlashWindowExProto(("FlashWindowEx", windll.user32))
FLASHW_CAPTION = 0x01
FLASHW_TRAY = 0x02
FLASHW_TIMERNOFG = 0x0C
params = FLASHWINDOW(sizeof(FLASHWINDOW),
windll.kernel32.GetConsoleWindow(),
FLASHW_CAPTION | FLASHW_TRAY | FLASHW_TIMERNOFG, 3, 0)
FlashWindowEx(params)