alencar.expro 0 Newbie Poster

(This code returns ERROR_INVALID_PARAMETER)

Option Explicit

Private Type DISPLAY_DEVICE
cb As Long
DeviceName As String * 32
DeviceString As String * 128
StateFlags As Long
DeviceID As String * 128
DeviceKey As String * 128
End Type

Private Type POINTL
x As Long
y As Long
End Type

Private Enum DISPLAYCONFIG_PIXELFORMAT
DISPLAYCONFIG_PIXELFORMAT_8BPP = &H1
DISPLAYCONFIG_PIXELFORMAT_16BPP = &H2
DISPLAYCONFIG_PIXELFORMAT_24BPP = &H3
DISPLAYCONFIG_PIXELFORMAT_32BPP = &H4
DISPLAYCONFIG_PIXELFORMAT_NONGDI = &H5
DISPLAYCONFIG_PIXELFORMAT_FORCE_UINT32 = &HFFFFFFFF
End Enum

Private Enum DISPLAYCONFIG_MODE_INFO_TYPE
DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE = &H1
DISPLAYCONFIG_MODE_INFO_TYPE_TARGET = &H2
DISPLAYCONFIG_MODE_INFO_TYPE_FORCE_UINT32 = &HFFFFFFFF
End Enum

Private Type DISPLAYCONFIG_RATIONAL
Numerator As Long
Denominator As Long
End Type

Private Type DISPLAYCONFIG_2DREGION
cx As Long
cy As Long
End Type

Private Enum DISPLAYCONFIG_SCANLINE_ORDERING
DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED = &H0
DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE = &H1
DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED = &H2
DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST = DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED
DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST = &H3
DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32 = &HFFFFFFFF
End Enum

Private Type DISPLAYCONFIG_VIDEO_SIGNAL_INFO
pixelRate As Long
hSyncFreq As DISPLAYCONFIG_RATIONAL
vSyncFreq As DISPLAYCONFIG_RATIONAL
activeSize As DISPLAYCONFIG_2DREGION
totalSize As DISPLAYCONFIG_2DREGION
videoStandard As Long
scanLineOrdering As DISPLAYCONFIG_SCANLINE_ORDERING
End Type

Private Type DISPLAYCONFIG_TARGET_MODE
targetVideoSignalInfo As DISPLAYCONFIG_VIDEO_SIGNAL_INFO
End Type

Private Type DISPLAYCONFIG_SOURCE_MODE
width As Long
height As Long
pixelFormat As DISPLAYCONFIG_PIXELFORMAT
position As POINTL
End Type

Private Type DISPLAYCONFIG_MODE_INFO
infoType As DISPLAYCONFIG_MODE_INFO_TYPE
id As Long
adapterId As String
targetMode As DISPLAYCONFIG_TARGET_MODE
sourceMode As DISPLAYCONFIG_SOURCE_MODE
End Type

Private Type DISPLAYCONFIG_PATH_SOURCE_INFO
adapterId As String
id As Long
modeInfoIdx As Long
statusFlags As Long
End Type

Private Type DISPLAYCONFIG_PATH_TARGET_INFO
adapterId As String
id As Long
modeInfoIdx As Long
outputTechnology As Long
rotation As Long
scaling As Long
refreshRate As DISPLAYCONFIG_RATIONAL
scanLineOrdering As Long
targetAvailable As Boolean
statusFlags As Long
End Type

Private Type DISPLAYCONFIG_PATH_INFO
sourceInfo As DISPLAYCONFIG_PATH_SOURCE_INFO
targetInfo As DISPLAYCONFIG_PATH_TARGET_INFO
Flags As Long
End Type

Private Enum DISPLAYCONFIG_TOPOLOGY_ID
DISPLAYCONFIG_TOPOLOGY_INTERNAL = &H1
DISPLAYCONFIG_TOPOLOGY_CLONE = &H2
DISPLAYCONFIG_TOPOLOGY_EXTEND = &H4
DISPLAYCONFIG_TOPOLOGY_EXTERNAL = &H8
DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32 = &HFFFFFFFF
End Enum

Private Enum DISPLAYCONFIG_DEVICE_INFO_TYPE
DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME = &H1
DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME = &H2
DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_PREFERRED_MODE = &H3
DISPLAYCONFIG_DEVICE_INFO_GET_ADAPTER_NAME = &H4
DISPLAYCONFIG_DEVICE_INFO_SET_TARGET_PERSISTENCE = &H5
DISPLAYCONFIG_DEVICE_INFO_FORCE_UINT32 = &HFFFFFFFF
End Enum

Private Type DISPLAYCONFIG_DEVICE_INFO_HEADER
type As DISPLAYCONFIG_DEVICE_INFO_TYPE
size As Long
adapterId As String
id As Long
End Type

