2019年3月16日土曜日

How to know the current processor architecture from UWP app

Unfortunately, WinRT does not have such of API. We, in 2019,  need to use pinvoke.

--start--
[DllImport("api-ms-win-core-sysinfo-l1-2-3.dll")]
static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public ushort processorArchitecture;
ushort reserved;
public uint pageSize;
public IntPtr minimumApplicationAddress;
public IntPtr maximumApplicationAddress;
public IntPtr activeProcessorMask;
public uint numberOfProcessors;
public uint processorType;
public uint allocationGranularity;
public ushort processorLevel;
public ushort processorRevision;
}
...
var si = new SYSTEM_INFO();
GetNativeSystemInfo(ref si);
Windows.System.ProcessorArchitecture at = (Windows.System.ProcessorArchitecture)si.processorArchitecture;
string architecture = at.ToString();
if( 12 == (int)at)
{
architecture = "Arm64";
}
view raw gistfile1.txt hosted with ❤ by GitHub
--end--

You may noticed that this unfamiliar name "api-ms-win-core-sysinfo-l1-2-3.dll". This is a sort of "OneCore Umbrella library".

OneCore.lib umbrella library
https://docs.microsoft.com/ja-jp/windows/desktop/apiindex/umbrella-lib-onecore-alpha

I could not tell you how this works, but works. :) It's too difficult for me. For detail, this site may helps you.

Runtime DLL name resolution: ApiSetSchema - Quarkslab's blog
https://blog.quarkslab.com/runtime-dll-name-resolution-apisetschema-part-i.html
https://blog.quarkslab.com/runtime-dll-name-resolution-apisetschema-part-ii.html

The API Set Schema - Geoff Chappell
http://www.geoffchappell.com/studies/windows/win32/apisetschema/index.htm


Note - UWP Community toolkit have the System Helper API. By using this, you can get the propertry "SystemInformation.OperatingSystemArchitecture". The document tell that  this property is "Gets used processor architecture" but actually not. It just return the "target" architecture of the UWP app package.

SystemInformation - UWP Community Toolkit
https://docs.microsoft.com/ja-jp/windows/communitytoolkit/helpers/systeminformation

0 件のコメント:

コメントを投稿