如何在WinRT 8.1上P/Invoke进入kernel32.dll [英] How to P/Invoke into kernel32.dll on WinRT 8.1

查看:79
本文介绍了如何在WinRT 8.1上P/Invoke进入kernel32.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本机API方法(GetNativeSystemInfo),该方法被标记为Windows 8.1上的手机和桌面商店应用程序都受支持.在文档中,它被列为kernel32.dll的宿主.伟大的!因此,我第一次尝试P/Invoke的过程是这样的:

I'm trying to use a native API method (GetNativeSystemInfo) that is marked as supported for both phone and desktop Store apps on Windows 8.1. In the documentation, it is listed as living in kernel32.dll. Great! So my first attempt at P/Invoke looked like this:

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = false, PreserveSig = true)]
private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSysInfo);

不幸的是,这无法在实际设备上运行-找不到kernel32!碰巧的是,有 kernelBase.dll,因此是我的第二次尝试:

Unfortunately this fails to run on actual devices - kernel32 is not found! As it happens, there is kernelBase.dll, and thus my second attempt:

[DllImport("kernelBase.dll", CharSet = CharSet.Unicode, ExactSpelling = false, PreserveSig = true)]
private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSysInfo);

虽然在我的手机上可以正常运行,但会导致该应用无法通过认证;方法名称和"kernelBase.dll"似乎没有列入白名单.

While this runs fine on my phone, it causes the app to fail certification; the method name and "kernelBase.dll" don't seem to be whitelisted.

这是对WACK的疏忽,还是导致该API在Store应用中无法使用的错误?我的目标是获取有关正在运行的处理器的信息(体系结构,类型等),我不希望因为这种简单的事情而使用C ++.如果此API在实践中不可用,是否还有另一种方式来获取此信息?

Is this an oversight of WACK, or a bug that renders this API unusable in Store apps? My goal is to get information about the running processor (architecture, type, etc), and I'd prefer not to drop in to C++ for something this simple. If this API is not usable in practice, is there another way to get this info?

推荐答案

对于Windows Phone和Windows Store版本,您将需要不同的pinvoke签名.有关电话参考,请参阅api-ms-win-core-sysinfo-l1-2-0.dll

You'll need different pinvoke signatures for the Windows Phone and the Windows Store versions. For the phone reference GetNativeSystemInfo from api-ms-win-core-sysinfo-l1-2-0.dll

#if WINDOWS_PHONE_APP
     [DllImport("api-ms-win-core-sysinfo-l1-2-0.dll", CharSet = CharSet.Unicode, ExactSpelling = false, PreserveSig = true)]
     private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSysInfo);
#else
     [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = false, PreserveSig = true)]
     private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSysInfo);
#endif

请参见 Windows Phone 8支持的Win32 API 列表(针对SL,但也对您的Runtime应用有效).如果您以本地方式调用该函数,则将自动引用正确的引用,但是对于pinvoke,该工具不可用.通常,将功能包装在本机Windows运行时组件中要比p调用容易,除非您只有几个简单的p调用.

See Supported Win32 APIs for Windows Phone 8 for a list (targetted for SL, but also valid for your Runtime app). The right reference will be automatically referenced if you call the function natively, but the tooling isn't there to do so for pinvoke. In general wrapping the function in a native Windows Runtime Component is easier than p-invoke, unless you only have a few, simple p-invokes.

这篇关于如何在WinRT 8.1上P/Invoke进入kernel32.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