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

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

问题描述

我在Silverlight 5外浏览器中有一个简单的FM Radio Tuner应用程序,它使用PInvoke从我的USB FM接收器中调用来自本机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.

问题是,当我运行该应用程序并从曾经在我的SL5应用程序中工作的PInvoked本机DLL中调用任何方法时(即使在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:\Users\Gutemberg\Documents\Visual Studio 11\Projects\MetroRadio\MetroRadio\MediaService.cs:line 160
       at MetroRadio.MainPage.radio_Click(Object sender, RoutedEventArgs e) in c:\Users\Gutemberg\Documents\Visual Studio 11\Projects\MetroRadio\MetroRadio\MainPage.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 Interop. Win32 API也受支持,并且可以通过上述技术进行调用,尽管它仅限于Metro应用程序可用的那些功能(即,如果C ++ Metro应用程序不能调用它,.NET应用程序也不能). /p>

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

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

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