-
I'm trying to get the name of the desktop using public static bool GetCurrentDesktop(out string desktopName)
{
desktopName = default;
var inputDesktop = OpenInputDesktop_SafeHandle(0, true, 0x10000000);
var desktopBytes = new byte[256];
unsafe
{
if (!GetUserObjectInformation(inputDesktop, USER_OBJECT_INFORMATION_INDEX.UOI_NAME, /** how to use parameters? **/))
{
desktopName = string.Empty;
return false;
}
}
// Other code ...
return true;
} |
Beta Was this translation helpful? Give feedback.
Answered by
AArnott
Nov 12, 2022
Replies: 1 comment 1 reply
-
Like this: static bool GetCurrentDesktop([NotNullWhen(true)] out string? desktopName)
{
using var inputDesktop = OpenInputDesktop_SafeHandle(0, true, 0x10000000);
unsafe
{
const int maxLength = 256;
fixed (char* pDesktopBytes = new char[maxLength])
{
uint cbLengthNeeded;
if (!GetUserObjectInformation(inputDesktop, USER_OBJECT_INFORMATION_INDEX.UOI_NAME, pDesktopBytes, maxLength, &cbLengthNeeded))
{
desktopName = null;
return false;
}
int charLength = (int)cbLengthNeeded / sizeof(char) - 1; // discount the null terminator
desktopName = new string(pDesktopBytes, 0, charLength);
return true;
}
}
} Of course error handling is still missing. But that's how you use pointers. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
vitkuz573
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like this: