Skip to content

Instantly share code, notes, and snippets.

@Tey
Created April 13, 2019 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tey/2c29b290c1aa6f18072a5ab16a07f8be to your computer and use it in GitHub Desktop.
Save Tey/2c29b290c1aa6f18072a5ab16a07f8be to your computer and use it in GitHub Desktop.
Windows 10 display detection API
// cl /EHsc Win10DetectDisplay.cpp
/* This program forces detection of added (or removed) display screens
* It does the same thing as when clicking on the "Detect" button in Windows display settings
* This can be used to create a shortcut to refresh displays list
*/
#define UNICODE
#define _UNICODE
#include <windows.h>
/* d3dkmthk.h - BEGIN */
typedef UINT D3DKMT_HANDLE; // d3dukmdt.h
typedef struct _D3DKMT_POLLDISPLAYCHILDREN
{
D3DKMT_HANDLE hAdapter; // in: Adapter handle
UINT NonDestructiveOnly : 1; // in: 0x00000001 Destructive or not
UINT SynchronousPolling : 1; // in: 0x00000002 Synchronous polling or not
UINT DisableModeReset : 1; // in: 0x00000004 Disable DMM mode reset on monitor event
UINT PollAllAdapters : 1; // in: 0x00000008 Poll all adapters
UINT PollInterruptible : 1; // in: 0x00000010 Poll interruptible targets as well.
UINT Reserved : 27; // in: 0xffffffc0
} D3DKMT_POLLDISPLAYCHILDREN;
EXTERN_C __checkReturn NTSTATUS APIENTRY D3DKMTPollDisplayChildren(__in CONST D3DKMT_POLLDISPLAYCHILDREN*);
/* d3dkmthk.h - END */
#pragma comment(lib, "gdi32.lib")
int CALLBACK wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
D3DKMT_POLLDISPLAYCHILDREN info;
SecureZeroMemory(&info, sizeof(info));
info.hAdapter = (D3DKMT_HANDLE) 0;
info.SynchronousPolling = 1;
info.PollAllAdapters = 1;
info.PollInterruptible = 1;
D3DKMTPollDisplayChildren(&info);
return 0;
}
#!/usr/bin/env python
# This script forces detection of added (or removed) display screens
# It does the same thing as when clicking on the "Detect" button in Windows display settings
# This can be used to create a shortcut to refresh displays list
import ctypes
class D3DKMT_POLLDISPLAYCHILDREN(ctypes.Structure):
_fields_ = [
('hAdapter', ctypes.c_uint32), # (D3DKMT_HANDLE) Adapter handle
('NonDestructiveOnly', ctypes.c_uint32, 1), # 0x00000001 Destructive or not
('SynchronousPolling', ctypes.c_uint32, 1), # 0x00000002 Synchronous polling or not
('DisableModeReset', ctypes.c_uint32, 1), # 0x00000004 Disable DMM mode reset on monitor event
('PollAllAdapters', ctypes.c_uint32, 1), # 0x00000008 Poll all adapters
('PollInterruptible', ctypes.c_uint32, 1), # 0x00000010 Poll interruptible targets as well
('Reserved', ctypes.c_uint32, 27) # 0xffffffc0
]
info = D3DKMT_POLLDISPLAYCHILDREN(hAdapter=0,
SynchronousPolling=1, PollAllAdapters=1, PollInterruptible=1)
ctypes.windll.gdi32.D3DKMTPollDisplayChildren(ctypes.pointer(info))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment