According to the documentation, WTSFreeMemoryExA
can be used to free a WTS_SESSION_INFO_1A
structure by passing a WTS_TYPE_CLASS
of WTSTypeSessionInfoLevel1
. However, any attempt to do so fails with error code 87 (ERROR_INVALID_PARAMETER
, "The parameter is incorrect"
).
How to get WTSFreeMemoryExA
to work?
This appears to be a bug in Windows (at least in Windows 10 version 2004). Contrary to the documentation, the
WTSFreeMemoryExA
function does not acceptWTSTypeSessionInfoLevel1
, whereasWTSFreeMemoryExW
does. This means that instead of using theWTSEnumerateSessionsExA
function which returnsWTS_SESSION_INFO_1A
structures, you need to instead use theWTSEnumerateSessionsExW
function which returnsWTS_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
andWTSFreeMemoryExW
appears to leak memory, which implies thatWTSEnumerateSessions
combined withWTSQuerySessionInformation
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.