服务简介几乎每一个操作系统都有一种在系统启动时刻启动进程的机制,这些进程提供了一些不依赖于任何用户交互式的服务。在Windows中,这样的进程称为服务。在桌面Windows系统中,服务程序由三个组件构成的:服务应用、服务控制程序(SCP)和服务控制管理器(SCM)。(以上参见《深入解析Windows操作系统》第四版第四章第二节。) 桌面系统的服务机制是非常复杂的,至少看的我现在还在晕。在嵌入式系统中,当然不会如此复杂。 Windows CE 5.0服务程序在系统架构中的位置如下图,Services.exe是作为服务DLL文件的宿主,提供开始、暂停和停止服务的能力。服务和驱动(主要由Device.exe加载)有个很有意思的关系,从本质上说它们是一回事。
下图是Windows Mobile 6.0 Professional模拟器Services.exe和Device.exe进程加载的DLL文件的截图: Windows CE 6.0服务程序在系统架构中的位置,微软把驱动分为用户模式和内核模式:
进一步详细的看下Windows CE 6.0的用户态:
ServicesD.EXE用于服务的宿主,UDevice.EXE用于用户态驱动的宿主。 "ServicesD.exe is a process that supplements the Udevice.exe process. ServicesD.exe provides enhanced loading capabilities such as support for starting, pausing, and stopping services. The programming model for writing services and writing device drivers is very similar in Windows Embedded CE. You can develop a server that runs on Udevice.exe rather than on ServicesD.exe, with identical code, provided that your server does not require advanced features offered by ServicesD.exe. “ 再详细看一下Windows CE 6.0的内核态: "DEVMGR.DLL is the Device Manager that is loaded by the kernel, it runs continuously, and it manages loaded device drivers and their interfaces. When the Device Manager loads, it also loads the I/O Resource Manager to read a list of available resources from the registry. “ 动手在WM 6.0 Professional下写一个服务第一步,在def文件中添加导出函数,xxx即是注册表里指定的前缀:
第二步,实现函数,xxx即是注册表里指定的前缀:
第三步,添加注册表:当系统启动的时候,Services.exe会遍历HKEY_LOCAL_MACHINE\Services注册表位置下子键,每个子键代表一个服务,Services.exe按照对应键值初始化服务,并且按照键值指定的顺序。 HKEY_LOCAL_MACHINE\Services\<Service Name>下的键值的说明: Context : REG_DWORD类型 Specifies the initial value that is passed into the initialization routine.
Description : REG_SZ Description of display service. DisplayName : REG_SZ Display service name. Dll : REG_SZ Dynamic-link library (DLL) file to be loaded. Flags : REG_DWORD Specifies a set of flags used to modify the behavior of the ActivateService function. The following list shows the valid flags:
Index : REG_SZ Service index. Keep : REG_DWORD If Keep = 0, the DLL will be unloaded immediately after initialization. Order : REG_DWORD Order in which Services.exe will load each service. The service with the lowest order is loaded first. Prefix : REG_SZ Prefix of the DLL.(3个英文字符。为什么?看看文档里介绍的Service.exe在调用上面这些函数时做的操作。) 一个具体例子:
第四步,代码签名,不签名的服务DLL不会被加载的,这也服务运行失败的常见原因。签名工具在此。 SDK自带的一些证书,在模拟器上实验是可以的: 2009.6.22更新:虽然.Net CF没有提供任何接口创建Windows Services,但是使用托管代码开发朋友可以参考这篇文章使用C#等语言创建服务。 作者: 王克伟
(王克伟)出处: http://wangkewei.cnblogs.com/ 版权声明: 本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任的权利。 您可以从这里更方便的找到我的文章。 |