主题:[原创]exe做成服务启动的问题??
[em1][em1][em1][em1][em1][em1][em1][em1]
exe做成服务启动的问题??
最近写了一个服务程序,服务可以安装,但是(自动/手动)启动不了,有没有高手啊?我把代码贴出来,请高手来指点(启动是系统提示1053错误)
#include<stdio.h>
#include<windows.h>
#include <winsvc.h>
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ZServiceMain(DWORD argc,LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
void msg();
BOOL installService();
BOOL DeleteService();
int main(int argc,char *argv[])
{
int t;
printf("\twindows based service demo\n");
printf("tgxisone@hotamil.com\n");
if(argc!=3)
{
printf("usage:%s -install[remove]",argv[0]);
}
t=installService();
if(t)
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nERROR installing Service\n");
SERVICE_TABLE_ENTRY DispatchTable[2];
DispatchTable[0].lpServiceName="WindowsMgr";//Service Name
DispatchTable[0].lpServiceProc=(LPSERVICE_MAIN_FUNCTION)ZServiceMain;
DispatchTable[1].lpServiceName=NULL;
DispatchTable[1].lpServiceProc=NULL;
if(!StartServiceCtrlDispatcher(DispatchTable))
printf("\nRegisterServer First!");
return 0;
}
void WINAPI ZServiceMain(DWORD argc,LPTSTR *agv)
{
m_ServiceStatusHandle=RegisterServiceCtrlHandler("WindowsMgr",(LPHANDLER_FUNCTION )ServiceCtrlHandler);
m_ServiceStatus.dwServiceType=SERVICE_WIN32;
m_ServiceStatus.dwCurrentState=SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwServiceSpecificExitCode=S_OK;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
if(m_ServiceStatusHandle==(SERVICE_STATUS_HANDLE)0)
return ;
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
msg();
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
{
m_ServiceStatus.dwCurrentState=SERVICE_PAUSED;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
}
case SERVICE_CONTROL_SHUTDOWN:
break;
case SERVICE_CONTROL_CONTINUE:
{m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;}
case SERVICE_CONTROL_STOP:
{m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwCurrentState=SERVICE_STOP_PENDING;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
}
case SERVICE_CONTROL_INTERROGATE:
{
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
}
}
}
BOOL installService()//服务安装函数
{
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
GetModuleFileName(NULL,strDir,sizeof(strDir));
char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\\WindowsMgr.exe");
if(CopyFile(strDir,chSysPath,FALSE))printf("\nCopy file OK\n");
strcpy(strDir,chSysPath);
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("\nopen scmanager failer,maybe you don't have the privilage to do this\n");
return false;
}
LPCTSTR lpszBinaryPathName=chSysPath;
schService=CreateService(schSCManager,"WindowsMgr","Windows Manager Control",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_SHARE_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
lpszBinaryPathName,
NULL,NULL,NULL,NULL,NULL);
if(schService==NULL)
{
printf("\nFaint,we failed just because we invoke createservices failed\n");
return false;
}
CloseServiceHandle(schService);
return true;
}
BOOL DeleteService()//服务删除函数
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\\WindowsMgr.exe");
if(schSCManager==NULL)
{
printf("faint,open scmanager failer\n");
return false;
}
hService=OpenService(schSCManager,"WIndowsMgr",SERVICE_ALL_ACCESS);
if(hService==NULL)
{
printf("faint,open services failt\n");
return false;
}
if(DeleteFile(chSysPath)==0)
{
printf("Dell file Failure!\n");
return false;
}
else printf("Delete file OK!\n");
if(DeleteService(hService)==0)
return false;
else
return true;
}
void msg()//自定义函数
{
MessageBox(NULL,"Hello","Hack",MB_OK);
}
exe做成服务启动的问题??
最近写了一个服务程序,服务可以安装,但是(自动/手动)启动不了,有没有高手啊?我把代码贴出来,请高手来指点(启动是系统提示1053错误)
#include<stdio.h>
#include<windows.h>
#include <winsvc.h>
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ZServiceMain(DWORD argc,LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
void msg();
BOOL installService();
BOOL DeleteService();
int main(int argc,char *argv[])
{
int t;
printf("\twindows based service demo\n");
printf("tgxisone@hotamil.com\n");
if(argc!=3)
{
printf("usage:%s -install[remove]",argv[0]);
}
t=installService();
if(t)
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nERROR installing Service\n");
SERVICE_TABLE_ENTRY DispatchTable[2];
DispatchTable[0].lpServiceName="WindowsMgr";//Service Name
DispatchTable[0].lpServiceProc=(LPSERVICE_MAIN_FUNCTION)ZServiceMain;
DispatchTable[1].lpServiceName=NULL;
DispatchTable[1].lpServiceProc=NULL;
if(!StartServiceCtrlDispatcher(DispatchTable))
printf("\nRegisterServer First!");
return 0;
}
void WINAPI ZServiceMain(DWORD argc,LPTSTR *agv)
{
m_ServiceStatusHandle=RegisterServiceCtrlHandler("WindowsMgr",(LPHANDLER_FUNCTION )ServiceCtrlHandler);
m_ServiceStatus.dwServiceType=SERVICE_WIN32;
m_ServiceStatus.dwCurrentState=SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwServiceSpecificExitCode=S_OK;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
if(m_ServiceStatusHandle==(SERVICE_STATUS_HANDLE)0)
return ;
m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
msg();
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
{
m_ServiceStatus.dwCurrentState=SERVICE_PAUSED;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
}
case SERVICE_CONTROL_SHUTDOWN:
break;
case SERVICE_CONTROL_CONTINUE:
{m_ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;}
case SERVICE_CONTROL_STOP:
{m_ServiceStatus.dwWin32ExitCode=0;
m_ServiceStatus.dwCurrentState=SERVICE_STOP_PENDING;
m_ServiceStatus.dwCheckPoint=0;
m_ServiceStatus.dwWaitHint=0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
}
case SERVICE_CONTROL_INTERROGATE:
{
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
}
}
}
BOOL installService()//服务安装函数
{
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
GetModuleFileName(NULL,strDir,sizeof(strDir));
char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\\WindowsMgr.exe");
if(CopyFile(strDir,chSysPath,FALSE))printf("\nCopy file OK\n");
strcpy(strDir,chSysPath);
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("\nopen scmanager failer,maybe you don't have the privilage to do this\n");
return false;
}
LPCTSTR lpszBinaryPathName=chSysPath;
schService=CreateService(schSCManager,"WindowsMgr","Windows Manager Control",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_SHARE_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
lpszBinaryPathName,
NULL,NULL,NULL,NULL,NULL);
if(schService==NULL)
{
printf("\nFaint,we failed just because we invoke createservices failed\n");
return false;
}
CloseServiceHandle(schService);
return true;
}
BOOL DeleteService()//服务删除函数
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
char chSysPath[1024];
GetSystemDirectory(chSysPath,sizeof(chSysPath));
strcat(chSysPath,"\\WindowsMgr.exe");
if(schSCManager==NULL)
{
printf("faint,open scmanager failer\n");
return false;
}
hService=OpenService(schSCManager,"WIndowsMgr",SERVICE_ALL_ACCESS);
if(hService==NULL)
{
printf("faint,open services failt\n");
return false;
}
if(DeleteFile(chSysPath)==0)
{
printf("Dell file Failure!\n");
return false;
}
else printf("Delete file OK!\n");
if(DeleteService(hService)==0)
return false;
else
return true;
}
void msg()//自定义函数
{
MessageBox(NULL,"Hello","Hack",MB_OK);
}