#include<stdio.h>
#include<windows.h>
SERVICE_STATUSm_ServiceStatus;
SERVICE_STATUS_HANDLEm_ServiceStatusHandle;
BOOLbRunning=true;
voidWINAPIServiceMain(DWORDargc,LPTSTR*argv);//服务主函数
voidWINAPIServiceCtrlHandler(DWORDOpcode);//服务控制函数
voidWINAPICmdStart(void);//要启动的程序函数
BOOLInstallService();//安装服务的函数
BOOLDeleteService();//删除服务的函数
intmain(intargc,char*argv[])
{
printf(" windowsbasedservicedemo ");
printf(" gxisone@hotmail.com ");
if(argc!=3)
{
printf("usage:%s-install[remove]",argv[0]);
return0;
}
if(strcmp(argv[1],"-install")==0)///install
{
if(InstallService())
printf(" ServiceInstalledSucessfully ");
else
printf(" ErrorInstallingService ");
}
elseif(strcmp(argv[1],"-remove")==0)//remove
{
if(DeleteService())
printf(" Serviceremovesucessfully ");
else
printf(" ErrorremovingService ");
}
else
{
printf(" usage:%s-install[remove] ",argv[0]);
return0;
}
//在进入点函数里面要完成ServiceMain的初始化,
//准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,
//这个结构记录了这个服务程序里面所包含的所有服务的名称
//和服务的进入点函数
SERVICE_TABLE_ENTRY
DispatchTable[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};
//最后的NULL指明数组的结束
StartServiceCtrlDispatcher(DispatchTable);
return0;
}
voidWINAPIServiceMain(DWORDargc,LPTSTR*argv)
{
m_ServiceStatus.dwServiceType=SERVICE_WIN32;
m_ServiceStatus.dwCurrentState=SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwServiceSpecificExitCode=0;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
m_ServiceStatusHandle=RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);
if(m_ServiceStatusHandle==(SERVICE_STATUS_HANDLE)0)return;
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;//设置服务状态
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
//SERVICE_STATUS结构含有七个成员,它们反映服务的现行状态。
//所有这些成员必须在这个结构被传递到SetServiceStatus之前正确的设置
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=true;
//*
CmdStart();//启动我们的服务程序
//*
return;
}
voidWINAPIServiceCtrlHandler(DWORDOpcode)//服务控制函数
{
switch(Opcode)
{
caseSERVICE_CONTROL_PAUSE://weacceptthecommandtopauseit
m_ServiceStatus.dwCurrentState=SERVICE_PAUSED;
break;
caseSERVICE_CONTROL_CONTINUE://wegotthecommandtocontinue
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
break;
caseSERVICE_CONTROL_STOP://wegotthecommandtostopthisservice
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwCurrentState=SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
caseSERVICE_CONTROL_INTERROGATE://
break;
}
return;
}
BOOLInstallService()//安装服务函数
{
charstrDir[1024];
SC_HANDLEschSCManager,schService;
GetCurrentDirectory(1024,strDir);
GetModuleFileName(NULL,strDir,sizeof(strDir));
charchSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\WindowsMgr.exe");
if(!CopyFile(strDir,chSysPath,FALSE))printf("CopyfileOK ");//把我们的服务程序复制到系统根目录
strcpy(strDir,chSysPath);
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("openscmangerfailed,maybeyoudonothavetheprivilagetodothis ");
returnfalse;
}
LPCTSTRlpszBinaryPathName=strDir;
schService=CreateService(schSCManager,"WindowsMgr","WindowsMangerControl",//将服务的信息添加到SCM的数据库
SERVICE_ALL_ACCESS,//desiredaccess
SERVICE_WIN32_OWN_PROCESS,//servicetype
SERVICE_AUTO_START,//starttype
SERVICE_ERROR_NORMAL,//errorcontroltype
lpszBinaryPathName,//service'sbinary
NULL,//noloadorderinggroup
NULL,//notagidentifier
NULL,//nodependencies
NULL,//LocalSystemaccount
NULL);//nopassword
if(schService==NULL)
{
printf("faint,wefailedjustbecauseweinvokecreateservicesfailed ");
returnfalse;
}
CloseServiceHandle(schService);
returntrue;
}
BOOLDeleteService()
{
SC_HANDLEschSCManager;
SC_HANDLEhService;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
charchSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\WindowsMgr.exe");
if(schSCManager==NULL)
{
printf("faint,openscmangerfailed ");
returnfalse;
}
hService=OpenService(schSCManager,"WindowsMgr",SERVICE_ALL_ACCESS);
if(hService==NULL)
{
printf("faint,openservicesfailt ");
returnfalse;
}
if(DeleteFile(chSysPath)==0)
{
printf("DellfileFailure! ");
returnfalse;
}
elseprintf("DeletefileOK! ");
if(DeleteService(hService)==0)
returnfalse;
if(CloseServiceHandle(hService)==0)
returnfalse;
else
returntrue;
}
voidWINAPICmdStart(void)
{
//--------------------------------
//把你的要做成服务启动的程序代码添加到这里
//那么你的代码就可以作为NT服务启动了
//--------------------------------
}
#include<windows.h>
SERVICE_STATUSm_ServiceStatus;
SERVICE_STATUS_HANDLEm_ServiceStatusHandle;
BOOLbRunning=true;
voidWINAPIServiceMain(DWORDargc,LPTSTR*argv);//服务主函数
voidWINAPIServiceCtrlHandler(DWORDOpcode);//服务控制函数
voidWINAPICmdStart(void);//要启动的程序函数
BOOLInstallService();//安装服务的函数
BOOLDeleteService();//删除服务的函数
intmain(intargc,char*argv[])
{
printf(" windowsbasedservicedemo ");
printf(" gxisone@hotmail.com ");
if(argc!=3)
{
printf("usage:%s-install[remove]",argv[0]);
return0;
}
if(strcmp(argv[1],"-install")==0)///install
{
if(InstallService())
printf(" ServiceInstalledSucessfully ");
else
printf(" ErrorInstallingService ");
}
elseif(strcmp(argv[1],"-remove")==0)//remove
{
if(DeleteService())
printf(" Serviceremovesucessfully ");
else
printf(" ErrorremovingService ");
}
else
{
printf(" usage:%s-install[remove] ",argv[0]);
return0;
}
//在进入点函数里面要完成ServiceMain的初始化,
//准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,
//这个结构记录了这个服务程序里面所包含的所有服务的名称
//和服务的进入点函数
SERVICE_TABLE_ENTRY
DispatchTable[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};
//最后的NULL指明数组的结束
StartServiceCtrlDispatcher(DispatchTable);
return0;
}
voidWINAPIServiceMain(DWORDargc,LPTSTR*argv)
{
m_ServiceStatus.dwServiceType=SERVICE_WIN32;
m_ServiceStatus.dwCurrentState=SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwServiceSpecificExitCode=0;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
m_ServiceStatusHandle=RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);
if(m_ServiceStatusHandle==(SERVICE_STATUS_HANDLE)0)return;
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;//设置服务状态
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
//SERVICE_STATUS结构含有七个成员,它们反映服务的现行状态。
//所有这些成员必须在这个结构被传递到SetServiceStatus之前正确的设置
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=true;
//*
CmdStart();//启动我们的服务程序
//*
return;
}
voidWINAPIServiceCtrlHandler(DWORDOpcode)//服务控制函数
{
switch(Opcode)
{
caseSERVICE_CONTROL_PAUSE://weacceptthecommandtopauseit
m_ServiceStatus.dwCurrentState=SERVICE_PAUSED;
break;
caseSERVICE_CONTROL_CONTINUE://wegotthecommandtocontinue
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
break;
caseSERVICE_CONTROL_STOP://wegotthecommandtostopthisservice
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwCurrentState=SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
caseSERVICE_CONTROL_INTERROGATE://
break;
}
return;
}
BOOLInstallService()//安装服务函数
{
charstrDir[1024];
SC_HANDLEschSCManager,schService;
GetCurrentDirectory(1024,strDir);
GetModuleFileName(NULL,strDir,sizeof(strDir));
charchSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\WindowsMgr.exe");
if(!CopyFile(strDir,chSysPath,FALSE))printf("CopyfileOK ");//把我们的服务程序复制到系统根目录
strcpy(strDir,chSysPath);
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("openscmangerfailed,maybeyoudonothavetheprivilagetodothis ");
returnfalse;
}
LPCTSTRlpszBinaryPathName=strDir;
schService=CreateService(schSCManager,"WindowsMgr","WindowsMangerControl",//将服务的信息添加到SCM的数据库
SERVICE_ALL_ACCESS,//desiredaccess
SERVICE_WIN32_OWN_PROCESS,//servicetype
SERVICE_AUTO_START,//starttype
SERVICE_ERROR_NORMAL,//errorcontroltype
lpszBinaryPathName,//service'sbinary
NULL,//noloadorderinggroup
NULL,//notagidentifier
NULL,//nodependencies
NULL,//LocalSystemaccount
NULL);//nopassword
if(schService==NULL)
{
printf("faint,wefailedjustbecauseweinvokecreateservicesfailed ");
returnfalse;
}
CloseServiceHandle(schService);
returntrue;
}
BOOLDeleteService()
{
SC_HANDLEschSCManager;
SC_HANDLEhService;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
charchSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\WindowsMgr.exe");
if(schSCManager==NULL)
{
printf("faint,openscmangerfailed ");
returnfalse;
}
hService=OpenService(schSCManager,"WindowsMgr",SERVICE_ALL_ACCESS);
if(hService==NULL)
{
printf("faint,openservicesfailt ");
returnfalse;
}
if(DeleteFile(chSysPath)==0)
{
printf("DellfileFailure! ");
returnfalse;
}
elseprintf("DeletefileOK! ");
if(DeleteService(hService)==0)
returnfalse;
if(CloseServiceHandle(hService)==0)
returnfalse;
else
returntrue;
}
voidWINAPICmdStart(void)
{
//--------------------------------
//把你的要做成服务启动的程序代码添加到这里
//那么你的代码就可以作为NT服务启动了
//--------------------------------
}
本文提供了一个Windows服务编程的完整示例,包括服务的安装、启动、停止及卸载等核心功能实现。通过C语言展示了如何创建一个能够在Windows环境下运行的服务程序。
9528

被折叠的 条评论
为什么被折叠?



