wince服务与windows pc服务有区别,是将服务写成dll的形式,在注册表中配置项,然后系统自启动时加载.
下面是示例
1. 编写dll文件,实现几个方法
//写日志组件
CSTLogFile* Logger = CSTLogFile::GetLogFile();
// Handle to the worker thread
HANDLE g_hWorkerThread = NULL;
bool m_bRunning = false;
DWORD m_nThreadId = 0;
extern "C" BOOL WINAPI DllEntry(HANDLE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//服务启动时要做的事,示例中在这里启动写文件线程
extern "C" LIBAPI DWORD FIN_Init(DWORD dwData)
{
//初始化
m_bRunning = true;
g_hWorkerThread = CreateThread(NULL,0,FingerWorker,NULL,0,(LPDWORD)&m_nThreadId);
return 1;
}
//停止服务时要做的事情
extern "C" LIBAPI BOOL FIN_Deinit(DWORD dwData)
{
//停止线程
m_bRunning = false;
WaitForSingleObject(g_hWorkerThread,INFINITE);
CloseHandle(g_hWorkerThread);
g_hWorkerThread = NULL;
return 1;
}
extern "C" LIBAPI DWORD FIN_Open (DWORD dwData, DWORD dwAccess,
DWORD dwShareMode)
{
return 1;
}
extern "C" LIBAPI BOOL FIN_Close (DWORD dwData)
{
return TRUE;
}
extern "C" LIBAPI BOOL FIN_IOControl(DWORD dwData, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut)
{
switch (dwCode)
{
// Control code sent to start a service. (services start FIN0:)
case IOCTL_SERVICE_START:
//Logger->Write("IOCTL_SERVICE_START--------\n");
break;
// Control code sent to refresh a service. (services refresh FIN0:)
case IOCTL_SERVICE_REFRESH:
break;
// Control code sent to stop a service. (services stop FIN0:)
case IOCTL_SERVICE_STOP:
//Logger->Write("IOCTL_SERVICE_STOP--------\n");
break;
// An application (possibly services.exe itself) is
// querying for the service's running state.
case IOCTL_SERVICE_STATUS:
break;
default:
;
break;
}
return TRUE;
}
extern "C" LIBAPI DWORD FIN_Read(DWORD dwData,LPVOID pBuff, DWORD dwLen)
{
return 0;
}
extern "C" LIBAPI DWORD FIN_Seek(DWORD dwData,LONG pos,DWORD type)
{
return 0;
}
extern "C" LIBAPI DWORD FIN_Write(DWORD dwData,LPCVOID pInBuf,DWORD dwInLen)
{
return 0;
}
//用于向文件写日志的方式测试
extern "C" DWORD WINAPI FingerWorker(LPVOID lpv)
{
while(m_bRunning)
{
Logger->Write("Test Logs------------\n");
Sleep(5000);
}
return 0;
}
2. 填加注册表项
2.1
void CRegEditDlg::OnBnClickedButtonWrite()
{
HKEY RootKey = HKEY_LOCAL_MACHINE;
HKEY hKey;
//打开主键,没有则创建
CString strSubKey = _T("\\Services\\FinServ");
DWORD dw;
long nRet = RegCreateKeyEx(RootKey,strSubKey,0,REG_NONE,REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,&hKey,&dw);
if(nRet != ERROR_SUCCESS)
{
return;
}
int tsize = sizeof(TCHAR);
//设置键值
LPCTSTR buff = _T("\\Flash Disk\\TestService\\WinCEService.dll");
int length = _tcslen(buff)*tsize;
nRet = RegSetValueEx(hKey,_T("Dll"),0,REG_SZ,(LPBYTE)buff,length);
if(nRet != ERROR_SUCCESS)
{
return;
}
dw = 8;
RegSetValueEx(hKey,_T("Order"),0,REG_DWORD,(LPBYTE)&dw,sizeof(DWORD));
if(nRet != ERROR_SUCCESS)
{
return;
}
dw = 1;
RegSetValueEx(hKey,_T("Keep"),0,REG_DWORD,(LPBYTE)&dw,sizeof(DWORD));
if(nRet != ERROR_SUCCESS)
{
return;
}
buff = _T("FIN");
length = _tcslen(buff)*tsize;
RegSetValueEx(hKey,_T("Prefix"),0,REG_SZ,(LPBYTE)buff,length);
if(nRet != ERROR_SUCCESS)
{
return;
}
dw = 0;
RegSetValueEx(hKey,_T("Index"),0,REG_DWORD,(LPBYTE)&dw,sizeof(DWORD));
if(nRet != ERROR_SUCCESS)
{
return;
}
dw = 0;
RegSetValueEx(hKey,_T("Context"),0,REG_DWORD,(LPBYTE)&dw,sizeof(DWORD));
if(nRet != ERROR_SUCCESS)
{
return;
}
buff = _T("wince service");
length = _tcslen(buff)*tsize;
RegSetValueEx(hKey,_T("DisplayName"),0,REG_SZ,(LPBYTE)buff,length);
if(nRet != ERROR_SUCCESS)
{
return;
}
buff = _T("Sample Wince Service");
length = _tcslen(buff)*tsize;
RegSetValueEx(hKey,_T("Description"),0,REG_SZ,(LPBYTE)buff,length);
if(nRet != ERROR_SUCCESS)
{
return;
}
}
2.2 删除注册表项
void CRegEditDlg::OnBnClickedButtonDel()
{
HKEY RootKey = HKEY_LOCAL_MACHINE;
CString strSubKey = _T("\\Services\\FinServ");
//找到该键,删除
LONG nRet = RegDeleteKey(RootKey,strSubKey);
if(nRet != ERROR_SUCCESS)
{
MessageBox(_T("删除注册表失败!"));
}
else
{
MessageBox(_T("删除注册表成功!"));
}
}
3. 启动和中止服务(这里是手动停止和中止服务,设置好注册表项和文件后位置后,系统启动时可以自动加载)
3.1 启动服务
HANDLE hService = RegisterService(_T("FIN"),0,_T("\\Flash Disk\\TestService\\WinCEService.dll"),NULL);
if(hService == NULL)
{
CString strMsg;
strMsg.Format(_T("GetLasterror: %d"), GetLastError());
AfxMessageBox(strMsg);
return;
}
注意:注意服务文件的路径,如果在Windows目录可以不指定前面的路径.
3.2 停止服务
void CRegEditDlg::OnBnClickedButtonStop()
{
//停止服务
HANDLE hService = GetServiceHandle(_T("FIN0:"),NULL,NULL);
if(hService !=INVALID_HANDLE_VALUE)
{
DeregisterService(hService);
}
else
{
CString strMsg;
strMsg.Format(_T("GetLasterror: %d"), GetLastError());
AfxMessageBox(strMsg);
}
}