Windows 10, UWP and PInvoke – GetNativeSystemInfo

Quite a long time ago, I wrote some Windows 8.1 app code that used the Win32 function GetNativeSystemInfo in order to figure out the processor architecture, number of processors and so on that a system has.

The Windows 10/UWP docs say that this function is available for Windows 10 apps and so it seems reasonable to interop to it;

    [DllImport("kernel32.dll")]
    static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

and that worked for me on Windows 10 desktop but I found that when I moved the code to Windows 10 Mobile (Preview) I hit a bit of a problem in that the PInvoke layer couldn’t find kernel32.dll.

I was a bit surprised at first but did some native debugging on my phone and, sure enough, the modules window showed that I didn’t have kernel32.dll loaded into my process. Instead, I had a DLL called kernel32legacy.dll.

Further, when I made a temporary “hack and try” change to the code to DllImport the DLL called kernel32legacy.dll I found that the PInvoke layer was then throwing an ‘entry point not found’ exception.

So, it was seeming that I had a kernel32.dll on the desktop which exported GetNativeSystemInfo and then I have a kernel32legacy.dll on the phone with no GetNativeSystemInfo exported.

What to do? Not at all sure.

This MSDN forums post shed some light and suggested a PInvoke signature like this;

    [DllImport("api-ms-win-core-sysinfo-l1-2-0.dll")]
    static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

and that works on the phone.

What then really surprised me is that it also works on desktop.

Clever people those Windows folks – I haven’t seen this type of PInvoke before so it was news to me and I thought I’d share in case other people bump into it.