服务简介
几乎每一个操作系统都有一种在系统启动时刻启动进程的机制,这些进程提供了一些不依赖于任何用户交互式的服务。在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服务程序在系统架构中的位置,微软把驱动分为用户模式和内核模式:
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. “
"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即是注册表里指定的前缀:
01 | EXPORTS |
02 | ; Explicit exports can go here |
03 | xxx_Close |
04 | xxx_Deinit |
05 | xxx_Init |
06 | xxx_IOControl |
07 | xxx_Open |
08 | xxx_Read |
09 | xxx_Seek |
10 | xxx_Write |
第二步,实现函数,xxx即是注册表里指定的前缀:
001 | /*************************************************************************** |
002 | * |
003 | * Function Name: DllMain |
004 | * Purpose: service entrance |
005 | * Input: |
006 | hinstDLL: Handle to the DLL. |
007 | dwReason: Specifies a flag indicating why the DLL entry-point function is being called. |
008 | lpvReserved: Specifies further aspects of DLL initialization and cleanup. |
009 | * Output:none |
010 | * Return:TRUE |
011 | ***************************************************************************/ |
012 | BOOL APIENTRY DllMain( HANDLE hinstDLL, DWORD dwReason, LPVOID lpvReserved) |
013 | { |
014 | switch ( dwReason ) |
015 | { |
016 | case DLL_PROCESS_ATTACH: |
017 | g_hInst=( HINSTANCE )hinstDLL; |
018 | break ; |
019 | case DLL_PROCESS_DETACH: |
020 | { |
021 | //.. |
022 | break ; |
023 | } |
024 | } |
025 | return 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: |
032 | dwData: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 | ***************************************************************************/ |
036 | BOOL 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: |
046 | dwData: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 | ***************************************************************************/ |
050 | BOOL 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: |
060 | dwData: Specifies the value returned by xxx_Init (Services.exe) for the given service instance. |
061 | dwCode: Specifies the control code for the operation. |
062 | pBufIn: Pointer to a buffer that contains the data required to perform the operation. |
063 | dwLenIn: Specifies the size, in bytes, of the buffer pointed to by pBufIn. |
064 | dwLenOut: Specifies the size, in bytes, of the buffer pointed to by pBufOut. |
065 | * Output: |
066 | pBufOut: Pointer to a buffer that receives the output data from the operation. |
067 | pdwActualOut: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 | ***************************************************************************/ |
070 | BOOL xxx_IOControl( |
071 | DWORD dwData, |
072 | DWORD dwCode, |
073 | PBYTE pBufIn, |
074 | DWORD dwLenIn, |
075 | PBYTE pBufOut, |
076 | DWORD dwLenOut, |
077 | PDWORD 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: |
087 | dwData:Specifies the value returned by xxx_Init (Services.exe) for the given service instance. |
088 | dwAccess :Specifies the type of access to the object. |
089 | dwShareMode: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 | ***************************************************************************/ |
093 | BOOL xxx_Open( |
094 | DWORD dwData, |
095 | DWORD dwAccess, |
096 | DWORD 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: |
106 | dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance. |
107 | dwLen:Specifies the number of bytes to be read. |
108 | * Output: |
109 | pBuf: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 | ***************************************************************************/ |
112 | DWORD xxx_Read( |
113 | DWORD dwData, |
114 | LPVOID pBuf, |
115 | DWORD 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: |
125 | dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance. |
126 | pos:Specifies the number of bytes to move the file pointer. pos is a 32-bit signed value. |
127 | type: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 | ***************************************************************************/ |
131 | DWORD xxx_Seek( |
132 | DWORD dwData, |
133 | long pos, |
134 | DWORD 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. |
143 | Only streaming services need to implement this function. |
144 | * Input: |
145 | dwData:Specifies the value returned by xxx_Open (Services.exe) for the given service instance. |
146 | dwInLen:Specifies the length of data in the buffer to be written. |
147 | * Output: |
148 | pInBuf: 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 | ***************************************************************************/ |
151 | DWORD xxx_Write( |
152 | DWORD dwData, |
153 | LPCVOID pInBuf, |
154 | DWORD 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: |
164 | dwData: 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 | ***************************************************************************/ |
168 | DWORD 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不会被加载的,这也服务运行失败的常见原因。
签名工具在此。
2009.6.22更新:虽然.Net CF没有提供任何接口创建Windows Services,但是使用托管代码开发朋友可以参考这篇文章使用C#等语言创建服务。
出处: http://wangkewei.cnblogs.com/
版权声明: 本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任的权利。
您可以从 这里更方便的找到我的文章。