This appears to be a bug in Windows (at least in Windows 10 version 2004). Contrary to the documentation, the WTSFreeMemoryExA
function does not accept WTSTypeSessionInfoLevel1
, whereas WTSFreeMemoryExW
does. This means that instead of using the WTSEnumerateSessionsExA
function which returns WTS_SESSION_INFO_1A
structures, you need to instead use the WTSEnumerateSessionsExW
function which returns WTS_SESSION_INFO_1W
.
This bug effectively makes WTSEnumerateSessionsExA
unusable, unless you don't care about the memory leak caused by the inability to free its results. This bug appears to have been known about for some time. (Hopefully, some day, Microsoft will fix this.)
Some reports claim that even using WTSEnumerateSessionsExW
and WTSFreeMemoryExW
appears to leak memory, which implies that WTSEnumerateSessions
combined with WTSQuerySessionInformation
may be the better approach. However, I myself have been unable to reproduce that issue. I suspect it was a real issue at one point, but has been fixed by Microsoft in more recent Windows versions.