Example #1
0
def GetDisplayModes():
    res = []
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    devMode = DEVMODE()
    devMode.dmSize = sizeof(DEVMODE)
    iDevNum = 0
    while True:
        if EnumDisplayDevices(None, iDevNum, pointer(displayDevice), 0) == 0:
            break
        iDevNum += 1
        if displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER:
            continue
        EnumDisplaySettingsEx(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS,
                              pointer(devMode), 0)
        displayMode = (
            displayDevice.DeviceName,
            devMode.dmPosition.x,
            devMode.dmPosition.y,
            devMode.dmPelsWidth,
            devMode.dmPelsHeight,
            devMode.dmDisplayFrequency,
            devMode.dmBitsPerPel,
            bool(displayDevice.StateFlags
                 & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP),
            bool(displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE),
            devMode.dmDisplayFlags,
        )
        res.append(displayMode)
    return tuple(res)
Example #2
0
def GetDisplayModes():
    res = []
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    devMode = DEVMODE()
    devMode.dmSize = sizeof(DEVMODE)
    iDevNum = 0
    while True:
        if EnumDisplayDevices(None, iDevNum, pointer(displayDevice), 0) == 0:
            break
        iDevNum += 1
        if displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER:
            continue
        EnumDisplaySettingsEx(
            displayDevice.DeviceName,
            ENUM_CURRENT_SETTINGS,
            pointer(devMode),
            0
        )
        displayMode = (
            displayDevice.DeviceName,
            devMode.dmPosition.x,
            devMode.dmPosition.y,
            devMode.dmPelsWidth,
            devMode.dmPelsHeight,
            devMode.dmDisplayFrequency,
            devMode.dmBitsPerPel,
            bool(
                displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
            ),
            bool(displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE),
            devMode.dmDisplayFlags,
        )
        res.append(displayMode)
    return tuple(res)
Example #3
0
def PyEnumProcesses():
    size = 1024
    pBytesReturned = DWORD()
    while True:
        data = (DWORD * size)()
        dataSize = size * sizeof(DWORD)
        res = EnumProcesses(data, dataSize, byref(pBytesReturned))
        if res == 0:
            raise WinError()
        if pBytesReturned.value != dataSize:
            break
        size *= 10
    return data[:pBytesReturned.value / sizeof(DWORD)]
Example #4
0
def PyEnumProcesses():
    size = 1024
    pBytesReturned = DWORD()
    while True:
        data = (DWORD * size)()
        dataSize = size * sizeof(DWORD)
        res = EnumProcesses(data, dataSize, byref(pBytesReturned))
        if res == 0:
            raise WinError()
        if pBytesReturned.value != dataSize:
            break
        size *= 10
    return data[:pBytesReturned.value / sizeof(DWORD)]
Example #5
0
def GetDisplays():
    res = []
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    iDevNum = 0
    while True:
        if EnumDisplayDevices(None, iDevNum, pointer(displayDevice), 0) == 0:
            break
        #DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 1
        if not (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER):
            disp = Display(iDevNum, displayDevice)
            res.append(disp)
            displayDevice = DISPLAY_DEVICE()
            displayDevice.cb = sizeof(DISPLAY_DEVICE)
        iDevNum += 1
    return res
Example #6
0
def GetDisplays():
    res = []
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    iDevNum = 0
    while True:
        if EnumDisplayDevices(None, iDevNum, pointer(displayDevice), 0) == 0:
            break
        #DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 1
        if not (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER):
            disp = Display(iDevNum, displayDevice)
            res.append(disp)
            displayDevice = DISPLAY_DEVICE()
            displayDevice.cb = sizeof(DISPLAY_DEVICE)
        iDevNum += 1
    return res
