由于一些很[哔——]的原因,微软在WinRT中移除了获取操作系统版本号的API,再也没有Environment.OSVersion.Version这种东西用了。但是这挡不住程序员们……目前据我所知有两种黑科技可以获取操作系统版本号:

使用PnpObject

以下代码来自AIDA64[1]应用(通过反编译获取):

public static async Task<string> GetWindowsVersionAsync()
{
    string str;
    string[] strArray = { "{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3" };
    PnpObject halDevice = await GetHalDevice(strArray);
    if (halDevice == null || !halDevice.Properties.ContainsKey("{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3"))
        str = null;
    else
        str = halDevice.Properties["{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3"].ToString();
    return str;
}

private static async Task<PnpObject> GetHalDevice(params String[] properties)
{
    String[] strArray = properties;
    String[] strArray1 = { "{A45C254E-DF1C-4EFD-8020-67D146A850E0},10" };
    IEnumerable<String> enumerable = strArray.Concat(strArray1);
    PnpObjectCollection pnpObjectCollection = await PnpObject.FindAllAsync(PnpObjectType.Device, enumerable, "System.Devices.ContainerId:="{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"");

    foreach (PnpObject pnpObject in pnpObjectCollection)
    {
        if (pnpObject.Properties == null || !pnpObject.Properties.Any())
            continue;

        KeyValuePair<String, Object> keyValuePair = pnpObject.Properties.Last();
        if (keyValuePair.Value == null || !keyValuePair.Value.ToString().Equals("4d36e966-e325-11ce-bfc1-08002be10318"))
            continue;
        return pnpObject;
    }
    return null;
}
使用LoadLibraryExA和RtlGetVersion

这个方法需要通过C++/CX代码获取LoadLibraryExA()的地址,加载ntdll.dll之后获取RtlGetVersion()的地址,然后强行调用。目前暂时不方便公开代码,留个坑(逃

参考资料:
[1] AIDA64
[2] How to detect in my Windows 8 app, that it’s running on Windows 8.1?

» 转载请注明来源及链接:未来代码研究所

Related Posts:

One Response to “Windows Phone 8.1应用移植手记5:获取操作系统版本号”

Leave a Reply

World Line
Time Machine
Friendly Links
Online Tools