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

WTSQuerySessionInformation throws exception on null hServer #1435

Closed
nefarius opened this issue Jan 19, 2023 · 1 comment
Closed

WTSQuerySessionInformation throws exception on null hServer #1435

nefarius opened this issue Jan 19, 2023 · 1 comment
Assignees
Labels
broken api An API is inaccurate and could lead to runtime failure bug Something isn't working

Comments

@nefarius
Copy link

Actual behavior

When passing null as hServer argument to WTSQuerySessionInformation it throws a System.ArgumentNullException despite the native WinApi docs stating that NULL (the macro WTS_CURRENT_SERVER_HANDLE expands to (HANDLE)NULL) is allowed to set the current machine as server.

Expected behavior

WTSQuerySessionInformation to succeed even with null as hServer.

Repro steps

  1. NativeMethods.txt content:
WTSGetActiveConsoleSessionId
WTSQuerySessionInformation
WTSFreeMemory
WTS_INFO_CLASS
  1. Any of your own code that should be shared?
    private static unsafe string GetUsername(int sessionId)
    {
        string username = SystemSessionUsername;
        if (PInvoke.WTSQuerySessionInformation(null, (uint)sessionId, WTS_INFO_CLASS.WTSUserName, out PWSTR buffer,
                out uint strLen) && strLen > 1)
        {
            username = new string(buffer.Value);
            PInvoke.WTSFreeMemory(buffer);
        }

        return username;
    }

Context

  • CsWin32 version: 0.2.138-beta
  • Win32Metadata version (if explicitly set by project): not explicitly set
  • Target Framework: net7.0-windows10.0.17763.0
  • LangVersion: latest
@nefarius nefarius added the bug Something isn't working label Jan 19, 2023
@AArnott
Copy link
Member

AArnott commented Jan 20, 2023

This is a bug in the win32metadata, as it doesn't annotate the hServer parameter with [Optional]:

public unsafe static extern BOOL WTSQuerySessionInformationW([In] HANDLE hServer, [In] uint SessionId, [In] WTS_INFO_CLASS WTSInfoClass, [Out] PWSTR* ppBuffer, [Out] uint* pBytesReturned);

Contrast this with CreateFile, which does annotate its HANDLE hTemplateFile parameter with [Optional]:

public unsafe static extern HANDLE CreateFileW([In][Const] PWSTR lpFileName, [In] FILE_ACCESS_FLAGS dwDesiredAccess, [In] FILE_SHARE_MODE dwShareMode, [Optional][In] SECURITY_ATTRIBUTES* lpSecurityAttributes, [In] FILE_CREATION_DISPOSITION dwCreationDisposition, [In] FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes, [Optional][In] HANDLE hTemplateFile);

@AArnott AArnott transferred this issue from microsoft/CsWin32 Jan 20, 2023
@AArnott AArnott added the broken api An API is inaccurate and could lead to runtime failure label Jan 20, 2023
@mikebattista mikebattista self-assigned this Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broken api An API is inaccurate and could lead to runtime failure bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants