以下是代码, 在StartServiceCtrlDispatcher 函数 调用是返回 0, GetLastError
返回是1063 ,
winerror.h --> #define ERROR_FAILED_SERVICE_CONTROLLER_CONNECT 1063L
不知道怎么搞?
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();int main(int argc, char* argv[])
{
long lRtn = 0; DeleteService();
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
// if(DeleteService())
// printf("\n\nService UnInstalled Sucessfully\n");
// else
// printf("\n\nError UnInstalling Service\n");
SERVICE_TABLE_ENTRY DispatchTable[]=
{{"Service1",ServiceMain},{NULL,NULL}};
char strDir[1024];
HANDLE schSCManager,schService;
::GetModuleFileName(NULL,strDir,1024);
//GetCurrentDirectory(1024,strDir);
// strcat(strDir,"\\Srv1.exe");
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir; // schService = OpenService((SC_HANDLE)schSCManager,"Service1",SERVICE_ALL_ACCESS);
//if (schService == NULL)
// return false;
lRtn =StartServiceCtrlDispatcher(DispatchTable);
//CloseServiceHandle((SC_HANDLE)schService);
lRtn = GetLastError();
return 0;
}void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
DWORD status;
DWORD specificError;
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("Service1",
ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
printf(" ser handl stauts \n");
return;
}
//m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
// m_ServiceStatus.dwCheckPoint = 0;
// m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{
printf(" stuas 2\n");
} printf(" service run \n");
bRunning=true;
while(bRunning)
{
Sleep(3000);
printf(" ok \n");
//Place Your Code for processing here....
}
return;
}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_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(m_ServiceStatusHandle,&m_ServiceStatus);
break;
case SERVICE_CONTROL_STOP:
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;
case SERVICE_CONTROL_INTERROGATE:
break;
} return;
}BOOL InstallService()
{
char strDir[1024];
HANDLE schSCManager,schService;
GetModuleFileName(NULL,strDir,1024);
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir; schService = CreateService((SC_HANDLE)schSCManager,"Service1",
"The Display Name Needed", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password if (schService == NULL)
return false;
CloseServiceHandle((SC_HANDLE)schService);
return true;
}BOOL DeleteService()
{
HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL)
return false;
hService=OpenService((SC_HANDLE)schSCManager,"Service1",SERVICE_ALL_ACCESS);
if (hService == NULL)
return false;
if(DeleteService(hService)==0)
return false;
if(CloseServiceHandle(hService)==0)
return false;return true;
}
//m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
// m_ServiceStatus.dwCheckPoint = 0;
// m_ServiceStatus.dwWaitHint = 0;3行得注释