Example #7
0
 def __init__(self, iDevNum, displayDevice):
     self.iDevNum = iDevNum
     self.deviceName = displayDevice.DeviceName
     self.deviceString = displayDevice.DeviceString
     self.isPrimary = bool(displayDevice.StateFlags
                           & DISPLAY_DEVICE_PRIMARY_DEVICE)
     self.devMode = DEVMODE()
     self.devMode.dmSize = sizeof(DEVMODE)
     self.dmp = pointer(self.devMode)
Example #8
0
 def __init__(self, iDevNum, displayDevice):
     self.iDevNum = iDevNum
     self.deviceName = displayDevice.DeviceName
     self.deviceString = displayDevice.DeviceString
     self.isPrimary = bool(
         displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE
     )
     self.devMode = DEVMODE()
     self.devMode.dmSize = sizeof(DEVMODE)
     self.dmp = pointer(self.devMode)
Example #9
0
 def SetDisplayMode(self, size, frequency, bitdepth, flags=0):
     devMode = DEVMODE()
     devMode.dmSize = sizeof(DEVMODE)
     devMode.dmPelsWidth = size[0]
     devMode.dmPelsHeight = size[1]
     devMode.dmBitsPerPel = bitdepth
     devMode.dmDisplayFrequency = frequency
     devMode.dmFields = (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT
                         | DM_DISPLAYFREQUENCY)
     ChangeDisplaySettingsEx(self.deviceName, pointer(devMode), 0, flags, 0)
Example #10
0
 def GetStatus(self):
     dwBytesNeeded = DWORD()
     result = QueryServiceStatusEx(
         self.schService,  # handle to service
         SC_STATUS_PROCESS_INFO,  # information level
         cast(byref(self.ssStatus), LPBYTE),  # address of structure
         sizeof(self.ssStatus),  # size of structure
         byref(dwBytesNeeded)  # size needed if buffer is too small
     )
     if not result:
         raise WinError()
     return self.ssStatus
Example #11
0
 def SetDisplayMode(self, size, frequency, bitdepth, flags=0):
     devMode = DEVMODE()
     devMode.dmSize = sizeof(DEVMODE)
     devMode.dmPelsWidth = size[0]
     devMode.dmPelsHeight = size[1]
     devMode.dmBitsPerPel = bitdepth
     devMode.dmDisplayFrequency = frequency
     devMode.dmFields = (
         DM_BITSPERPEL |
         DM_PELSWIDTH |
         DM_PELSHEIGHT |
         DM_DISPLAYFREQUENCY
     )
     ChangeDisplaySettingsEx(self.deviceName, pointer(devMode), 0, flags, 0)
Example #12
0
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()
Example #13
0
def RunAs(filePath, asAdministrator, *args):
    sei = SHELLEXECUTEINFO()
    sei.cbSize = sizeof(SHELLEXECUTEINFO)
    sei.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS
    if asAdministrator:
        sei.lpVerb = u"runas"
    else:
        sei.lpVerb = u""
    sei.lpFile = GetUncPathOf(filePath)
    sei.lpParameters = " ".join(['"%s"' % arg.replace('"', '""') for arg in args])
    sei.nShow = SW_SHOWNORMAL
    if not ctypes.windll.shell32.ShellExecuteExW(byref(sei)):
        err = GetLastError()
        raise WindowsError(err, "ShellExecuteEx: %s" % FormatError(err))
    return sei.hProcess
Example #14
0
def RunAs(filePath, asAdministrator, *args):
    sei = SHELLEXECUTEINFO()
    sei.cbSize = sizeof(SHELLEXECUTEINFO)
    sei.fMask = (SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI
                 | SEE_MASK_NOCLOSEPROCESS)
    if asAdministrator:
        sei.lpVerb = u"runas"
    else:
        sei.lpVerb = u""
    sei.lpFile = GetUncPathOf(filePath)
    sei.lpParameters = " ".join(
        ['"%s"' % arg.replace('"', '""') for arg in args])
    sei.nShow = SW_SHOWNORMAL
    if not ctypes.windll.shell32.ShellExecuteExW(byref(sei)):
        err = GetLastError()
        raise WindowsError(err, "ShellExecuteEx: %s" % FormatError(err))
    return sei.hProcess
