wp7中编写native代码的方法

转载,未验证 http://blog.walshie.me/2010/11/12/windows-phone-7-native-apps-the-missing-link/

Ok, take a deep breath, let it sink in, are you ready?

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.

Anyone wanting to test or need more information on this feel free to email me via my blog.

如鹏网.Net培训班正在报名,有网络的地方就可以参加如鹏网的学习,学完就能高薪就业,点击此处了解

 

    三年前只要懂“三层架构”就可以说“精通分层架构”;现在则需要懂IOC(AutoFac等)、CodeFirst、lambda、DTO等才值钱;

    三年前只要会SQLServer就可以说自己“精通数据库开发”;现在则需还需要掌握MySQL等开源数据库才能说是“.Net开源”时代的程序员;

    三年前只要会进行用户上传内容的安全性处理即可;现在则需要熟悉云存储、CDN等才能在云计算时代游刃有余;

    三年前只要掌握Lucene.Net就会说自己“熟悉站内搜索引擎开发”;现在大家都用ElasticSearch了,你还用Lucene.Net就太老土了;

    三年前发邮件还是用SmtpClient;现在做大型网站发邮件必须用云邮件引擎;

    三年前缓存就是Context.Cache;现在则是Redis、Memcached的天下;

    如鹏网再次引领.Net社区技术潮流!点击此处了解如鹏网.Net最新课程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值