Gaming Life hacks: Switch to second screen for Screencasting

Honestly every time I head about GeforceNow, Stadia and all the other streaming services I just smile and start Moonlight on my FireTV, my smartphone or any other Android or Windows device I have sitting around. Moonlight has made it so comfortable using your own PC as gaming cloud that it’s almost ridicoulous.

Add Wake-On-Lan, Wireguard and a tiny WOL script on a Raspberry Pi and you don’t even have to keep your PC running. And waking Windows 11 up is a lot faster than any other game service I’ve tried creating a new instance for me.

So, long story short: Live is good if you have a PC and you don’t need no stinking Streaming service.

Sadly, there’s a but. A big one. Well, at least if you have a 32:9 screen like me connected via DisplayPort. Or if you don’t want to keep your screen running every time you stream. Again long story short: If your primary screen isn’t a 1920×1080 screen connected via HDMI you’re going to have issues.

Straightforward solution, add a second screen. The best way I’ve found is simply plugging in an “HDMI Dummy”, you can find plenty of those on Amazon (shouldn’t cost more than $10).

Now, all you need is switch to this device when Streaming starts and you’r set.

Now doing this via the Windows API is slightly complicated, but the good news is that Windows includes a braindead simple app called DisplaySwitch.exe … and detecting active streaming boils down to checking for a process alled “nvstreamer”.

So what it all boils down to is this:

static bool OnSecondScreen=false;
var Switch = Path.Combine(Environment.SystemDirectory, "DisplaySwitch.exe");
var IsStreaming= Process.GetProcesses().Where(a => a.ProcessName.ToLower() == "nvstreamer").Count() > 0;

if(IsStreaming && !OnSecondScreen)
   Process.Start(Switch, "/external");
else if(!IsStreaming && OnSecondScreen)
   Process.Start(Switch, "/internal");

OnSecondScreen = IsStreaming;


Now, there are any number of ways to implement this, (I’ve got a nice little Tray Icon in my taskbar), but all you really need is the good old C# compiler that comes with Windows. Wrapping the code above in a loop you end up with

using System;
using System.IO;
using System.Linq;
using System.Diagnostics;

static class Program{
	static void Main(){
		var OnSecondScreen=false;
		var Switch = Path.Combine(Environment.SystemDirectory, "DisplaySwitch.exe");
		while(true){
			var IsStreaming= Process.GetProcesses().Where(a => a.ProcessName.ToLower() == "nvstreamer").Count() > 0;

			if(IsStreaming && !OnSecondScreen)
			   Process.Start(Switch, "/external");
			else if(!IsStreaming && OnSecondScreen)
			   Process.Start(Switch, "/internal");

			OnSecondScreen = IsStreaming;
			
			System.Threading.Thread.Sleep(1000);
		}
	}
}

which you can save to a cs file (StreamToSecond.cs in the following example) and compile with the old C# compiler included in Windows (which is outdated, but sufficient for our purposes)

c:\windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe StreamToSecond.cs

Put the EXE into your Startup folder and it will watch for NVStreamer and switch screens accordingly.

Hope it helps someone ^^.

One thought on “Gaming Life hacks: Switch to second screen for Screencasting”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.