0

First, I have extensively read through Autoconnect to MS Wireless display on Windows 10 and tried basically every solution. (I did technically get the AutoHotKey solution working, and in fact did that before even researching. But, I feel like that's kind of unprofessional and surely there is some API that can connect to this thing.) After going through all of this, I just started reading through the different namespaces. Finally, I found Windows.Devices.WiFiDirect. This gave me the most progress I've been able to get, which is, it begins to connect and says so on screen, then an exception stating that the device is unreachable occurs. Very infuriating.

Can anyone explain exactly what is happening here? It seems like this should be the proper way to connect my screen to this device, but it is just not working. Code below, it's pretty short and straightforward.

Edit:

Based on Roy Li's suggestion, I attempted to use a different overload of the socket.ConnectAsync method. This actually did have an effect but I am still receiving an exception, although a different one. The method now attempts to connect for longer but still fails out, this time with a "connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond" exception. Could this mean there is some sort of secret handshake that Window's OS is using when connecting to this device? If so, this might be a dead end. The code has been updated below.

        static async Task Main()
        {
            string id = null;
            string prefix = "MicrosoftDisplayAdapter";
            WiFiDirectDevice device;
            StreamSocket socket = new StreamSocket();

            try
            {
                DeviceInformationCollection devInfoCollection = await DeviceInformation.FindAllAsync(WiFiDirectDevice.GetDeviceSelector());
                foreach (DeviceInformation devInfo in devInfoCollection)
                {
                    if (devInfo.Name.StartsWith(prefix))
                    {
                        id = devInfo.Id;
                    }
                }
                device = await WiFiDirectDevice.FromIdAsync(id);
                var endpointPairCollection = device.GetConnectionEndpointPairs();
                await socket.ConnectAsync(endpointPairCollection[0].RemoteHostName, "50001"); //This line begins connecting to the display but ultimately fails
            }
            catch (Exception e)
            {
                //device unreachable exception
            }
        }
Riko
  • 1
  • 3
  • Have you tried the official sample-[WiFiDirect](https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/WiFiDirect) to check if the official sample could connect to your device? – Roy Li - MSFT Aug 16 '21 at 05:39
  • I have not specifically copied this code just to see if it would work. Finding the device isn't really the issue. I've successfully found it in multiple ways. What I'm more curious/concerned with is that I want to know why it begins connecting (on the display adapter's screen, it actually says connecting to my computer's name) but then receiving a "device unreachable" error before finishing connection. This code example is still using `socket.ConnectAsync`, although it's using a different overload of it. Out of curiosity, I could try this overload. – Riko Aug 16 '21 at 12:48
  • @RoyLi-MSFT Using that overload of the method did actually have an effect. I put an edit in the post detailing what happened. – Riko Aug 16 '21 at 13:14
  • Could you connect to the Display Adapter using Microsoft Wireless Display Adapter app or using the Windows built-in Wifi mode? – Roy Li - MSFT Aug 17 '21 at 07:21
  • Yes, with the Connect sidebar I've been able to connect with no problem. Win+k is the shortcut for this. – Riko Aug 17 '21 at 12:12
  • Got it. I'll check this behavior. Could you please submit feedback in the Feedback Hub as well? You could find the Feedback Hub in the Start Menu. – Roy Li - MSFT Aug 18 '21 at 03:05

1 Answers1

0

I finally found something along the lines of what I need. I came across https://social.msdn.microsoft.com/Forums/en-US/7608d127-d864-436a-802e-472fd55cc02c/use-projectionmanager-from-net-framework?forum=csharpgeneral, which gave me a way to cast/project to the Microsoft Display Adapter. As the link states, I do get a "Catastrophic Error" but it does make the connection and keep it, anyway. What my code ended up looking like is below:

        static async Task Main()
        {
            string prefix = "MicrosoftDisplayAdapter";
            DeviceInformation displayAdapter = null;

            try
            {
                //Get projection devices
                DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(ProjectionManager.GetDeviceSelector());
                foreach (DeviceInformation device in devices)
                {
                    if (device.Name.StartsWith(prefix))
                    {
                        displayAdapter = device;
                    }
                }
                //Start projection.  This throws an error but works without issue.
                await ProjectionManager.StartProjectingAsync(0, 0, displayAdapter);
            }
            catch (Exception e)
            {
                //Ignore this error
                if (e.Message.StartsWith("Catastrophic"))
                {
                    //Change display to use secondary only
                    Process proc = new Process();
                    proc.StartInfo.UseShellExecute = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.FileName = @"C:\Windows\Sysnative\DisplaySwitch.exe";
                    proc.StartInfo.Arguments = "/external";
                    proc.Start();
                    proc.WaitForExit();
                }
                else
                {
                    Console.WriteLine(e);
                }
            }
        }
Riko
  • 1
  • 3