我写的程序怎么创建成服务?
看过几个代码 好像都不行
给个地址让俺看看?
8 个解决方案
#1
查 Service 的相关资料,从网上随便给你找了篇:
http://blog.mynice.cn/user1/23/archives/2006/41152.shtml
http://blog.mynice.cn/user1/23/archives/2006/41152.shtml
#2
main()函数这样写:
ServiceMain函数:
SERVICE_TABLE_ENTRY ste[] =
{ {"servicename",(LPSERVICE_MAIN_FUNCTION)ServiceMain}, {NULL,NULL} };
if (StartServiceCtrlDispatcher(ste)==0)
return (int)GetLastError();
return 0;
ServiceMain函数:
SERVICE_STATUS ss;
SERVICE_STATUS_HANDLE ssh;
void ServiceMain(DWORD dwArgc, LPSTR* lpszArgv)
{
ssh = RegisterServiceCtrlHandlerEx("servicename",(LPHANDLER_FUNCTION_EX)ServiceCtrl,NULL);
SetStatus(SERVICE_START_PENDING);
// 本地初始化
SetStatus(SERVICE_RUNNING);
// 执行
// 这里是进入运行状态的代码
// 退出,执行清理
SetStatus(SERVICE_STOP_PENDING);
// 这里是清理代码
SetStatus(SERVICE_STOPPED);
}
DWORD ServiceCtrl( DWORD dwCtrlCode, DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext )
{
switch(dwCtrlCode)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
SetStatus(SERVICE_STOP_PENDING);
return NO_ERROR;
break;
default:
SetStatus(SERVICE_RUNNING);
return NO_ERROR;
}
}
void SetStatus(DWORD dwStatus, DWORD dwExitCode/* =NO_ERROR */, DWORD dwCheckPoint/* =0 */, DWORD dwWaitHint/* =0 */)
{
if (dwStatus==SERVICE_START_PENDING)
ss.dwControlsAccepted = 0;
else
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ss.dwServiceSpecificExitCode = 0;
ss.dwCurrentState = dwStatus;
ss.dwWin32ExitCode = dwExitCode;
ss.dwCheckPoint = dwCheckPoint;
ss.dwWaitHint = dwWaitHint;
if (!SetServiceStatus(ssh, &ss))
SetStatus(SERVICE_STOPPED, GetLastError());
}
#4
这本书是这方面的权威书籍:
Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000
#6
我搞定啦 谢谢 各位
怎么给分呀?
怎么给分呀?
#7
这么快,还准备蹭点分的
#8
mark,学习^_^
#1
查 Service 的相关资料,从网上随便给你找了篇:
http://blog.mynice.cn/user1/23/archives/2006/41152.shtml
http://blog.mynice.cn/user1/23/archives/2006/41152.shtml
#2
main()函数这样写:
ServiceMain函数:
SERVICE_TABLE_ENTRY ste[] =
{ {"servicename",(LPSERVICE_MAIN_FUNCTION)ServiceMain}, {NULL,NULL} };
if (StartServiceCtrlDispatcher(ste)==0)
return (int)GetLastError();
return 0;
ServiceMain函数:
SERVICE_STATUS ss;
SERVICE_STATUS_HANDLE ssh;
void ServiceMain(DWORD dwArgc, LPSTR* lpszArgv)
{
ssh = RegisterServiceCtrlHandlerEx("servicename",(LPHANDLER_FUNCTION_EX)ServiceCtrl,NULL);
SetStatus(SERVICE_START_PENDING);
// 本地初始化
SetStatus(SERVICE_RUNNING);
// 执行
// 这里是进入运行状态的代码
// 退出,执行清理
SetStatus(SERVICE_STOP_PENDING);
// 这里是清理代码
SetStatus(SERVICE_STOPPED);
}
DWORD ServiceCtrl( DWORD dwCtrlCode, DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext )
{
switch(dwCtrlCode)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
SetStatus(SERVICE_STOP_PENDING);
return NO_ERROR;
break;
default:
SetStatus(SERVICE_RUNNING);
return NO_ERROR;
}
}
void SetStatus(DWORD dwStatus, DWORD dwExitCode/* =NO_ERROR */, DWORD dwCheckPoint/* =0 */, DWORD dwWaitHint/* =0 */)
{
if (dwStatus==SERVICE_START_PENDING)
ss.dwControlsAccepted = 0;
else
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ss.dwServiceSpecificExitCode = 0;
ss.dwCurrentState = dwStatus;
ss.dwWin32ExitCode = dwExitCode;
ss.dwCheckPoint = dwCheckPoint;
ss.dwWaitHint = dwWaitHint;
if (!SetServiceStatus(ssh, &ss))
SetStatus(SERVICE_STOPPED, GetLastError());
}
#3
#4
这本书是这方面的权威书籍:
Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000
#5
看看具体的步骤:
编写Windows服务程序的五个步骤
编写Windows服务程序的五个步骤
#6
我搞定啦 谢谢 各位
怎么给分呀?
怎么给分呀?
#7
这么快,还准备蹭点分的
#8
mark,学习^_^