Skip to content

OSVERSIONINFOEX uses incorrect charset with RtlGetVersion() #2095

Closed
@kpreisser

Description

@kpreisser

I'm submitting a...

  • Bug report (I searched for similar issues and did not find one)

Current behavior

Hi, please see the following code in OSVersionHelper.cs (branch rel/3.0.0-preview):
https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/280ae1ed883e1c46bbc491c4545f52808e818fa2/Microsoft.Toolkit.Win32.UI.Controls/Interop/OSVersionHelper.cs#L128-L129

Note that the comment indicates that RtlGetVersion() doesn't set the ProductType in the OSVERSIONINFOEX structure. However, actually RtlGetVersion() sets this property, but OSVERSIONINFOEX isn't set to use Unicode, which is why the properties are not correctly filled as the string field (CSDVersion) will have the incorrect size (128 instead of 256 bytes).

Currently, the struct is defined as follows:
https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/280ae1ed883e1c46bbc491c4545f52808e818fa2/Microsoft.Toolkit.Win32.UI.Controls/Interop/Win32/OSVERSIONINFOEX.cs#L17-L18

Here, Marshal.SizeOf(typeof(OSVERSIONINFOEX)) will return 156.

Expected behavior

The declaration should be changed to:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct OSVERSIONINFOEX

In that case, Marshal.SizeOf(typeof(OSVERSIONINFOEX)) will return 284, and the remaining properties like ProductType should now be set correctly.

Note that the declaration of GetVersionEx() should then also be changed from

    [DllImport(ExternDll.Kernel32, SetLastError = true, EntryPoint = "GetVersionEx")]
    private static extern bool _GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

to e.g.:

    [DllImport(ExternDll.Kernel32, SetLastError = true, EntryPoint = "GetVersionExW", CharSet = CharSet.Unicode)]

so that it uses the wide variant (GetVersionExW). But I think it can also be removed because RtlGetVersion should work correctly now.

Environment

Nuget Package(s): Microsoft.Toolkit.Win32.UI.Controls

Package Version(s): 3.0.0-preview

Windows 10 Build Number:
- [X] April 2018 Update (17134)

App min and target version:
- [X] April 2018 Update (17134)

Device form factor:
- [X] Desktop

Visual Studio 
- [X] 2017 (version: 15.7.0)

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions