Search code examples
uwppinvokedllimportwindows-community-toolkit

GetProductInfo() Giving exception in UWP app


I am consuming GetProductInfo() from Kernel32.dll

Code Sample :

[DllImport("kernel32.dll", SetLastError = false)]
    static extern bool GetProductInfo(
  int dwOSMajorVersion,
  int dwOSMinorVersion,
  int dwSpMajorVersion,
  int dwSpMinorVersion,
  out int pdwReturnedProductType);

but when I try to invoke this method at runtime , i am getting an excption saying,

ExeptionMessage: "Unresolved P/Invoke method 'GetProductInfo!kernel32.dll' from this method. Please look for this method in build warnings for more details. "

Build Warning: MCG : warning MCG0007: Unresolved P/Invoke method 'Kernel32.dll!GetProductInfo' for method 'GetProductInfo'. Calling this method would throw exception at runtime. Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. If for some reason your P/Invoke does not satisify those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP APIs.

But If i manually include "Kernel32.dll" in my project everything works fine. Can anybody please explain what exactly be the issue.


Solution

  • But If i manually include "Kernel32.dll" in my project everything works fine. Can anybody please explain what exactly be the issue.

    In short, the GetProductInfo method is not available in UWP. Not all win32 APIs are supported in UWP. Windows 10 Universal Windows Platform (UWP) apps only can use a subset of the Win32 and COM APIs. See Win32 and COM APIs for UWP apps.

    You manually included "Kernel32.dll" in your project. It's actually that you make this dll contained in your app package. It seems that it works in your testing environment, but when you're going to publish your app to windows store, your package will not be able to pass the WACK. The Windows App Certification Kit allows that your app uses only this subset of the Win32 and COM API.