如何编写引用本机代码的 Windows 10 通用 class 库?

How do I write a Windows 10 universal class library that references native code?

我有一个引用和包装 Win32 本机方法的 C# class 库。但我似乎无法将这段代码移植到更新的 "universal" C# class 库中。我需要做什么才能正确编组这些本机方法?

Windows Windows Runtime API, a new "native" API for Windows. Applications using this API run in a sandbox which limits their access to devices and operating system services. While Win32 APIs can be accessed like normal, for example using P/Invoke in the case of C# code, the sandbox only permits the use of certain API functions下运行10个通用应用:

Windows Runtime apps can use a subset of the Win32 and COM APIs. This subset of APIs was chosen to support key scenarios for Windows Runtime apps that were not already covered by the Windows Runtime, HTML/CSS, or other supported languages or standards. The Windows App Certification Kit ensures that your app uses only this subset of the Win32 and COM API. In a native app, you can call these APIs directly. In a managed app, you can call them via a Windows Runtime Component, or via P/Invoke.

...

The documentation for each programming element in the API indicates whether it can be used in a Windows Runtime app. See the "Applies to" line at the top of the page.

所以例如 FormatMessage works with Windows Runtime, but GetNamedPipeInfo 没有。

如果您要使用的 Win32 API 不是 Windows 运行时支持的 Win32 之一,我相信您唯一的选择是让您的 class 库以传统为目标桌面应用程序。