Autoconnect to MS Wireless display on Windows 10

24.9k Views Asked by At

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?

6

There are 6 best solutions below

0
On

To automate and control Miracast, you want to write a UWP app using the Windows.Media.Casting namespace and Windows Universal Samples.

They can be written in many common languages including C#, C++, Javascript, and Visual Basic.

Windows provided two sets of functional sample programs to demonstrate what you can do with Miracast that can be opened and launched as Visual Studio solutions (.sln).

Basic Casting

Advanced Casting

Because UWP apps are designed to be made available on the Windows app store, they have specific requirements and regulations for functioning. (I recommend researching these.)

Also avoid the pitfall I made when I started writing a generic program and then wondered where the Universal Windows library was:

Starting a UWP project

0
On

I ended up using the vbs-based solution from here: https://superuser.com/questions/1062789/how-to-connect-to-a-wireless-display-adapter-automatically-through-scripts-or-ta

While it basically does the same steps, I found this much more robust than the other solutions. What is more, vbs is available (I think) on most Win machines.

0
On

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.

Image of the connect button in Action Center
enter image description here

3
On

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:

  1. First it will press WIN+K which will open the Action Center Connect window
  2. Then it will wait for 3 seconds so that the wireless display dongle can be found (You can adjust this value as you please but I needed to wait more than 2 seconds for my wireless display dongle to show up)
  3. After the wait it will press ENTER which will automatically choose the first wireless display dongle in the list and trigger the connect function (If no wireless display dongles can be found your Default Browser will open the "Help" link)
  4. The last thing the script does is to press the ESC key to close the Action Center

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)

0
On

This was the AutoHotKey script that I wrote;

Run, explorer.exe ms-settings-connectabledevices:devicediscovery
Sleep, 800
Send, {Tab}
Send,+{Tab}
Send,{Enter}
Sleep, 200
; then next Send command types the first few letters of the name of the WiDi adaptor
Send, Microsoft
Sleep, 200
Send, {Tab}
Sleep, 200
Send, {Enter}
2
On

The question is a bit old, but currently I've faced the same problem.

I suggest writing a UWP App utilizing the ProjectionManager class according to sample 5 + 6 in the Advanced casting sample.

The essentially steps are:

  • getting a DeviceID (for example by picking with DevicePicker class)
  • calling ProjectionManager.StartProjectingAsync(newViewId, currentViewId, selectedDeviceInformation) to start the projection

The DeviceInformation object can be obtained by calling DeviceInformation.CreateFromIdAsync() using the acquired (and saved) deviceId. The viewIds may be set to 0 (or other invalid values) the projection is starting anyway.

If the projection has started, the app can be closed, the projection is still keeping alive.

If anyone is interested in the solution, I will upload the code to GitHub.