Giter Site home page Giter Site logo

virtualdesktop's Introduction

VirtualDesktop

VirtualDesktop is C# wrapper for IVirtualDesktopManager on Windows 11 (and Windows 10).

Build Publish License

Platform NuGet
Core NuGet Badge
Forms NuGet Badge
WPF NuGet Badge

Features

  • Switch, add, and remove a virtual desktop.
  • Move the window in the same process to any virtual desktop.
  • Move the window of another process to any virtual desktop (Support in version 2.0 or later).
  • Pin any window or application; will be display on all desktops.
  • Notification for switching, deletion, renaming, etc.
  • Change the wallpaper for each desktop.

Sample app

samples/VirtualDesktop.Showcase

Requirements

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
  • .NET 5, 6 or 7
  • Windows 10 build 19041 (20H1) or later

Installation

Install NuGet package(s).

PM> Install-Package VirtualDesktop

How to use

Preparation

Because of the dependency on C#/WinRT (repo), the target framework must be set to net5.0-windows10.0.19041.0 or later.

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>

If it doesn't work, try creating an app.manifest file and optimize to work on Windows 10.

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
	    <!-- Windows 10 / 11-->
	    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
</compatibility>

The namespace to use is WindowsDesktop.

using WindowsDesktop;

Get instance of VirtualDesktop class

// Get all virtual desktops
var desktops = VirtualDesktop.GetDesktops();

// Get Virtual Desktop for specific window
var desktop = VirtualDesktop.FromHwnd(hwnd);

// Get the left/right desktop
var left  = desktop.GetLeft();
var right = desktop.GetRight();

Manage virtual desktops

// Create new
var desktop = VirtualDesktop.Create();

// Remove
desktop.Remove();

// Switch
desktop.GetLeft().Switch();

Subscribe virtual desktop events

// Notification of desktop switching
VirtualDesktop.CurrentChanged += (_, args) => Console.WriteLine($"Switched: {args.NewDesktop.Name}");

// Notification of desktop creating
VirtualDesktop.Created += (_, desktop) => desktop.Switch();

for WPF window

// Need to install 'VirtualDesktop.WPF' package

// Check whether a window is on the current desktop.
var isCurrent = window.IsCurrentVirtualDesktop();

// Get Virtual Desktop for WPF window
var desktop = window.GetCurrentDesktop();

// Move window to specific Virtual Desktop
window.MoveToDesktop(desktop);

// Pin window
window.Pin()

Windows version support

The class IDs of some of the undocumented interfaces we use tend to change a lot between different versions of Windows. If the demo application crashes on start-up chances are all you need to do is provide the proper IDs for the version of Windows you are running on.

Open regedit and export this path into a file: \HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface. Open the resulting reg file and search it for matches against the whole word of each interface name we need:

  • IApplicationView
  • IApplicationViewCollection
  • IObjectArray
  • IServiceProvider
  • IVirtualDesktop
  • IVirtualDesktopManager
  • IVirtualDesktopManagerInternal
  • IVirtualDesktopNotification
  • IVirtualDesktopNotificationService
  • IVirtualDesktopPinnedApps

Once you have the IDs add them in a new setting element in app.config. Make sure to specify the correct 5 digits Windows build version. You can get it using one of those methods:

  • From the UI run: winver
  • From shell run: ver
  • From powershell run: cmd /c ver

Make sure to contribute back your changes.

Publish

To publish a new release specify your version in Directory.Build.props and push the changes with a commit description such as: Release vx.y.z where x, y, z form your version number. That should publish it on NuGet providing that your secret NUGET_API_KEY is still valid.

Resources

License

This library is under the MIT License.

virtualdesktop's People

Contributors

akazarenn avatar danielchalmers avatar dexterously avatar grabacr07 avatar maciej-makowski avatar mpdreamz avatar mpolicki avatar mzomparelli avatar sameera avatar slion avatar tmyt avatar vegardlarsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

virtualdesktop's Issues

Windows build 22621.2361 not supported

