wp7中调用native代码的方法

What I discovered today was some FUNKY code that Samsung had created to set the Network properties of a Windows Phone 7 handset. Taking a peek under the skirt, I noticed something unreal. The application has unmanaged DLL’s inside the XAP file!!!

XAPFile

Ok, you’re thinking, so what, this isn’t anything special. All they’ve done is add them manually into the project as a content build action.

True, they have done that, but what’s next is even more UNREAL.

Looking “into” NetworkProfile.dll (Silverlight managed code) reveals the TRUTH we’ve all been waiting to hear, can I run a “native” app for Windows Phone 7? The answer is TRUE (with some caveats).

There’s a funky DLL called Microsoft.Phone.InteropServices that lives in the GAC of all WP7 handsets that we need to enable COM access.

InteropServices

Now for this blog post, we’re only going to cover the “ComBridge” class in the above picture. When you open it in reflector it’s just an empty static method. But it’s magic is hidden.

Now to use this DLL in our application you can’t simply add it as a reference,you need to load the Assembly via reflection,and call its static methods.

Assembly a = Assembly.Load("Microsoft.Phone.InteropServices, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e");

Ok, now we’ve loaded it. We need to get the type (from the namespace), get the method and invoke it.

Type comBridgeType = a.GetType("Microsoft.Phone.InteropServices.ComBridge");
            MethodInfo dynMethod = comBridgeType.GetMethod("RegisterComDll", BindingFlags.Public | BindingFlags.Static);
            dynMethod.Invoke(null, new object[] { "Assembly.dll", new Guid("SomeGuidHere") });

Ok, now we’re almost ready. We need to clear ALL capabilities in WMAppManifest.xml and add the following line.

<Capability Name="ID_CAP_INTEROPSERVICES"/>

Once you’ve done that, add a blank XML file in the root of the app called “WMInteropManifest.xml” and add the following content to it.

<?xml version="1.0" encoding="UTF-8"?>
<Interop>
</Interop>

Yes I’m aware the tags are empty, they’re not required. Once you’ve done all that, right click and deploy your app. Before this will work, I’ve found you need to reboot your device for it to actually work.

That’s all for now, I’ll be posting more this weekend about how you can get access to the RIL (Radio Interface Layer), Registry & even access to the file system.

Above I mentioned some “caveats”. Sure you’re now loading native DLL’s, but your application is STILL running as a managed instance, and you are still bound to the normal restrictions from the OS. You will still get tombstoned, killed etc.

这样才Windows Phone中就可以调用Win32 DLL了,也就可以在Windows Phone中调用C/C++写的代码了

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值