织梦CMS - 轻松建站从此开始!

罗索

Windows Mobile上的服务程序

落鹤生 发布于 2011-03-06 14:40 点击:次 
Windows CE 5.0服务程序在系统架构中的位置如下图,Services.exe是作为服务DLL文件的宿主,提供开始、暂停和停止服务的能力。服务和驱动(主要由Device.exe加载)有个很有意思的关系,从本质上说它们是一回事。
TAG:

服务简介

几乎每一个操作系统都有一种在系统启动时刻启动进程的机制,这些进程提供了一些不依赖于任何用户交互式的服务。在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即是注册表里指定的前缀:

  1. EXPORTS  
  2.  ; Explicit exports can go here   
  3. xxx_Close  
  4. xxx_Deinit  
  5. xxx_Init  
  6. xxx_IOControl  
  7. xxx_Open  
  8. xxx_Read  
  9. xxx_Seek  
  10. xxx_Write  

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

  1. /*************************************************************************** 
  2. * 
  3. * Function Name: DllMain 
  4. * Purpose: service entrance 
  5. * Input: 
  6. hinstDLL: Handle to the DLL. 
  7. dwReason: Specifies a flag indicating why the DLL entry-point function is being called. 
  8. lpvReserved: Specifies further aspects of DLL initialization and cleanup. 
  9. * Output:none 
  10. * Return:TRUE 
  11. ***************************************************************************/ 
  12. BOOL APIENTRY DllMain( HANDLE hinstDLL, DWORD  dwReason,  LPVOID lpvReserved) 
  13. switch( dwReason ) 
  14. case DLL_PROCESS_ATTACH: 
  15.     g_hInst=(HINSTANCE)hinstDLL; 
  16.     break
  17. case DLL_PROCESS_DETACH: 
  18.     //.. 
  19.     break
  20. return TRUE; 
  21. /*************************************************************************** 
  22. * 
  23. * Function Name:xxx_Close 
  24. * Purpose: This function is implemented by a service and will be
  25.  called by Services.exe. 
  26. * Input: dwData:Specifies the value returned by xxx_Open (Services.exe)
  27.  for the given service instance. 
  28. * Output:none 
  29. * Return:TRUE indicates success. FALSE indicates failure.
  30. * Remarks:This function is called when a service instance is
  31.  closing during an application's call to CloseHandle. 
  32. ***************************************************************************/ 
  33. BOOL xxx_Close(DWORD dwData) 
  34. {     
  35.     //.. 
  36.     //return FALSE; 
  37. /*************************************************************************** 
  38. * 
  39. * Function Name:xxx_Deinit 
  40. * Purpose: This function is to be implemented by a service and
  41.  will be called by Services.exe. 
  42. * Input:dwData:Specifies the value returned by xxx_Init (Services.exe)
  43.  for the given service instance. 
  44. * Output:none 
  45. * Return:TRUE indicates success. FALSE indicates failure.
  46. * Remarks:This function is called during an application's
  47.  call to DeregisterService. 
  48. ***************************************************************************/ 
  49. BOOL xxx_Deinit(DWORD dwData) 
  50. {     
  51.     //.. 
  52.     //return FALSE; 
  53. /*************************************************************************** 
  54. * 
  55. * Function Name:xxx_IOControl 
  56. * Purpose: This function is used to send a control code to a service. 
  57. * Input: dwData:  Specifies the value returned by xxx_Init
  58.  (Services.exe) for the given service instance. 
  59. dwCode:Specifies the control code for the operation. 
  60. pBufIn:Pointer to a buffer that contains the data
  61. required to perform the operation. 
  62. dwLenIn:Specifies the size, in bytes, of the buffer pointed to by pBufIn. 
  63. dwLenOut:Specifies the size, in bytes, of the buffer pointed to by pBufOut. 
  64. * Output: pBufOut:  Pointer to a buffer that receives the output
  65.  data from the operation. 
  66. pdwActualOut:Pointer to a variable that receives the size, in bytes
  67. , of the data stored into the buffer pointed to by pBufOut. 
  68. * Return:TRUE indicates success. FALSE indicates failure.
  69. * Remarks:The control code specifies the action that the
  70.  driver is to perform. For example, a control code can ask a service
  71. to return information or direct the service to carry out an action.
  72.  Windows Embedded CE. NET provides a number of
  73. standard control codes. In addition, a service can define
  74.  its own service-specific control code. 
  75. ***************************************************************************/ 
  76. BOOL xxx_IOControl( 
  77. DWORD dwData, 
  78. DWORD dwCode, 
  79. PBYTE pBufIn, 
  80. DWORD dwLenIn, 
  81. PBYTE pBufOut, 
  82. DWORD dwLenOut, 
  83. PDWORD pdwActualOut) 
  84.     //.. 
  85.     //return TRUE; 
  86. /*************************************************************************** 
  87. * 
  88. * Function Name:xxx_Open 
  89. * Purpose: This function is to be implemented by a service and
  90.  will be called by Services.exe. 
  91. * Input: 
  92. dwData:Specifies the value returned by xxx_Init (Services.exe)
  93.  for the given service instance. 
  94. dwAccess :Specifies the type of access to the object. 
  95. dwShareMode:Specifies how the object can be shared. 
  96. * Output:none 
  97. * Return:TRUE indicates success. FALSE indicates failure.
  98. * Remarks: This function is called during an application's
  99.  call to CreateFile. The values for the dwAccess and dwShareMode
  100.  parameters are passed directly from the call to CreateFile. 
  101. ***************************************************************************/ 
  102. BOOL xxx_Open( 
  103. DWORD dwData, 
  104. DWORD dwAccess, 
  105. DWORD dwShareMode) 
  106. {     
  107.     //.. 
  108.     //return FALSE; 
  109. /*************************************************************************** 
  110. * 
  111. * Function Name:xxx_Read 
  112. * Purpose: This function is to be implemented by a service
  113.  and will be called by Services.exe. This function need
  114.  only be implemented by a streaming service. 
  115. * Input: 
  116. dwData:Specifies the value returned by xxx_Open (Services.exe)
  117.  for the given service instance. 
  118. dwLen:Specifies the number of bytes to be read. 
  119. * Output: 
  120. pBuf:Pointer to the storage location for the data that is read. 
  121. * Return:Returns the number of bytes read.
  122. * Remarks: This function is called by Services.exe
  123.  as a result of an application's call to ReadFile. 
  124. ***************************************************************************/ 
  125. DWORD xxx_Read( 
  126. DWORD dwData, 
  127. LPVOID pBuf, 
  128. DWORD dwLen) 
  129.     //.. 
  130.     //return 0; 
  131. /***************************************************************************
  132. * Function Name:xxx_Seek 
  133. * Purpose: This function is to be implemented by a service and will
  134.  be called by Services.exe.
  135. This function need only be implemented by a streaming service. 
  136. * Input: 
  137. dwData:Specifies the value returned by xxx_Open (Services.exe)
  138.  for the given service instance. 
  139. pos:Specifies the number of bytes to move the file pointer.
  140.  pos is a 32-bit signed value. 
  141. type:Specifies the starting point for the file pointer move. 
  142. * Output:none 
  143. * Return:Returns the current location of the file pointer.
  144. * Remarks: This function is called by Services.exe as a
  145.  result of an application's call to SetFilePointer. 
  146. ***************************************************************************/ 
  147. DWORD xxx_Seek( 
  148. DWORD dwData, 
  149. long pos, 
  150. DWORD type) 
  151.     //.. 
  152.     //return 0; 
  153. /*************************************************************************** 
  154. * 
  155. * Function Name:xxx_Write 
  156. * Purpose: This function is to be implemented by a service and will
  157.  be called by Services.exe. 
  158. Only streaming services need to implement this function. 
  159. * Input: 
  160. dwData:Specifies the value returned by xxx_Open (Services.exe)
  161.  for the given service instance. 
  162. dwInLen:Specifies the length of data in the buffer to be written. 
  163. * Output: 
  164. pInBuf: Pointer to the buffer containing data to write. 
  165. * Return:Returns the number of bytes actually written.
  166. * Remark: This function is called by Services.exe as a
  167.  result of an application's call to WriteFile. 
  168. ***************************************************************************/ 
  169. DWORD xxx_Write( 
  170. DWORD dwData, 
  171. LPCVOID pInBuf, 
  172. DWORD dwInLen) 
  173. {     
  174.     //.. 
  175.     //return 0; 
  176. /*************************************************************************** 
  177. * 
  178. * Function Name:xxx_Init 
  179. * Purpose:This function is to be implemented by a service and
  180.  will be called by Services.exe. 
  181. * Input: 
  182. dwData:  Specifies the service-supplied data. 
  183. * Output:none 
  184. * Return:Returns a value to be used in calls to xxx_Open (Services.exe).
  185. * Remarks: This function is called during RegisterService, in which
  186.  case dwData will be the fourth parameter to RegisterService.
  187.  It is also called during Services.exe initialization,
  188.  in which case dwData is the DWORD value set in the registry value
  189.  HKEY_LOCAL_MACHINE\Services\Service\Context,
  190.  or zero if this value is not set. 
  191. ***************************************************************************/ 
  192. DWORD xxx_Init(DWORD dwData) 
  193.     //.. 
  194.     //return 1; 

第三步,添加注册表:

当系统启动的时候,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/
版权声明: 本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任的权利。
您可以从这里更方便的找到我的文章。
(王克伟)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201103/11023.html]
本文出处:wangkewei.cnblogs.com 作者:王克伟
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容