使用 Windows 8/WinRT 在 Metro 应用程序中 PInvoke [英] PInvoke in Metro Apps with Windows 8 / WinRT

查看:18
本文介绍了使用 Windows 8/WinRT 在 Metro 应用程序中 PInvoke的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silverlight 5 Out-of-browser 中有一个简单的 FM Radio Tuner 应用程序,它使用 PInvoke 从我的 USB FM 接收器调用来自 Native C++ DLL 的代码.此应用程序在带有 Silverlight5 的 Windows 8 中运行良好.

I have an simple FM Radio Tuner App in Silverlight 5 Out-of-browser that calls code from a Native C++ DLL from my USB FM Receiver using PInvoke. This application works very fine in Windows 8 with Silverlight5.

现在我正在尝试使用 WinRT 将其移植到 Metro 应用程序.

Now I'm trying to port it to a Metro App using WinRT.

从 Silverlight 迁移到 XAML UI 非常简单并且运行良好.使用命名空间 System.Runtime.InteropServices 和 DLLImport 属性检测到 PInvoke 签名和属性很好.

The migration from Silverlight to XAML UI is pretty easy and worked well. The PInvoke signatures and attributes was detected fine with the namespace System.Runtime.InteropServices and the DLLImport attribute.

问题是,当我运行应用程序并从 PInvoked 本机 DLL 调用任何在我的 SL5 应用程序中工作的方法时(即使在 Windows8 上也能正常工作),我有这个异常:

The problem is that when I'm running the App and call any method from the PInvoked native DLL that used to work in my SL5 app(that works fine even on Windows8), I have this exception:

System.DllNotFoundException was unhandled by user code
  Message=Unable to load DLL 'CarTFTFM.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
  Source=metroradio
  TypeName=""
  StackTrace:
       at MetroRadio.FMRadio.HWInit2(Int32 port, Int32 initVolume)
       at MetroRadio.MediaService.InitializeRadio() in c:UsersGutembergDocumentsVisual Studio 11ProjectsMetroRadioMetroRadioMediaService.cs:line 160
       at MetroRadio.MainPage.radio_Click(Object sender, RoutedEventArgs e) in c:UsersGutembergDocumentsVisual Studio 11ProjectsMetroRadioMetroRadioMainPage.xaml.cs:line 43
  InnerException: 

那么,我的问题是,这个 PInvoke 有什么问题?由于它在带有 SL5 应用程序的 Windows8 上运行良好,我看不出有任何理由与 WinRT/Metro 应用程序有问题.

So, my question is, what is the problem on this PInvoke? Since it Works well on Windows8 with a SL5 app, I dont see any reasons to have problems with WinRT/Metro Apps.

推荐答案

根据 Pavel Minaev 的回复对 Chris Pietschmann 的回答:

As per Pavel Minaev's reply to Chris Pietschmann's answer:

.NET Metro 应用程序肯定支持 P/Invoke 和 COM 互操作.Win32 API 也受支持,可以通过上述技术调用,但仅限于 Metro 应用程序可用的那些功能(即,如果 C++ Metro 应用程序不能调用它,.NET 应用程序也不能).

P/Invoke and COM Interop are most certainly supported for .NET Metro apps. Win32 API is also supported, and can be called through the above technologies, though it is limited to those functions that are available to Metro apps (i.e. if a C++ Metro app can't call it, neither can a .NET app).

正如 Morten Frederiksen 指出的那样,CarTFTFM.dll 很可能包含 Win32 中不受支持的部分WinRT 的 API 界面:

As Morten Frederiksen points out, it is however likely that CarTFTFM.dll contains an unsupported part of the Win32 API surface for WinRT:

仅支持 Win32 和 COM API 的子集:msdn.microsoft.com/en-us/library/windows/apps/br205757

Only a subset of Win32 and COM API is supported: msdn.microsoft.com/en-us/library/windows/apps/br205757

这篇关于使用 Windows 8/WinRT 在 Metro 应用程序中 PInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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