Windows Mobile上的服务程序

服务简介

几乎每一个操作系统都有一种在系统启动时刻启动进程的机制,这些进程提供了一些不依赖于任何用户交互式的服务。在Windows中,这样的进程称为服务。在桌面Windows系统中,服务程序由三个组件构成的:服务应用、服务控制程序(SCP)和服务控制管理器(SCM)。(以上参见《深入解析Windows操作系统》第四版第四章第二节。)

桌面系统的服务机制是非常复杂的,至少看的我现在还在晕。在嵌入式系统中,当然不会如此复杂。

Windows CE 5.0服务程序在系统架构中的位置如下图,Services.exe是作为服务DLL文件的宿主,提供开始、暂停和停止服务的能力。服务和驱动(主要由Device.exe加载)有个很有意思的关系,从本质上说它们是一回事。
image 

下图是Windows Mobile 6.0 Professional模拟器Services.exe和Device.exe进程加载的DLL文件的截图:
clip_image002
clip_image002[6]

Windows CE 6.0服务程序在系统架构中的位置,微软把驱动分为用户模式和内核模式:
image 

进一步详细的看下Windows CE 6.0的用户态:
image 

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的内核态:
image

"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即是注册表里指定的前缀:

01EXPORTS
02 ; Explicit exports can go here 
03xxx_Close
04xxx_Deinit
05xxx_Init
06xxx_IOControl
07xxx_Open
08xxx_Read
09xxx_Seek
10xxx_Write

第二步,实现函数,xxx即是注册表里指定的前缀:

001/***************************************************************************
002*
003* Function Name: DllMain
004* Purpose: service entrance
005* Input:
006hinstDLL: Handle to the DLL.
007dwReason: Specifies a flag indicating why the DLL entry-point function is being called.
008lpvReserved: Specifies further aspects of DLL initialization and cleanup.
009* Output:none
010* Return:TRUE
011***************************************************************************/
012BOOL APIENTRY DllMain( HANDLE hinstDLL, DWORD  dwReason,  LPVOID lpvReserved)
013{
014switch( dwReason )
015{
016case DLL_PROCESS_ATTACH:
017    g_hInst=(HINSTANCE)hinstDLL;
018    break;
019case DLL_PROCESS_DETACH:
020{
021    //..
022    break;
023}
024}
025return TRUE;
026}
027/***************************************************************************
028*
029* Function Name:xxx_Close
030* Purpose: This function is implemented by a service and will be called by Services.exe.
031* Input:
032dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance.
033* Output:none
034* Return:TRUE indicates success. FALSE indicates failure. * Remarks:This function is called when a service instance is closing during an application's call to CloseHandle.
035***************************************************************************/
036BOOL xxx_Close(DWORD dwData)
037{   
038    //..
039    //return FALSE;
040}
041/***************************************************************************
042*
043* Function Name:xxx_Deinit
044* Purpose: This function is to be implemented by a service and will be called by Services.exe.
045* Input:
046dwData:Specifies the value returned by xxx_Init (Services.exe) for the given service instance.
047* Output:none
048* Return:TRUE indicates success. FALSE indicates failure.* Remarks:This function is called during an application's call to DeregisterService.
049***************************************************************************/
050BOOL xxx_Deinit(DWORD dwData)
051{   
052    //..
053    //return FALSE;
054}
055/***************************************************************************
056*
057* Function Name:xxx_IOControl
058* Purpose: This function is used to send a control code to a service.
059* Input:
060dwData:  Specifies the value returned by xxx_Init (Services.exe) for the given service instance.
061dwCode: Specifies the control code for the operation.
062pBufIn:   Pointer to a buffer that contains the data required to perform the operation.
063dwLenIn:   Specifies the size, in bytes, of the buffer pointed to by pBufIn.
064dwLenOut:  Specifies the size, in bytes, of the buffer pointed to by pBufOut.
065* Output:
066pBufOut:  Pointer to a buffer that receives the output data from the operation.
067pdwActualOut:Pointer to a variable that receives the size, in bytes, of the data stored into the buffer pointed to by pBufOut.
068* Return:TRUE indicates success. FALSE indicates failure.* Remarks:The control code specifies the action that the driver is to perform. For example, a control code can ask a service            to return information or direct the service to carry out an action. Windows Embedded CE. NET provides a number of            standard control codes. In addition, a service can define its own service-specific control code.
069***************************************************************************/
070BOOL xxx_IOControl(
071DWORD dwData,
072DWORD dwCode,
073PBYTE pBufIn,
074DWORD dwLenIn,
075PBYTE pBufOut,
076DWORD dwLenOut,
077PDWORD pdwActualOut)
078{
079    //..
080    //return TRUE;
081}
082/***************************************************************************
083*
084* Function Name:xxx_Open
085* Purpose: This function is to be implemented by a service and will be called by Services.exe.
086* Input:
087dwData:Specifies the value returned by xxx_Init (Services.exe) for the given service instance.
088dwAccess :Specifies the type of access to the object.
089dwShareMode:Specifies how the object can be shared.
090* Output:none
091* Return:TRUE indicates success. FALSE indicates failure.* Remarks: This function is called during an application's call to CreateFile. The values for the           dwAccess and dwShareMode parameters are passed directly from the call to CreateFile.
092***************************************************************************/
093BOOL xxx_Open(
094DWORD dwData,
095DWORD dwAccess,
096DWORD dwShareMode)
097{   
098    //..
099    //return FALSE;
100}
101/***************************************************************************
102*
103* Function Name:xxx_Read
104* Purpose: This function is to be implemented by a service and will be called by Services.exe. This function need            only be implemented by a streaming service.
105* Input:
106dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance.
107dwLen:Specifies the number of bytes to be read.
108* Output:
109pBuf:Pointer to the storage location for the data that is read.
110* Return:Returns the number of bytes read.* Remarks: This function is called by Services.exe as a result of an application's call to ReadFile.
111***************************************************************************/
112DWORD xxx_Read(
113DWORD dwData,
114LPVOID pBuf,
115DWORD dwLen)
116{
117    //..
118    //return 0;
119}
120/***************************************************************************
121*
122* Function Name:xxx_Seek
123* Purpose: This function is to be implemented by a service and will be called by Services.exe.            This function need only be implemented by a streaming service.
124* Input:
125dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance.
126pos:Specifies the number of bytes to move the file pointer. pos is a 32-bit signed value.
127type:Specifies the starting point for the file pointer move.
128* Output:none
129* Return:Returns the current location of the file pointer.* Remarks: This function is called by Services.exe as a result of an application's call to SetFilePointer.
130***************************************************************************/
131DWORD xxx_Seek(
132DWORD dwData,
133long pos,
134DWORD type)
135{
136    //..
137    //return 0;
138}
139/***************************************************************************
140*
141* Function Name:xxx_Write
142* Purpose: This function is to be implemented by a service and will be called by Services.exe.
143Only streaming services need to implement this function.
144* Input:
145dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance.
146dwInLen:Specifies the length of data in the buffer to be written.
147* Output:
148pInBuf: Pointer to the buffer containing data to write.
149* Return:Returns the number of bytes actually written.* Remark: This function is called by Services.exe as a result of an application's call to WriteFile.
150***************************************************************************/
151DWORD xxx_Write(
152DWORD dwData,
153LPCVOID pInBuf,
154DWORD dwInLen)
155{   
156    //..
157    //return 0;
158}
159/***************************************************************************
160*
161* Function Name:xxx_Init
162* Purpose:This function is to be implemented by a service and will be called by Services.exe.
163* Input:
164dwData:  Specifies the service-supplied data.
165* Output:none
166* Return:Returns a value to be used in calls to xxx_Open (Services.exe).* Remarks: This function is called during RegisterService, in which case dwData will be the fourth parameter            to RegisterService. It is also called during Services.exe initialization, in which case dwData            is the DWORD value set in the registry value HKEY_LOCAL_MACHINE/Services/Service/Context, or zero if this value is not set.
167***************************************************************************/
168DWORD xxx_Init(DWORD dwData)
169{
170    //..
171    //return 1;
172}

