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
WTSFreeMemoryExAfunction does not acceptWTSTypeSessionInfoLevel1, whereasWTSFreeMemoryExWdoes. This means that instead of using theWTSEnumerateSessionsExAfunction which returnsWTS_SESSION_INFO_1Astructures, you need to instead use theWTSEnumerateSessionsExWfunction which returnsWTS_SESSION_INFO_1W.This bug effectively makes
WTSEnumerateSessionsExAunusable, 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
WTSEnumerateSessionsExWandWTSFreeMemoryExWappears to leak memory, which implies thatWTSEnumerateSessionscombined withWTSQuerySessionInformationmay 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.