我正在Windows CE 6.0下开发Windows服务。该项目生成一个DLL,它集成在OS Image中。由于注册表设置,WinCE启动时服务启动。
问题是我无法启动“Thread1”线程。我应该看到MessageBox,但屏幕上没有任何内容。为什么?将MessageBox放入SFC_Init可以正常工作。
另一件事 - 当我在控制台中输入“服务列表”时(在WinCE系统中),我的服务状态是未知的......为什么会这样?
请帮忙!
// SrvForCom.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
HINSTANCE hInst;
// main entry point of the DLL
BOOL WINAPI DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hInst = (HINSTANCE)hModule;
switch(ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls((HMODULE)hModule);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// function called during initialization process
DWORD SFC_Init(DWORD dwContext) {
PSRVCONTEXT pSrv;
HANDLE hThrd;
DWORD err = ERROR_INVALID_PARAMETER;
// fill the info structure
pSrv->servState = SERVICE_STATE_UNKNOWN;
switch (dwContext) {
case SERVICE_INIT_STARTED:
pSrv->servState = SERVICE_STATE_ON;
break;
case SERVICE_INIT_STOPPED:
pSrv->servState = SERVICE_STATE_OFF;
break;
}
// start new thread
hThrd = CreateThread (NULL, 0, Thread1, NULL, 0, NULL);
if (hThrd) {
CloseHandle (hThrd);
err = 0;
} else {
err = GetLastError();
}
return hThrd;
}
BOOL SFC_Deinit(DWORD dwData) {
return TRUE;
}
BOOL SFC_Open(DWORD dwData, DWORD dwAccess, DWORD dwShareMode) {
PSRVCONTEXT pSrv = (PSRVCONTEXT)dwData;
return (DWORD)pSrv;
}
BOOL SFC_Close(DWORD dwData) {
return 1;
}
BOOL SFC_IOControl(DWORD dwData, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut) {
PSRVCONTEXT pSrv = (PSRVCONTEXT)dwData;
switch (dwCode) {
case IOCTL_SERVICE_STATUS:
*pBufOut = pSrv->servState;
break;
}
return 1;
}
DWORD WINAPI Thread1(LPVOID lpv) {
MessageBox (NULL, TEXT ("The thread has been successfully started!"), TEXT ("Info"), MB_OK);
return 0;
}
答案 0 :(得分:0)
我找到了这个问题的答案。除了一个细节之外,上面的代码是正确的 - 未初始化的结构。 “pSrv”是带有“servState”字段的结构SRVCONTEXT的指针(类型PSRVCONTEXT)。执行“pSrv-> servState = SERVICE_STATE_UNKNOWN;”时memmory的某些部分被覆盖,导致应用程序出错。
解决方案是在使用之前首先将memmory分配给此结构:
pSrv = (PSRVCONTEXT)LocalAlloc (LPTR, sizeof (SRVCONTEXT));
将以上行添加到我的应用程序后,一切正常。线程正确启动,服务状态更改为“正在运行”。
P.S。结构如下所示:
typedef struct {
DWORD servState;
} SRVCONTEXT, *PSRVCONTEXT;