GetProductInfo() 在 UWP 应用程序中给出异常

GetProductInfo() Giving exception in UWP app

我正在使用来自 Kernel32.dll

的 GetProductInfo()

代码示例:

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

但是当我尝试在运行时调用此方法时,我收到一条异常消息,

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.

但是如果我在我的项目中手动包含“Kernel32.dll”,一切正常。 谁能解释一下到底是什么问题。

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

简而言之,GetProductInfo 方法在 UWP 中不可用。 UWP 不支持所有 win32 API。 Windows 10 通用 Windows 平台 (UWP) 应用程序只能使用 Win32 和 COM API 的子集。参见 Win32 and COM APIs for UWP apps

您手动将 "Kernel32.dll" 添加到您的项目中。其实就是你把这个dll包含在你的app包里。它似乎适用于您的测试环境,但是当您要将您的应用程序发布到 windows 商店时,您的包将无法通过 WACK。 Windows 应用认证工具包允许您的应用仅使用 Win32 和 COM API.

的这个子集