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?
Enable Auto-connect to automatically connect to last display when Wireless display is turned on. Touch Apps > Settings > Display > Wireless display > select Auto-connect.
Select Start > Power > Restart. After your device restarts, select Start > Settings > Devices > Bluetooth & other devices. Select Add Bluetooth or other devices. Select Wireless display or dock to reconnect your device and the Microsoft Wireless Display Adapter.
Step 1: Launch Settings, search for “cast”, and click the search result. Step 2: Tap Cast. Step 3: Click the 3 dots on the top-right corner and tick-mark “Enable Wireless Display”. Step 4: Your Windows 10 Device name should now appear.
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.
First I want to say that @jaredbaszler has provided a really good solution. It worked like a charm thank you :)
I was playing around with AutoHotkey too since I wanted to find out if there is another way to do this. After a while I came up with the following script:
Send #k ; Sends Windows button + K to open the Action Center Connect window
Sleep, 3000 ; Wait some time so the wireless display dongle can be found
Send {Enter} ; Send ENTER key to connect to wireless display dongle (works when only 1 is found)
Send {Esc} ; Send ESC key to close the Action Center Connect window
Ok. Now let me explain how this script works:
Well, that´s it. It´s nothing special but it works. I have tested this script a few times with my tablet and my wireless display dongle (I have this one here) and it seems to work just fine. Unfortunately my script will not work as expected if you have multiple wireless display dongles up and running at the same time because my script will simply choose the first one that shows up. (This is not a problem for me since I only have one wireless display dongle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With