I want to write a Windows service (in c#) or a powershell script that connects my laptop automatically (at boot or key combination) to my MS wireless display adapter for screen mirroring. In Windows 10 I can only do it manually by going to the notifications and click Connect>MS Wireless adapter>connect.
What I found is that there is a Miracast API but there is not much documentation on how to use it.
I also found this documentation on MiraDisp.dll and there are two functions OpenMiracastSession and CloseMiracastSession.
The problem is I don't know how to use these functions in c#. I know I will probably have to use pInvoke. Can anyone point me in the right direction?
First of all, thanks to @CodingGorilla for the suggestion on AutoHotkey. I've been playing around with that the past couple of days.
I went the AutoHotkey route as I couldn't find an easy place to start with any Windows 10 API. All kinds of documentation out there to push toast notifications but I couldn't find anything to control the action center. If anyone has suggestions on that front, please post them.
Here is what I came up with using AutoHotkey. Pretty simple but not an ideal solution as there are a few variables with this. Below is AutoHotkey script code I used to open the action center, click connect, then click the top-most listed wireless display:
Send #a ;Sends Windows button + A to open the action center
Sleep, 750 ; Give it some time to slide open
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Connect.png ;Try to find the Connect button tile
if ErrorLevel = 2
MsgBox Could not conduct the search for the connect button in action center. Make sure your search image is in the correct location.
else if ErrorLevel = 1
MsgBox Connect button cannot be found on the screen.
else
MoveMouseAndClick(FoundX, FoundY)
Sleep, 1250 ;Delay so the wireless displays have a chance to load into the Action Center window
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\WirelessDisplay.png
if ErrorLevel = 2
MsgBox Could not conduct the search for the wireless display.
else if ErrorLevel = 1
{
;Search image cannot be found. Try 1 more time in case it took a long time for the wireless displays to appear
Sleep, 750
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\WirelessDisplay.png ;try to find the first Wireless Display device listed
if ErrorLevel = 1
MsgBox Wireless display image cannot be found on the screen. Make sure the wireless device is turned on.
else
MoveMouseAndClick(FoundX, FoundY)
}
else
MoveMouseAndClick(FoundX, FoundY)
Send {Esc} ;Send Esc to get rid of the Action Center window
Return
MoveMouseAndClick(x, y) {
MouseMove, x + 25, y + 25 ;Move it down the right a bit to make sure we click the button
Sleep, 250
MouseClick, left
}
I've also attached the images as an example of what I made. You will need to make your own search images. Before making those images, you must also turn off the transparency of the Action Center, start and taskbar in Windows 10 - Settings->Personalization->Colors->Make Start, taskbar, and action center transparent->Off. It is especially important to redo the 2nd image as mine image lists "Roku Stick" within the image. I had to redo my search image between my desktop development machine and the MS Surface 3 I'm running this script on. Resolutions and such will change between devices. Follow the instructions on how to create your own search image here:
https://autohotkey.com/docs/commands/ImageSearch.htm
Lastly, this likely won't work if the wireless display is already connected. In my environment connecting the wireless display causes the resolution on the tablet to change and therefore it can't find the images on screen.