Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structure member alignment issues in WTSApi32 #278

Closed
rosenqui-cysiv opened this issue Feb 24, 2022 · 2 comments · Fixed by #279
Closed

Structure member alignment issues in WTSApi32 #278

rosenqui-cysiv opened this issue Feb 24, 2022 · 2 comments · Fixed by #279

Comments

@rosenqui-cysiv
Copy link

Describe the bug
The timestamp fields in the WTSINFO and WTSINFOEX_LEVEL1 end up with bad values because their offsets within the structure aren't correct. This is due to the SizeConst = USERNAME_LENGTH + 1 of the previous field in WTSINFO messing up the padding. I haven't fiddled with WTSINFOEX_LEVEL1 but I believe it has similar issues.

Changing WTSINFO to use SizeConst = USERNAME_LENGTH + 2 or adding a private char _padding field after UserName fixes it.

What code is involved
PInvoke\WTSApi32\WTSApi32.cs

Expected behavior
The timestamp fields should have either 0s or sane values. The CurrentTime field should always be sane.

Repro

if (WTSEnumerateSessionsEx(HWTSSERVER.WTS_CURRENT_SERVER_HANDLE, out var sessionList)) {
    foreach (var session in sessionList) {
        if (WTSQuerySessionInformation(HWTSSERVER.WTS_CURRENT_SERVER_HANDLE, session.SessionId, WTS_INFO_CLASS.WTSSessionInfo, out var pSessionInfo, out var bytesReturned)) {
            using (pSessionInfo) {
                var sessionInfo = pSessionInfo.ToStructure<WTSINFO>(bytesReturned);
                Console.WriteLine("CurrentTime is {0}", sessionInfo.CurrentTime.ToDateTime());
            }
        }
    }
}
@NN---
Copy link
Contributor

NN--- commented Feb 25, 2022

Also using FILETIME instead of LARGE_INTEGER in WTSINFO definition is not correct.

https://devblogs.microsoft.com/oldnewthing/20040825-00/?p=38053

@NN---
Copy link
Contributor

NN--- commented Feb 25, 2022

Seems like this is the real issue here.
Will submit PR with a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants