def open(self):
"""
Open device to query properties
:return: context
"""
handle = _setupapi.SetupDiCreateDeviceInfoList(None, None)
if handle == -1:
err_no = ctypes.GetLastError()
raise WindowsError(err_no, ctypes.FormatError(err_no))
try:
dev_info = DeviceInfoData()
if not _setupapi.SetupDiOpenDeviceInfoW(handle, ctypes.create_unicode_buffer(self._instance_id), None,
DIOD_INHERIT_CLASSDRVS, ctypes.byref(dev_info)):
err_no = ctypes.GetLastError()
raise WindowsError(err_no, ctypes.FormatError(err_no))
self._handle = (handle, dev_info, self._handle) # Stack
yield self
finally:
if self._handle is not None and \
self._handle[0] == handle: # If last handle is opened in this function, pop it
self._handle = self._handle[2]
_setupapi.SetupDiDestroyDeviceInfoList(handle) # Close handle
评论列表
文章目录