I don't have time to test right now, but it did break my app that is not using this repo but is using the vd interfaces. I will investigate further tonight.

Changing GUIDs in app.config is not recommended

I see now why you modify app.config manually. It's not always this easy and I think this is why you got stuck on the 22621.2215 change.

If the underlying interface changes in such a way (members added or removed) then simply changing app.config will not fix it. Usually when the GUIDs change then there is an underlying interface change we should examine.

While changing app.config might work sometimes, it may not and could lead to a MemoryAccessViolation due to the interface offsets being different due to a change in members.

app.config is created during the build process and the info is take from Settings.settings.

I recommend removing this part and encouraging users to open an issue so we can investigate. This way we hear about it and can check for changes to ensure it'll work.

image

Better OS Build version support

Review the changes that were made in 88cec4b and consider using a Version object rather than a double to represent our OS Build.

Parsing should not assume fixed number of digits for major and minor OS Build number, namely 5 and 4 currently.

All applications use the same generated DLL

All applications using VirtualDesktop use the same generated DLL from the app data folder.
We should store it on a path specific to the running application to avoid conflicts.

Broken on Windows 11 v22631.3155

Just did an update and something must have changed again. Can anyone confirm?

System.InvalidCastException: Specified cast is not valid.
at WindowsDesktop.Interop.IServiceProvider.QueryService(Guid& guidService, Guid& riid)
at WindowsDesktop.Interop.ComInterfaceAssembly.CreateInstance(Type type, Nullable1 guidService) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\ComInterfaceAssembly.cs:line 54 at WindowsDesktop.Interop.ComInterfaceAssembly.CreateInstance(String comInterfaceName, Guid clsid) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\ComInterfaceAssembly.cs:line 45 at WindowsDesktop.Interop.ComWrapperBase1..ctor(ComInterfaceAssembly assembly, Guid clsid) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\ComWrapperBase.cs:line 33
at WindowsDesktop.Interop.Build22621.VirtualDesktopManagerInternal..ctor(ComInterfaceAssembly assembly, ComWrapperFactory factory) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\Build22621_2215\VirtualDesktopManagerInternal.cs:line 13
at WindowsDesktop.Interop.Build22621.VirtualDesktopProvider22621.InitializeCore(ComInterfaceAssembly assembly) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\Build22621_2215.Provider.cs:line 42
at WindowsDesktop.Interop.VirtualDesktopProvider.Initialize(ComInterfaceAssembly assembly) in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\Interop\VirtualDesktopProvider.cs:line 30
at WindowsDesktop.VirtualDesktop.InitializeCore() in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\VirtualDesktop.system.cs:line 126
at WindowsDesktop.VirtualDesktop.InitializeIfNeeded() in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\VirtualDesktop.system.cs:line 114
at WindowsDesktop.VirtualDesktop.GetDesktops() in C:\Users\simonl\OneDrive - Personal\OneDrive\Documents\Dev\VirtualDesktopGridSwitcher\VirtualDesktop-master\src\VirtualDesktop\VirtualDesktop.cs:line 132
at VirtualDesktopGridSwitcher.VirtualDesktopGridManager.Start()

Reverse engineering process

We need to document the reverse engineering process to make it easier to support future versions of Windows.

My understanding is that you need to run a Python script from a fork of GetVirtualDesktopAPI_DIA that dumps the GUIDs and interfaces definitions from twinui.pcshell.dll using Debug Interface Access and Microsoft Symbol Server.

It would be nice to port that Python script to C# and integrate it to this repository. That would make it even easier to perform reverse engineering.

Support OS Build 22621.2215

Windows 11 OS Build 22621.2134 was working fine.
However since 22H2 August 2023 update and OS Build 22621.2215 VirtualDesktop is broken again.

Checkout the forks of https://github.com/t1m0thyj/GetVirtualDesktopAPI_DIA/network and try use them to reverse engineer the latest changes.

Since we can also have breakage between minor OS Build update we should incorporate minor OS Build in our ID resolution system.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.