第三步,添加注册表:

当系统启动的时候,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:

  • DEVFLAGS_NONE (0x00000000): No flags defined.
  • DEVLFAGS_UNLOAD (0x00000001): Unload service after call to xxx_Init (Services.exe) returns.
  • DEVFLAGS_LOADLIBRARY (0x00000002): Use the LoadLibrary function to load the service DLL.
  • DEVFLAGS_NOLOAD (0x00000004): Do not load the service.
  • DEVFLAGS_TRUSTEDCALLERONLY (0x00010000) : This service only can be called by a privileged process.
  • DEVFLAGS_NOUNLOAD(0x00000020): Do not allow the service to be unloaded.

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在调用上面这些函数时做的操作。)

一个具体例子:

1[HKEY_LOCAL_MACHINE/Services/MySevice]
2"Description"="MySevice"
3"DisplayName"="MySevice"
4"Prefix"="OBX"
5"Dll"="mysevice.dll"
6"Index"=dword:0
7"Keep"=dword:1
8"Order"=dword:9

第四步,代码签名,不签名的服务DLL不会被加载的,这也服务运行失败的常见原因。

签名工具在此

SDK自带的一些证书,在模拟器上实验是可以的:clip_image002[1]

 

2009.6.22更新:虽然.Net CF没有提供任何接口创建Windows Services,但是使用托管代码开发朋友可以参考这篇文章使用C#等语言创建服务。

作者: 王克伟
出处: http://wangkewei.cnblogs.com/
版权声明: 本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任的权利。
您可以从 这里更方便的找到我的文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值