0

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?

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59

2 Answers2

1

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.

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
  • 1
    Hi,I can reproduce this issue now. And I will confirm it with Internal engineer, and response here if there is any update. Thanks for your understanding. – Zeus May 07 '21 at 01:26
  • It's probably a bug in user code to be using WTS_SESSION_INFO_1A anyway because, well, you don't need me to explain why the Unicode API is preferred. So it's not exactly a high priority to fix this defect. – David Heffernan May 07 '21 at 05:59
  • @DavidHeffernan Historically what you say is correct, but with the introduction of [UTF-8 support in Win10 version 1903](https://learn.microsoft.com/en-us/windows/uwp/design/globalizing/use-utf8-code-page) the W-over-A preference arguably no longer applies, so this bug would be higher priority than it used to be. And a UTF-8 code page app using A apis instead of W is not a bug. With UTF-8 code page, now A apis are Unicode too. (W=UTF-16, A=UTF-8). – Simon Kissane May 09 '21 at 04:08
1

thank you for raising this question.

We checked the relevant source code and found the source code related to WTSFreeMemoryA. It accepts the first parameter WTSTypeClass as WTSTypeProcessInfoLevel0 or WTSTypeProcessInfoLevel1, but it doesn’t accept the value WTSTypeSessionInfoLevel1 and therefore return the ERROR_INVALID_PARAMETER error on this call.

This is different from the description in the document, we will submit this issue. And you can try to use WTSFreeMemoryW to avoid this issue.

Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Thanks for your response. When you say `WTSFreeMemoryA`, you mean `WTSFreeMemoryExA`, right? (Likewise for `WTSFreeMemoryW`.) Also, I understand you are a Microsoft employee? It might be good to clarify that in your answer. (Otherwise people will be wondering how you are checking the Windows source code.) – Simon Kissane Jun 04 '21 at 08:11