如何编写引用本机代码的Windows 10通用类库?(How do I write a Windows 10 universal class library that references nativ

编程入门 行业动态 更新时间:2024-10-23 15:20:29
如何编写引用本机代码的Windows 10通用类库?(How do I write a Windows 10 universal class library that references native code?)

我有一个C#类库,它引用并包装Win32本机方法。 但我似乎无法将此代码移植到更新的“通用”C#类库中。 为了正确封送这些原生方法,我需要做些什么?

I have a C# class library that references and wraps Win32 native methods. But I can't seem to port this code over to a newer "universal" C# class library. What do I need to do in order to properly marshal these native methods?

最满意答案

Windows 10 Universal Applications在Windows运行时API下运行 ,这是一种适用于Windows的新“本机”API。 使用此API的应用程序在沙箱中运行,这限制了它们对设备和操作系统服务的访问。 虽然可以像平常一样访问Win32 API,例如在C#代码的情况下使用P / Invoke, 但沙箱只允许使用某些API函数 :

Windows运行时应用程序可以使用Win32和COM API的子集。 选择此API子集是为了支持Windows运行时应用程序的关键方案,这些方案尚未包含在Windows运行时,HTML / CSS或其他受支持的语言或标准中。 Windows应用认证工具包可确保您的应用仅使用Win32和COM API的此子集。 在本机应用程序中,您可以直接调用这些API。 在托管应用程序中,您可以通过Windows运行时组件或通过P / Invoke调用它们。

...

API中每个编程元素的文档指示它是否可以在Windows运行时应用程序中使用。 请参阅页面顶部的“适用于”行。

因此,例如, FormatMessage适用于Windows运行时,但GetNamedPipeInfo不适用。

如果您要使用的Win32 API不是Windows运行时支持的那个,我相信您唯一的选择是让您的类库定位传统桌面应用程序。

Windows 10 Universal Applications run under the 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:

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.

So for example FormatMessage works with Windows Runtime, but GetNamedPipeInfo doesn't.

If the Win32 API you want to use isn't one of the ones supported by Windows Runtime, I believe your only option is to have your class library target traditional desktop apps.

更多推荐

本文发布于:2023-08-07 10:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:本机   类库   代码   Windows   write

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!