读书人

一个服务程序在xp正常运行在2003启

发布时间: 2012-10-16 09:57:37 作者: rapoo

一个服务程序,在xp正常运行,在2003启动报1053错误
一个服务程序,在中文xp正常运行,在英文2003系统启动报1053错误

启动部分代码如下

SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;

////////////////////////////////////////////////////////////
// Make the forward definitions of functions prototypes.
//
////////////////////////////////////////////////////////////
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();
extern int goipmain();

extern void getcfg(char *file);

int WriteToLog(char* str)
{
FILE* log;
log = fopen(logfile, "a+");
if (log == NULL){
OutputDebugString("Log file open failed.");
return -1;
}
time_t mytime=time(0);
fprintf(log, "%s %s", str,ctime(&mytime));
fclose(log);
return 0;
}

// Service initialization
int InitService()
{
OutputDebugString("Monitoring started.");
int result;
result = WriteToLog("Monitoring started.");
return(result);
}

// Control Handler
void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
OutputDebugString("Monitoring stopped.");
WriteToLog("Monitoring stopped.");

ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;

case SERVICE_CONTROL_SHUTDOWN:
OutputDebugString("Monitoring stopped.");
WriteToLog("Monitoring stopped.");

ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;

default:
break;
}

// Report current status
SetServiceStatus (hStatus, &ServiceStatus);

return;
}

void ServiceMain(int argc, char** argv)
{
int error;

ServiceStatus.dwServiceType =
SERVICE_WIN32;
ServiceStatus.dwCurrentState =
SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;

hStatus = RegisterServiceCtrlHandler(
"MemoryStatus",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}

// Initialize Service
error = InitService();
if (error)
{
// Initialization failed
ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// We report the running status to SCM.


ServiceStatus.dwCurrentState =
SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);

MEMORYSTATUS memory;
WriteToLog("Monitoring ++++.");
// The worker loop of a service
while (ServiceStatus.dwCurrentState ==
SERVICE_RUNNING)
{
char buffer[16];
GlobalMemoryStatus(&memory);
//sprintf(buffer, "%d", memory.dwAvailPhys);
//system("httpd.exe -n \"Apache2.2\"");

OutputDebugString(buffer);
//int result = WriteToLog(buffer);
//if (result)
//{
real_main();
ServiceStatus.dwCurrentState =
SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus,
&ServiceStatus);
return;
//}
Sleep(SLEEP_TIME);
}
return;
}

int main(int argc, char* argv[])
{
if(argc>1)
logfile=strdup(argv[1]);
if(argc>2)
getcfg(argv[2]);
else
getcfg(NULL);
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = "goipcron";
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
// Start the control dispatcher thread for our service
StartServiceCtrlDispatcher(ServiceTable);
return 0;
}

[解决办法]
http://blog.csdn.net/wadefelix/archive/2008/01/21/2056967.aspx
试试这个帖子的方法是否可行。
[解决办法]
我以前遇到过这个错误,一般的应用程序被设置为服务程序后,由于它不能够与SCM进行通信,所以SCM无法将其启动。错误提示如下:(下面wcdj是我的服务名字)
本地计算机无法启动wcdj服务
错误1053:服务没有及时响应启动或控制请求
参考
[解决办法]
http://zh-cn.w3support.net/index.php?db=so&id=793731

读书人网 >windows

热点推荐