MScholtes/VirtualDesktop

SetDesktopName

guipalazzo opened this issue · 4 comments

Hello, I'm having issues when trying to rename the virtual desktop, by calling SetDesktopName.

The method do some marshalling on the name string, but it uses the HString type. According to this link, HSTRING marshalling is not supported in .NET 5 or newer, so I'm not sure how the renaming should be conducted in this case.

"When using a WinRT-based API, you may need to marshal a string as an HSTRING. Using the UnmanagedType.HString value, you can marshal a string as a HSTRING. HSTRING marshalling is only supported on runtimes with built-in WinRT support. WinRT support was removed in .NET 5, so HSTRING marshalling is not supported in .NET 5 or newer."

I've tried to change it from UnmanagedType.HString to UnmanagedType.LPWStr, but I then get a "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt". Even though my knowledge is not that deep on marshalling objects, I was able to use UnmanagedType.LPWStr marshalling in this past with no protected memory issues. Obviously and unfortunately, I couldn't figure out what is the issue in this case

Could you please assist?

Hello @guipalazzo ,

.Net 5 and higher (formerly .Net Core) is a completely different version of .Net than the .Net Framework (formerly .Net), where the current version is 4.8x.
.Net Core is a cross-platform product that runs on Linux and MacOS too, for example, and therefore does not support Windows-specific functions (at least Microsoft is in the process of gradually removing them from .Net Core).
For this reason, the compilation of VirtualDesktop with .Net Core does not work and does not make sense, as virtual desktops are Windows-specific. Please use .Net Framework 4.x as target platform or directly the attached script Compile.bat.

Greetings

Markus

Hello @MScholtes,

Thanks for getting back. As a matter of fact, my (WPF) application is targeting net6.0-windows10.0.19041.0. The compilation works fine and I'm able to create virtual workspaces. What I get is a runtime error when I try to rename the virtual desktop.

System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination.
at System.StubHelpers.StubHelpers.ThrowInteropParamException(Int32 resID, Int32 paramIdx)

I found this other package which share some similarities to yours, although it is not updated for Windows 11 22H2. I've noticed that this one is not using any string marshalling, so I tried to remove the string marshalling from the SetName method from yours, but then I get a memory error.

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I'm not sure how to deal with the string marshalling in this case, that is causing the runtime error.

Hello @guipalazzo,

my application is based on .Net Framework as it is targeting windows spezific features (so is WPF). There is no need to use or support .Net Core, so I don't.

Good luck with it

Markus

P.S. in my companion project PSVirtualDesktop I define a class HString that might be useful for you.