def SetClipboardText(text): charBuffer = create_unicode_buffer(text) charBufferSize = sizeof(charBuffer) hGlobalMem = GlobalAlloc(GHND, charBufferSize) lpGlobalMem = GlobalLock(hGlobalMem) memcpy(lpGlobalMem, charBuffer, charBufferSize) GlobalUnlock(hGlobalMem) if not SafeOpenClipboard(): return try: EmptyClipboard() SetClipboardData(CF_UNICODETEXT, hGlobalMem) finally: CloseClipboard()
def GetComPorts(availableOnly=True): """ Scans the registry for serial ports and return a list of (port, desc, hwid) tuples. If availableOnly is true only return currently existing ports. """ result = [] stringBuffer = create_unicode_buffer(256) flags = DIGCF_DEVICEINTERFACE if availableOnly: flags |= DIGCF_PRESENT hdi = SetupDiGetClassDevs(byref(GUID_CLASS_COMPORT), None, 0, flags) if hdi == INVALID_HANDLE_VALUE: raise WinError() dwRequiredSize = DWORD() dwIndex = 0 while True: did = SP_DEVICE_INTERFACE_DATA() did.cbSize = sizeof(did) if not SetupDiEnumDeviceInterfaces( hdi, None, byref(GUID_CLASS_COMPORT), dwIndex, byref(did) ): err = GetLastError() if err != ERROR_NO_MORE_ITEMS: raise WinError(err) break # get the size if not SetupDiGetDeviceInterfaceDetail( hdi, byref(did), None, 0, byref(dwRequiredSize), None ): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) # allocate buffer class _SP_DEVICE_INTERFACE_DETAIL_DATA(Structure): _fields_ = [ ('cbSize', DWORD), ('DevicePath', TCHAR*(dwRequiredSize.value - sizeof(DWORD))), ] idd = _SP_DEVICE_INTERFACE_DETAIL_DATA() idd.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) devinfo = SP_DEVINFO_DATA() devinfo.cbSize = sizeof(devinfo) if not SetupDiGetDeviceInterfaceDetail( hdi, byref(did), cast(byref(idd), PSP_DEVICE_INTERFACE_DETAIL_DATA), dwRequiredSize, None, byref(devinfo) ): raise WinError() # hardware ID if not SetupDiGetDeviceRegistryProperty( hdi, byref(devinfo), SPDRP_HARDWAREID, None, cast(stringBuffer, PBYTE), sizeof(stringBuffer)-1, None ): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) szHardwareID = stringBuffer.value # friendly name if not SetupDiGetDeviceRegistryProperty( hdi, byref(devinfo), SPDRP_FRIENDLYNAME, None, cast(stringBuffer, PBYTE), sizeof(stringBuffer)-1, None ): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) szFriendlyName = stringBuffer.value portName = re.search(r"\((.*)\)", szFriendlyName).group(1) result.append((portName, szFriendlyName, szHardwareID)) dwIndex += 1 SetupDiDestroyDeviceInfoList(hdi) return result
def GetComPorts(availableOnly=True): """ Scans the registry for serial ports and return a list of (port, desc, hwid) tuples. If availableOnly is true only return currently existing ports. """ result = [] stringBuffer = create_unicode_buffer(256) flags = DIGCF_DEVICEINTERFACE if availableOnly: flags |= DIGCF_PRESENT hdi = SetupDiGetClassDevs(byref(GUID_CLASS_COMPORT), None, 0, flags) if hdi == INVALID_HANDLE_VALUE: raise WinError() dwRequiredSize = DWORD() dwIndex = 0 while True: did = SP_DEVICE_INTERFACE_DATA() did.cbSize = sizeof(did) if not SetupDiEnumDeviceInterfaces( hdi, None, byref(GUID_CLASS_COMPORT), dwIndex, byref(did)): err = GetLastError() if err != ERROR_NO_MORE_ITEMS: raise WinError(err) break # get the size if not SetupDiGetDeviceInterfaceDetail(hdi, byref(did), None, 0, byref(dwRequiredSize), None): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) # allocate buffer class _SP_DEVICE_INTERFACE_DETAIL_DATA(Structure): _fields_ = [ ('cbSize', DWORD), ('DevicePath', TCHAR * (dwRequiredSize.value - sizeof(DWORD))), ] idd = _SP_DEVICE_INTERFACE_DETAIL_DATA() idd.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) devinfo = SP_DEVINFO_DATA() devinfo.cbSize = sizeof(devinfo) if not SetupDiGetDeviceInterfaceDetail( hdi, byref(did), cast(byref(idd), PSP_DEVICE_INTERFACE_DETAIL_DATA), dwRequiredSize, None, byref(devinfo)): raise WinError() # hardware ID if not SetupDiGetDeviceRegistryProperty( hdi, byref(devinfo), SPDRP_HARDWAREID, None, cast(stringBuffer, PBYTE), sizeof(stringBuffer) - 1, None): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) szHardwareID = stringBuffer.value # friendly name if not SetupDiGetDeviceRegistryProperty( hdi, byref(devinfo), SPDRP_FRIENDLYNAME, None, cast(stringBuffer, PBYTE), sizeof(stringBuffer) - 1, None): # Ignore ERROR_INSUFFICIENT_BUFFER err = GetLastError() if err != ERROR_INSUFFICIENT_BUFFER: raise WinError(err) szFriendlyName = stringBuffer.value portName = re.search(r"\((.*)\)", szFriendlyName).group(1) result.append((portName, szFriendlyName, szHardwareID)) dwIndex += 1 SetupDiDestroyDeviceInfoList(hdi) return result