Example #15
0
 def GetDisplayModes(self, allModes=False):
     devMode = DEVMODE()
     devMode.dmSize = sizeof(DEVMODE)
     lpDevMode = pointer(devMode)
     modes = {}
     if allModes:
         flag = EDS_RAWMODE
     else:
         flag = 0
     iModeNum = 0
     while 0 != EnumDisplaySettingsEx(self.deviceName, iModeNum, lpDevMode,
                                      flag):
         iModeNum += 1
         resolution = (devMode.dmPelsWidth, devMode.dmPelsHeight)
         deepthDict = modes.setdefault(resolution, {})
         frequencyList = deepthDict.setdefault(devMode.dmBitsPerPel, [])
         frequencyList.append(devMode.dmDisplayFrequency)
     return modes
Example #16
0
 def GetDisplayModes(self, allModes=False):
     devMode = DEVMODE()
     devMode.dmSize = sizeof(DEVMODE)
     lpDevMode = pointer(devMode)
     modes = {}
     if allModes:
         flag = EDS_RAWMODE
     else:
         flag = 0
     iModeNum = 0
     while 0 != EnumDisplaySettingsEx(
         self.deviceName, iModeNum, lpDevMode, flag
     ):
         iModeNum += 1
         resolution = (devMode.dmPelsWidth, devMode.dmPelsHeight)
         deepthDict = modes.setdefault(resolution, {})
         frequencyList = deepthDict.setdefault(devMode.dmBitsPerPel, [])
         frequencyList.append(devMode.dmDisplayFrequency)
     return modes
Example #17
0
def SetDisplayModes(*args):
    for (deviceName, x, y, width, height, freq, bitdepth, isAttached,
         isPrimary, displayFlags) in args:
        devMode = DEVMODE()
        devMode.dmSize = sizeof(DEVMODE)
        if isAttached:
            devMode.dmPosition.x = x
            devMode.dmPosition.y = y
            devMode.dmPelsWidth = width
            devMode.dmPelsHeight = height
            devMode.dmBitsPerPel = bitdepth
            devMode.dmDisplayFrequency = freq
            devMode.dmDisplayFlags = displayFlags
        devMode.dmFields = (DM_POSITION | DM_BITSPERPEL | DM_PELSWIDTH
                            | DM_PELSHEIGHT | DM_DISPLAYFLAGS
                            | DM_DISPLAYFREQUENCY)
        flags = (CDS_UPDATEREGISTRY | CDS_NORESET)
        if isPrimary:
            flags |= CDS_SET_PRIMARY
        ChangeDisplaySettingsEx(deviceName, pointer(devMode), 0, flags, 0)
    ChangeDisplaySettingsEx(None, None, 0, 0, 0)
Example #18
0
def SetDisplayModes(*args):
    for (
        deviceName,
        x,
        y,
        width,
        height,
        freq,
        bitdepth,
        isAttached,
        isPrimary,
        displayFlags
    ) in args:
        devMode = DEVMODE()
        devMode.dmSize = sizeof(DEVMODE)
        if isAttached:
            devMode.dmPosition.x = x
            devMode.dmPosition.y = y
            devMode.dmPelsWidth = width
            devMode.dmPelsHeight = height
            devMode.dmBitsPerPel = bitdepth
            devMode.dmDisplayFrequency = freq
            devMode.dmDisplayFlags = displayFlags
        devMode.dmFields = (
            DM_POSITION |
            DM_BITSPERPEL |
            DM_PELSWIDTH |
            DM_PELSHEIGHT |
            DM_DISPLAYFLAGS |
            DM_DISPLAYFREQUENCY
        )
        flags = (CDS_UPDATEREGISTRY | CDS_NORESET)
        if isPrimary:
            flags |= CDS_SET_PRIMARY
        ChangeDisplaySettingsEx(deviceName, pointer(devMode), 0, flags, 0)
    ChangeDisplaySettingsEx(None, None, 0, 0, 0)
Example #19
0
 class _SP_DEVICE_INTERFACE_DETAIL_DATA(Structure):
     _fields_ = [
         ('cbSize', DWORD),
         ('DevicePath', TCHAR * (dwRequiredSize.value - sizeof(DWORD))),
     ]
Example #20
0
def GetComPorts(availableOnly=True):
    """
    This generator scans the device registry for com ports and yields port,
    desc, hwid.
    If availableOnly is true only return currently existing ports.
    """
    stringBuffer = ctypes.create_unicode_buffer(256)
    flags = DIGCF_DEVICEINTERFACE
    if availableOnly:
        flags |= DIGCF_PRESENT
    hdi = SetupDiGetClassDevs(byref(GUID_CLASS_COMPORT), None, NULL, flags)
    #~ for i in range(256):
    for dwIndex in range(256):
        did = SP_DEVICE_INTERFACE_DATA()
        did.cbSize = sizeof(did)

        if not SetupDiEnumDeviceInterfaces(
            hdi,
            None,
            byref(GUID_CLASS_COMPORT),
            dwIndex,
            byref(did)
        ):
            if ctypes.GetLastError() != ERROR_NO_MORE_ITEMS:
                raise ctypes.WinError()
            break

        dwNeeded = DWORD()
        # get the size
        if not SetupDiGetDeviceInterfaceDetail(
            hdi,
            byref(did),
            None,
            0,
            byref(dwNeeded),
            None
        ):
            # Ignore ERROR_INSUFFICIENT_BUFFER
            if ctypes.GetLastError() != ERROR_INSUFFICIENT_BUFFER:
                raise ctypes.WinError()
        # allocate buffer
        class _SP_DEVICE_INTERFACE_DETAIL_DATA(ctypes.Structure):
            _fields_ = [
                ('cbSize', DWORD),
                ('DevicePath', TCHAR*(dwNeeded.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),
            dwNeeded,
            None,
            byref(devinfo)
        ):
            raise ctypes.WinError()
        #print idd.DevicePath, sizeof(idd)
        # hardware ID
        if not SetupDiGetDeviceRegistryProperty(
            hdi,
            byref(devinfo),
            SPDRP_HARDWAREID,
            None,
            cast(stringBuffer, PBYTE),
            sizeof(stringBuffer) - 1,
            None
        ):
            # Ignore ERROR_INSUFFICIENT_BUFFER
            if ctypes.GetLastError() != ERROR_INSUFFICIENT_BUFFER:
                raise ctypes.WinError()
        szHardwareID = stringBuffer.value
        # friendly name
        #szFriendlyName = ctypes.create_string_buffer('\0' * 250)
        if not SetupDiGetDeviceRegistryProperty(
            hdi,
            byref(devinfo),
            SPDRP_FRIENDLYNAME,
            None,
            cast(stringBuffer, PBYTE),
            sizeof(stringBuffer) - 1,
            None
        ):
            # Ignore ERROR_INSUFFICIENT_BUFFER
            if ctypes.GetLastError() != ERROR_INSUFFICIENT_BUFFER:
                raise ctypes.WinError()
        szFriendlyName = stringBuffer.value
        portName = re.search(r"\((.*)\)", szFriendlyName).group(1)
        yield portName, szFriendlyName, szHardwareID

    SetupDiDestroyDeviceInfoList(hdi)
Example #21
0
def GetDisplay(displayNum):
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    if EnumDisplayDevices(None, displayNum, pointer(displayDevice), 0) == 0:
        return None
    return Display(displayNum, displayDevice)
Example #22
0
def GetDisplay(displayNum):
    displayDevice = DISPLAY_DEVICE()
    displayDevice.cb = sizeof(DISPLAY_DEVICE)
    if EnumDisplayDevices(None, displayNum, pointer(displayDevice), 0) == 0:
        return None
    return Display(displayNum, displayDevice)
Example #23
0
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
Example #24
0
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