def __init__(self, dllname, funcname):
self.__dllname = dllname
self.__funcname = funcname
self.__func = getattr(getattr(ctypes.windll, dllname), funcname)
python类WinDLL()的实例源码
def not_windows():
raise SystemError(
"Invalid platform. ctypes.windll must be available."
)
clipboard.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def _pasteWindows():
CF_UNICODETEXT = 13
d = ctypes.windll
d.user32.OpenClipboard(0)
handle = d.user32.GetClipboardData(CF_UNICODETEXT)
data = ctypes.c_wchar_p(handle).value
d.user32.CloseClipboard()
return data
def __init__(self, dllname, funcname):
self.__dllname = dllname
self.__funcname = funcname
self.__func = getattr(getattr(ctypes.windll, dllname), funcname)
def not_windows():
raise SystemError(
"Invalid platform. ctypes.windll must be available."
)
def not_windows():
raise SystemError(
"Invalid platform. ctypes.windll must be available."
)
def setUp(self):
self.mox = mox.Mox()
ctypes.windll = self.mox.CreateMockAnything()
ctypes.windll.kernel32 = self.mox.CreateMockAnything()
ctypes.windll.kernel32.CreateFileW = self.mox.CreateMockAnything()
ctypes.windll.kernel32.ReadDirectoryChangesW = self.mox.CreateMockAnything()
ctypes.windll.kernel32.CancelIoEx = self.mox.CreateMockAnything()
ctypes.windll.kernel32.CloseHandle = self.mox.CreateMockAnything()
ctypes.WinError = WinError
def test_with_no_change(self):
watcher = win32_file_watcher.Win32FileWatcher('/tmp')
ctypes.windll.kernel32.CreateFileW(
mox_c('/tmp'),
mox_c(1L),
mox_c(3L),
None,
mox_c(3L),
mox_c(win32_file_watcher._FILE_FLAG_BACKUP_SEMANTICS),
None).AndReturn(31415)
# pylint: disable=unused-argument
def found_nothing(
handle, buff, size, recursive, change_type, size_returned_by_ref,
unused1, unused2):
ctypes.cast(size_returned_by_ref, ctypes.POINTER(ctypes.c_ulong))[0] = 0L
ctypes.windll.kernel32.ReadDirectoryChangesW(
31415,
mox.IgnoreArg(),
mox_c(4096),
True,
mox_c(351),
mox.IgnoreArg(),
None,
None).WithSideEffects(found_nothing).AndReturn(-1)
ctypes.windll.kernel32.CancelIoEx(31415, None)
ctypes.windll.kernel32.CloseHandle(31415)
self.mox.ReplayAll()
watcher.start()
watcher.quit()
self.assertEqual(watcher.changes(), set())
self.mox.VerifyAll()
def test_with_error(self):
watcher = win32_file_watcher.Win32FileWatcher('/tmp')
ctypes.windll.kernel32.CreateFileW(
mox_c('/tmp'),
mox_c(1L),
mox_c(3L),
None,
mox_c(3L),
mox_c(win32_file_watcher._FILE_FLAG_BACKUP_SEMANTICS),
None).AndReturn(win32_file_watcher._INVALID_HANDLE_VALUE)
self.mox.ReplayAll()
self.assertRaises(WinError, watcher.start)
self.mox.VerifyAll()
def not_windows():
raise SystemError(
"Invalid platform. ctypes.windll must be available."
)
win_inet_pton.py 文件源码
项目:Livewire-Simple-Delegation-Switcher
作者: anthonyeden
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def not_windows():
raise SystemError(
"Invalid platform. ctypes.windll must be available."
)
def listen():
"""Listen for keyboard input."""
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg), 0, 0, 0)
def install_handle_input(self):
"""Install the hook."""
self.pointer = self.get_fptr()
self.hooked = ctypes.windll.user32.SetWindowsHookExA(
13,
self.pointer,
ctypes.windll.kernel32.GetModuleHandleW(None),
0
)
if not self.hooked:
return False
return True
def uninstall_handle_input(self):
"""Remove the hook."""
if self.hooked is None:
return
ctypes.windll.user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None
def listen():
"""Listen for mouse input."""
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg), 0, 0, 0)
def uninstall_handle_input(self):
"""Remove the hook."""
if self.hooked is None:
return
ctypes.windll.user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None
def _find_xinput(self):
"""Find most recent xinput library."""
for dll in XINPUT_DLL_NAMES:
try:
self.xinput = getattr(ctypes.windll, dll)
except OSError:
pass
else:
# We found an xinput driver
break
else:
# We didn't find an xinput library
warn("No xinput driver dll found, gamepads not supported.")
def listen():
"""Listen for keyboard input."""
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg), 0, 0, 0)
def uninstall_handle_input(self):
"""Remove the hook."""
if self.hooked is None:
return
ctypes.windll.user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None
def listen():
"""Listen for mouse input."""
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg), 0, 0, 0)
def install_handle_input(self):
"""Install the hook."""
self.pointer = self.get_fptr()
self.hooked = ctypes.windll.user32.SetWindowsHookExA(
14,
self.pointer,
ctypes.windll.kernel32.GetModuleHandleW(None),
0
)
if not self.hooked:
return False
return True
def uninstall_handle_input(self):
"""Remove the hook."""
if self.hooked is None:
return
ctypes.windll.user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None
def _find_xinput(self):
"""Find most recent xinput library."""
for dll in XINPUT_DLL_NAMES:
try:
self.xinput = getattr(ctypes.windll, dll)
except OSError:
pass
else:
# We found an xinput driver
break
else:
# We didn't find an xinput library
warn("No xinput driver dll found, gamepads not supported.")
def platform_specific_functions():
# use stddecl on windows, cdecl on all other platforms
d = {'library_loader' : ctypes.cdll
,'function_pointer' : ctypes.CFUNCTYPE
}
if platform.system() in ('Windows', 'Microsoft'):
d['library_loader'] = ctypes.windll
d['function_pointer'] = ctypes.WINFUNCTYPE
return d
def jvm_load(prefer=None):
"""
Loads the JVM dynamic library. The `prefer` argument works like for `jvm_find`. If the library
is already loaded, this produces a warning if `prefer` doesn't match the loaded library,
otherwise it does nothing.
"""
global libjvm, libjvm_type
if libjvm is not None:
if prefer is not None and prefer != libjvm_type:
import warnings
warnings.warn('Already loaded JVM "%s", so preferred JVM "%s" is unloadable'%(libjvm_type,prefer))
return
jvm,libjvm_type = jvm_find(prefer)
libjvm = (ctypes.windll if is_win else ctypes.cdll).LoadLibrary(jvm)
def quick_win_define(name, output, *args, **kwargs):
dllname, fname = name.split('.')
params = kwargs.get('params', None)
if params:
params = tuple([(x, ) for x in params])
func = (WINFUNCTYPE(output, *args))((fname, getattr(windll, dllname)), params)
err = kwargs.get('err', err_on_zero_or_null_check)
if err:
func.errcheck = err
return func
def __init__(self, window_name, with_alpha=False, bbox=None):
window = windll.user32.FindWindowW(None, window_name)
self.window = window
rect = GetClientRect(window)
self.width = rect.right - rect.left
self.height = rect.bottom - rect.top
if bbox:
bbox = [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]]
if not bbox[2] or not bbox[3]:
bbox[2] = self.width - bbox[0]
bbox[3] = self.height - bbox[1]
self.x, self.y, self.width, self.height = bbox
else:
self.x = 0
self.y = 0
self.windowDC = GetDC(window)
self.memoryDC = CreateCompatibleDC(self.windowDC)
self.bitmap = CreateCompatibleBitmap(self.windowDC, self.width, self.height)
self.bitmapInfo = BITMAPINFOHEADER()
self.bitmapInfo.biSize = sizeof(BITMAPINFOHEADER)
self.bitmapInfo.biPlanes = 1
self.bitmapInfo.biBitCount = 32 if with_alpha else 24
self.bitmapInfo.biWidth = self.width
self.bitmapInfo.biHeight = -self.height
self.bitmapInfo.biCompression = BI_RGB
self.bitmapInfo.biSizeImage = 0
self.channels = 4 if with_alpha else 3
self.closed = False
def test_with_change(self):
watcher = win32_file_watcher.Win32FileWatcher('/tmp')
ctypes.windll.kernel32.CreateFileW(
mox_c('/tmp'),
mox_c(1L),
mox_c(3L),
None,
mox_c(3L),
mox_c(win32_file_watcher._FILE_FLAG_BACKUP_SEMANTICS),
None).AndReturn(31415)
# pylint: disable=unused-argument
def found_something(
handle, buff, size, recursive, change_type, size_returned_by_ref,
unused1, unused2):
parray = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # next offset = 0
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # Action
0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 4 * 10 chrs
0x74, 0x00, 0x00, 0x00, # 't'
0x65, 0x00, 0x00, 0x00, # 'e'
0x73, 0x00, 0x00, 0x00, # 's'
0x74, 0x00, 0x00, 0x00, # 't'
0x20, 0x00, 0x00, 0x00, # ' '
0x73, 0x00, 0x00, 0x00, # 's'
0x74, 0x00, 0x00, 0x00, # 't'
0x75, 0x00, 0x00, 0x00, # 'u'
0x66, 0x00, 0x00, 0x00, # 'f'
0x66, 0x00, 0x00, 0x00] # 'f'
nbuff = (ctypes.c_ubyte * len(parray))(*parray)
ctypes.memmove(
buff,
ctypes.addressof(nbuff),
ctypes.sizeof(nbuff))
psize = ctypes.cast(size_returned_by_ref, ctypes.POINTER(ctypes.c_ulong))
psize[0] = ctypes.sizeof(nbuff)
ctypes.windll.kernel32.ReadDirectoryChangesW(
31415,
mox.IgnoreArg(),
mox_c(win32_file_watcher._BUFF_SIZE),
True,
mox_c(351),
mox.IgnoreArg(),
None,
None).WithSideEffects(found_something).AndReturn(1)
ctypes.windll.kernel32.CancelIoEx(31415, None)
ctypes.windll.kernel32.CloseHandle(31415)
self.mox.ReplayAll()
watcher.start()
watcher.quit()
self.assertEqual(watcher.changes(), {'test stuff'})
self.mox.VerifyAll()
def _count_devices(self):
"""See what Windows' GetRawInputDeviceList wants to tell us.
For now, we are just seeing if there is at least one keyboard
and/or mouse attached.
GetRawInputDeviceList could be used to help distinguish between
different keyboards and mice on the system in the way Linux
can. However, Roma uno die non est condita.
"""
number_of_devices = ctypes.c_uint()
if ctypes.windll.user32.GetRawInputDeviceList(
ctypes.POINTER(ctypes.c_int)(),
ctypes.byref(number_of_devices),
ctypes.sizeof(RawInputDeviceList)) == -1:
warn("Call to GetRawInputDeviceList was unsuccessful."
"We have no idea if a mouse or keyboard is attached.",
RuntimeWarning)
return
devices_found = (RawInputDeviceList * number_of_devices.value)()
if ctypes.windll.user32.GetRawInputDeviceList(
devices_found,
ctypes.byref(number_of_devices),
ctypes.sizeof(RawInputDeviceList)) == -1:
warn("Call to GetRawInputDeviceList was unsuccessful."
"We have no idea if a mouse or keyboard is attached.",
RuntimeWarning)
return
for device in devices_found:
if device.dwType == 0:
self._raw_device_counts['mice'] += 1
elif device.dwType == 1:
self._raw_device_counts['keyboards'] += 1
elif device.dwType == 2:
self._raw_device_counts['otherhid'] += 1
else:
self._raw_device_counts['unknown'] += 1
def _count_devices(self):
"""See what Windows' GetRawInputDeviceList wants to tell us.
For now, we are just seeing if there is at least one keyboard
and/or mouse attached.
GetRawInputDeviceList could be used to help distinguish between
different keyboards and mice on the system in the way Linux
can. However, Roma uno die non est condita.
"""
number_of_devices = ctypes.c_uint()
if ctypes.windll.user32.GetRawInputDeviceList(
ctypes.POINTER(ctypes.c_int)(),
ctypes.byref(number_of_devices),
ctypes.sizeof(RawInputDeviceList)) == -1:
warn("Call to GetRawInputDeviceList was unsuccessful."
"We have no idea if a mouse or keyboard is attached.",
RuntimeWarning)
return
devices_found = (RawInputDeviceList * number_of_devices.value)()
if ctypes.windll.user32.GetRawInputDeviceList(
devices_found,
ctypes.byref(number_of_devices),
ctypes.sizeof(RawInputDeviceList)) == -1:
warn("Call to GetRawInputDeviceList was unsuccessful."
"We have no idea if a mouse or keyboard is attached.",
RuntimeWarning)
return
for device in devices_found:
if device.dwType == 0:
self._raw_device_counts['mice'] += 1
elif device.dwType == 1:
self._raw_device_counts['keyboards'] += 1
elif device.dwType == 2:
self._raw_device_counts['otherhid'] += 1
else:
self._raw_device_counts['unknown'] += 1