Detect all connected monitors using Windows API - winapi

I am using Windows 7. When I plug a new monitor when the PC is running, the monitor is not automatically detected. I must go to the Display->Screen Resolution and click on the Detect button in order to detect the monitor with the registry configuration.
I am searching for a way to do the same programmatically using the Windows API. So I would like to write a program that detects all connected monitors reproducing the Detect button. I tried using ChangeDisplaySettingEx but with bad results. Any suggestions?

Not really a WinApi but try this utility:
C:\Windows\System32\DisplaySwitch.exe
DisplaySwitch.exe /internal - Switch to Primary only
DisplaySwitch.exe /external - Switch to Secondary only
DisplaySwitch.exe /clone - Clone desktop on both screens (Not HDCP compliant!)
DisplaySwitch.exe /extend - Extend desktop to both screens
In particular, try /extend switch, it should perform detection internally.

Not an API solution, but you can use MS's DevCon utility on the command line to scan for new hardware.
https://support.microsoft.com/en-us/kb/311272
Download it from that page, then just execute this from a cmd window:
devcon.exe rescan
You can easily create a .bat or .cmd file to run this from a link.

You should read this if you want to learn about display-aware apps.

Related

Windows Containers - Is it possible to interact with desktop apps running in a container using the Desktop Sharing API?

I understand that desktop/GUI apps are not supported in Windows containers. They do run but there's no built-in way to interact with them. I had the following idea - maybe I could use the Desktop Sharing API (https://learn.microsoft.com/en-us/windows/win32/api/_rdp/) for this purpose, the idea is to run a desktop app, then run a sharing program that uses the Desktop Sharing API, and connect to it using a Desktop Sharing API viewing program from the host.
I had to do some recap about window stations and desktops, and I noticed that when starting the container with cmd in interactive mode, I'm logged with ContainerAdministrator as a service (logon type 5). I tried running some WinAPI functions that deal with desktops and winstation and got some access denied results, so I switched to running cmd as system.
The window station of the cmd process (and other child processes) is not the interactive WinSta0, but instead some other service window station, which makes sense since I'm logged on as a service, and I figured that I can't use this window station, so I used a little program I wrote to run notepad in Winsta0 in the Default desktop. Afterwards I ran another program that enumerates the windows on WinSta0\Default, and the notepad window does get enumerated and I also get it's title, so it's running somewhere.
So now I tried running the desktop sharing API program (also on WinSta0\Default). It runs and I can connect from the host, but I only get a black screen without anything on it. I also tried running a program that takes a screenshot of the windows but I get an empty bitmap.
So I thought maybe the Default desktop is not the active desktop, and by using the OpenInputDesktop function I could confirm it - the current desktop was the Winlogon desktop, so I used the SwitchDesktop function to switch to the Default desktop (I used OpenInputDesktop again to verify that it actually worked).
Unfortunately, this didn't change anything, I still get an empty screen and empty bitmaps.
I know that containers are built for micro services and are not supposed to run GUI apps and so, but still - is there a way to make this work? Or any ideas of what else I can check? Alternatively, if you know that it can't work - I would also be happy to hear a good technical explanation of why it doesn't work.

Prevent disconnected display from merging windows into the active one

I've got a personal laptop (running Windows 10) which I use at work where I connect it to an external display using extended display mode. I keep all my personal icons and windows on my laptop display and store all the work-related windows on the external display. Whenever I unplug it, all the windows and icons from that display are merged into my laptop screen. I want to programmatically prevent changing anything on my primary screen when the secondary is disconnected. I'm currently writing a utility app for a variety of small productivity improving features and would like to add such feature in it. I can think of two ways to achieve this:
by tricking the system to think that the external display hasn't been
disconnected;
or take all the opened windows and icons on disconnected screen and put them on separate virtual desktop.
I was looking into Windows GDI Device Context Functions but haven't found anything about display connection/disconnection events. How can I detect display disconnection (and get that display's opened windows and icons)? Anything that can be done using C#, C++ or PowerShell scripts would be much appreciated!

How to put monitor on standby and not let it wake up

I have 3 monitors, but I don't need them all turned on all the time. I can just shut them down with power button, but I want to use their standby mode, like Windows does when we let PC idle for a while - it shuts down monitors, HDD, etc.
But of course, I wanna keep using PC and let just that monitor on standby. Others must remain on and that one doesn't wake up even with me using PC.
Is it possible to do that? It would be great to have a shortcut like Winkey+1, 2, 3 etc to shut down and wake up each monitor.
An existing app with this feature is not likely to exist, but is there a Windows API function that can control monitor state, for each monitor in a MultiMonitor system?
The display control panel applet calls SetDisplayConfig to start or stop forced projection on a particular target
You can probably use MS Detours or some other API hooking tool to inspect the usage pattern of the API while using the applet to adjust display settings.
You'll want to try Display Fusion. You should be able to do what you're asking for using Monitor configurations.
I know I'm late on this but use DDC to control your display. You can easily create hotkeys that send a command via DDC to the display to turn-off. This would be equivalent to turning off the display using the button. Works like a charm for me. The only trick is that DDC command specs vary across monitor manufacturers but its not hard to find the right codes to send with the help of google.
Ready made tools also exist for this; search for anything that is related to DDC or EDID and you should find.
Be aware though that this does not remove the display from Windows which means that apps may find their way onto displays that are off and you will be looking for them.

Autolaunch exe on USB in windows XP SP3

I want to autolaunch my application on USB as soon as I plug it into a PC running on windows XP SP3. I have made an autorun.inf (having below written code) and also tried to lauch setup using a batch file(autorun.bat) but none of these seem to be working.
I have also read that a particular windows update (KB971029) disables the autorun/autoplay feature of USB. How can I overcome it? (I can't ask my client to uninstall a windows security update for using my product..)
Code Of autorun.inf file
[autorun]
UseAutoPlay=1
icon=setup.exe
Open=setup.exe
action="Start my application"
Label=setup
[Autoplay]
shellexecute=autorun.bat
Icon=setup.exe
My product contains some media (video) files too so on inserting the USB I get a pop up asking following options
1.) to open files in explorer
2.) play files using media player
3.) take no action
If autorun is disabled, you cannot use autorun. It's that simple.

Programmatically uninstall a device in windows device manager

Is it possible to programmatically uninstall a device in windows and perform a 'scan for hardware changes' programmatically? Is there any VB script that can be used for the same? Or is there any windows API that can be called to achieve the same?
To force the 'scan for hardware changes' checkout "How To Force Reenumeration of a Device Tree From an Application" the sample there shows how to force the entire tree to be re-enumerated.
As to removing a driver checkout DiUninstallDevice.

Resources