Private Declare Function GetDisplayConfigBufferSizes Lib "user32" (Flags As Long, pNumPathArrayElements As Long, pNumModeInfoArrayElements As Long) As Long
Private Declare Function QueryDisplayConfig Lib "user32" (Flags As Long, pNumPathArrayElementsas As Long, pPathInfoArray As DISPLAYCONFIG_PATH_INFO, pNumModeInfoArrayElements As Long, pModeInfoArray As DISPLAYCONFIG_MODE_INFO, pCurrentTopologyId As DISPLAYCONFIG_TOPOLOGY_ID) As Long
Private Declare Function SetDisplayConfig Lib "user32" (numPathArrayElements As Long, pathArray As DISPLAYCONFIG_PATH_INFO, numModeInfoArrayElements As Long, modeInfoArray As DISPLAYCONFIG_MODE_INFO, Flags As Long) As Long
Private Declare Function DisplayConfigGetDeviceInfo Lib "user32" (requestPacket As DISPLAYCONFIG_DEVICE_INFO_HEADER) As Long
Private Declare Function DisplayConfigSetDeviceInfo Lib "user32" (setPacket As DISPLAYCONFIG_DEVICE_INFO_HEADER) As Long

Const MONITOR_DEFAULTTONULL As Long = &H0
Const MONITOR_DEFAULTTOPRIMARY As Long = &H1
Const MONITOR_DEFAULTTONEAREST As Long = &H2

Const QDC_ALL_PATHS As Long = &H1
Const QDC_ONLY_ACTIVE_PATHS As Long = &H2
Const QDC_DATABASE_CURRENT As Long = &H4

Const SDC_TOPOLOGY_INTERNAL As Long = &H1
Const SDC_TOPOLOGY_CLONE As Long = &H2
Const SDC_TOPOLOGY_EXTEND As Long = &H4
Const SDC_TOPOLOGY_EXTERNAL As Long = &H8
Const SDC_TOPOLOGY_SUPPLIED As Long = &H10
Const SDC_USE_DATABASE_CURRENT As Long = (SDC_TOPOLOGY_INTERNAL Or _
SDC_TOPOLOGY_CLONE Or _
SDC_TOPOLOGY_EXTEND Or _
SDC_TOPOLOGY_EXTERNAL)
Const SDC_USE_SUPPLIED_DISPLAY_CONFIG As Long = &H20
Const SDC_VALIDATE As Long = &H40
Const SDC_APPLY As Long = &H80
Const SDC_NO_OPTIMIZATION As Long = &H100
Const SDC_SAVE_TO_DATABASE As Long = &H200
Const SDC_ALLOW_CHANGES As Long = &H400
Const SDC_PATH_PERSIST_IF_REQUIRED As Long = &H800
Const SDC_FORCE_MODE_ENUMERATION As Long = &H1000
Const SDC_ALLOW_PATH_ORDER_CHANGES As Long = &H2000

Const ERROR_SUCCESS As Long = &H0 '0
Const ERROR_INVALID_PARAMETER As Long = &H57 '87
Const ERROR_NOT_SUPPORTED As Long = &H32 '50
Const ERROR_ACCESS_DENIED As Long = &H5 '5
Const ERROR_GEN_FAILURE As Long = &H1F '31
Const ERROR_BAD_CONFIGURATION As Long = &H488 '1160

Private Sub Form_Load()
Dim pathArraySize As Long
Dim modeArraySize As Long

Dim pathArray As DISPLAYCONFIG_PATH_INFO
Dim modeInfoArray As DISPLAYCONFIG_MODE_INFO
Dim currentTopology As DISPLAYCONFIG_TOPOLOGY_ID

Dim ret As Long

'Obtém todos os privilégios do sistema operacional
Call SetAllPrivileges

ret = GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, pathArraySize, modeArraySize)

Call ERR_RAISE(ret, "GetDisplayConfigBufferSizes")

ret = QueryDisplayConfig(QDC_DATABASE_CURRENT, _
pathArraySize, _
pathArray, _
modeArraySize, _
modeInfoArray, _
currentTopology)


Call ERR_RAISE(ret, "QueryDisplayConfig")

ret = SetDisplayConfig(Len(pathArray), _
pathArray, _
Len(modeInfoArray), _
modeInfoArray, _
SDC_APPLY Or _
SDC_SAVE_TO_DATABASE Or _
SDC_ALLOW_CHANGES Or _
SDC_USE_SUPPLIED_DISPLAY_CONFIG)

Call ERR_RAISE(ret, "SetDisplayConfig")

End Sub

Private Sub ERR_RAISE(ret As Long, sFunction As String)
Select Case ret
Case ERROR_SUCCESS
MsgBox "SUCCESS", , sFunction
Case ERROR_INVALID_PARAMETER
MsgBox "INVALID PARAMETER", , sFunction
Case ERROR_NOT_SUPPORTED
MsgBox "NOT SUPPORTED", , sFunction
Case ERROR_ACCESS_DENIED
MsgBox "ACCESS DENIED", , sFunction
Case ERROR_GEN_FAILURE
MsgBox "GEN FAILURE", , sFunction
Case ERROR_BAD_CONFIGURATION
MsgBox "BAD CONFIGURATION", , sFunction
End Select
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.