---
title: "windows moblie 5.0在托管程序中实现短信接收和拦截"
author: "cjc"
date: "2008-01-12T03:27:00Z"
tags: []
slug: "windows-moblie-5-0"
draft: false
lastmod: "2008-01-12T03:27:00Z"
id: 402727
url: "https://cjc.iteye.com/blog/402727"
---

&lt;script&gt;function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}&lt;/script&gt;

曾经尝试只用.net cf 在手机（windows moblie 5.0）中实现短信的接收，而同时拦截手机本身的短信提示，代码如下：

private void Form1\_Load(object sender, EventArgs e)  
{

interceptor = new MessageInterceptor();  
interceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;  
interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor\_MessageReceived);  
}

void interceptor\_MessageReceived(object sender, MessageInterceptorEventArgs e)  
{  
SmsMessage msg = e.Message as SmsMessage;

> this.txtMessage.Text ="发件人："+msg.From.Address +",内容：" +msg.Body +"时间:"+msg.Received.ToString("yyyy年MM月dd日 MM时mm分ss秒")
> 
> }
> 
> 上述程序是通过在窗体加载时委托一个拦截短信事件，虽然实现我想要的功能，但有个确定，当应用程序不处于被激活的状态，就无效了。后来在ms的网站上找到一份资料：[Receiving SMS Messages Inside a Managed Application](http://msdn2.microsoft.com/en-us/library/aa446561.aspx)，可以来实现我想要的这个功能,原理简单来说是修改手机的注册表，手机短信程序启动后加载dll文件，把拦截规则注入，然后通过.net cf 程序来获得拦截的短信。本质上说，不是“拦截”，只是把短信直接删除到“废件箱”，仿佛“拦截”了。

实现条件:

1、安装Windows Mobile 5.0 Pocket PC SDK

2、 开发工具 vs2005

3、安装实例程序：[http://www.microsoft.com/downloads/details.aspx?FamilyId=98CCF3D4-DB7C-4A7D-A323-53BBDBBE0420&displaylang=en](http://www.microsoft.com/downloads/details.aspx?FamilyId=98CCF3D4-DB7C-4A7D-A323-53BBDBBE0420&displaylang=en)

方法：

1、用vs ++ 2005 打开实例程序中的Mapirule（在D:\\Program Files\\.NET Compact Framework Samples\\Receiving SMS Messages\\Code\\Mapirule中，假定你的安装目录是D:\\Program Files），在这个提供的c++中只实现了“短信内容中”包含“zzz”字符的短信拦截，我修改了一下，可以实现对所有短信的拦截：  
源代码 ：[mapirule.cpp](http://www.cnblogs.com/Files/xjb/mapirule.rar)

不过要这个c++的项目编译通过，并生成一个setup文件，还必须安装一个微软提供的一个证书文件，在

安装Windows Mobile 5.0 Pocket PC SDK后会在D:\\Program Files\\Windows CE Tools\\wce500\\Windows

Mobile 5.0 Pocket PC SDK\\Tools 下有个SDKSamplePrivDeveloper.pfx，直接双击安装，安装过程很简

单（安装过程中有个提示需要输入密码，不必理会，直接点下一步就可以了），这个证书是ms对开发者

用作测试用的，有效期到2009年。编译通过后，在手机上运行setup.exe文件，安装完成后注意一定要重

启手机。

2.编写net cf程序 ：参考（D:\\Program Files\\.NET Compact Framework Samples\\Receiving SMS

Messages\\Code\\CaptureSMS）  
源文件：[](http://www.cnblogs.com/Files/xjb/SMSListener.rar)[](http://www.cnblogs.com/Files/xjb/SMSListener.rar)[SMSListener.cs](http://www.cnblogs.com/Files/xjb/SMSListener.rar)  
源文件：[frmHProcess.cs](http://www.cnblogs.com/Files/xjb/frmHProcess.rar)

上述程序，改造一下，可以拦截垃圾短信  
环境：在vs2005+windows mobile 5.0 ＋多普达818测试通过。