|
-
June 30th, 2009, 05:34 AM
#1
Windown service not starting
hi all,
i have written a small service.now when i install the service, the service gets installed what i feel but is not able to start. using an installer i am installing the service application in windows vista.
it gives out an error during installation, when the service is getting started.
it throws an error as follows:
Create process failed: error code 14001. the application has failed to start because the application configuration is incorrect. reinstalling the application may fix the problem.
i tried reinstalling, however the same problem persists...
i am pasting the code below,, for initial main, service installation and service start functions..please can i get some help on fixing the error....
*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void __cdecl _tmain(int argc, char **argv)
{
HKEY hKeyHandle;
DWORD dwDataToWrite = 1;
if( argc == 3 )
{
printf("ERROR:\tIncorrect number of arguments\n\n");
return;
}
char *arg1 = argv[1];
char *arg2 = argv[2];
if(_strnicmp(arg1, "install", 7) == 0)
{
if(!PathFileExists(arg2 ))
{
;
exit(1);
}
if (InstallService(arg2))
{
MessageBox(NULL,"Service installed successfully",Message,MB_OK);
}
else
{
MessageBox(NULL,"Error in service installation",Message,MB_OK);
exit(2);
}
}
else if (lstrcmpi( arg1, TEXT("uninstall")) == 0 )
{
if(!ServiceDelete())
{
MessageBox(NULL,"Error in Un-installing",Message,MB_OK);
exit(3);
}
}
else if (lstrcmpi( arg1, TEXT("enable")) == 0 )
{
if (!EnableService())
{
MessageBox(NULL,"Error in enabling",Message,MB_OK);
exit(4);
}
}
else
exit(0);
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SRVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain },
{ NULL, NULL }
};
StartServiceCtrlDispatcher(DispatchTable);
}
// Service main function for the service
VOID WINAPI SvcMain(DWORD dwArgs,LPTSTR *lpszArgv)
{
DWORD dwErr = 0;
debug_log("In Service Main-The Entry Point for Service");
// Register the handler function to service
svcStatusHandle = RegisterServiceCtrlHandlerEx(TEXT(SRVCNAME),(LPHANDLER_FUNCTION_EX)SvcCtrlHandler,NULL);
if(!svcStatusHandle)
{
debug_log("Failed to register service handler function error(%d) returned",GetLastError());
(VOID)ReportServiceStatus(SERVICE_STOPPED,dwErr,0);
return;
}
// service status members remain same as set here.
svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
svcStatus.dwServiceSpecificExitCode = 0;
if(!ReportServiceStatus( SERVICE_START_PENDING, NO_ERROR, 3000 ));
CleanupHandles();
//Start the service...
ServiceStart(dwArgs,lpszArgv);
debug_log("Exiting Service Main");
}
//Function for installing the service in SCM database
BOOL InstallService(char *szPath)
{
SC_LOCK sclLock;
LPQUERY_SERVICE_LOCK_STATUS lpqslsBuf;
SERVICE_DESCRIPTION sdBuf;
DWORD dwBytesNeeded;
BOOL bSuccess=TRUE;
SC_ACTION sca[3];
SERVICE_FAILURE_ACTIONS sfaBuf;
debug_log("Installing service now...");
//Get a handle to SCM database..
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE | SC_MANAGER_ALL_ACCESS | SC_MANAGER_LOCK | SC_MANAGER_QUERY_LOCK_STATUS);
if (schSCManager == NULL)
{
debug_log("Failed to open SCM database, error-(%d) returned\n",GetLastError());
return false;
}
else debug_log("Connection to SCM database success");
LPCTSTR lpszBinaryPathName= szPath;
// If database opened create the service
schService = CreateService(schSCManager,SRVCNAME,SRVCNAME,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,lpszBinaryPathName,NULL,NULL,NULL,NULL,"");
if(schService == NULL)
{
debug_log("Error in creating service,[%d] error code returned",GetLastError());
return false;
}
else debug_log("Service %s created and installed",SRVCNAME);
CloseHandle(schService);
// service config change to specify action on failure
debug_log("Acquiring lock on SCM database");
sclLock = LockServiceDatabase(schSCManager);
if (sclLock == NULL)
{
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
{
debug_log("Database Lock failed");
return FALSE;
}
// Allocate buffer to det details about the lock
lpqslsBuf = (LPQUERY_SERVICE_LOCK_STATUS)LocalAlloc(LPTR,sizeof(QUERY_SERVICE_LOCK_STATUS)+ 256);
if (lpqslsBuf == NULL)
{
debug_log("Local alloc failed");
return FALSE;
}
// Get the lock status information
if(!QueryServiceLockStatus(schSCManager,lpqslsBuf,sizeof(QUERY_SERVICE_LOCK_STATUS)+256,&dwBytesNeeded))
{
debug_log("Query lock status failed");
return FALSE;
}
if (lpqslsBuf->fIsLocked)
{
debug_log("Already locked by %s",lpqslsBuf->fIsLocked);
}
else
LocalFree(lpqslsBuf);
}
//if database is locked make the changes
schService= OpenService(schSCManager,SRVCNAME,SERVICE_ALL_ACCESS | SERVICE_CHANGE_CONFIG);
if (schService == NULL)
{
debug_log("Open service failed");
return FALSE;
}
sca[0].Type = SC_ACTION_RESTART;
sca[0].Delay = 1;
sca[1].Type = SC_ACTION_RESTART;
sca[1].Delay = 1;
sca[2].Type = SC_ACTION_RESTART;
sca[2].Delay = 1;
sfaBuf.cActions = 3;
sfaBuf.lpsaActions = sca;
sfaBuf.dwResetPeriod = INFINITE;
sdBuf.lpDescription = "Test Service";
if (!ChangeServiceConfig2(schService,SERVICE_CONFIG_FAILURE_ACTIONS,&sfaBuf))
{
debug_log("Change service config failed");
bSuccess = FALSE;
}
UnlockServiceDatabase(sclLock);
CleanupHandles();
debug_log("Exiting install service...");
return bSuccess;
}
// Start the service
VOID ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv)
{
debug_log("Inside Service Start,Service Started");
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
// Acquire handle to the SCM database.
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
debug_log("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Handle to the service.
schService = OpenService(schSCManager,SRVCNAME,SERVICE_ALL_ACCESS);
if (schService == NULL)
{
debug_log("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
// Start the service.
if (!StartService(schService,0,NULL) )
{
debug_log("StartService failed error-(%d) returned\n", GetLastError());
CleanupHandles();
return;
}
// Check if the service has started
if (!QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE) &ssStatus,sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded ) )
{
debug_log("Service status 1 query failed,error[%d] returned",GetLastError());
return;
}
// Check the system start time....
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE) &ssStatus,sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) )
{
debug_log("Service status 2 query failed,error[%d] returned",GetLastError());
break;
}
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// Service is running with system start
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
//Service hasn't been started with system start
debug_log("Service has not started with system startup");
break;
}
}
}
// Determine whether the service is running
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
debug_log("Service has been started successfully.\n");
}
else
{
debug_log("Service not started. \n");
debug_log(" Current State: %d\n", ssStatus.dwCurrentState);
debug_log(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
debug_log(" Check Point: %d\n", ssStatus.dwCheckPoint);
debug_log(" Wait Hint: %d\n", ssStatus.dwWaitHint);
}
// Start the action to be performed by the service..
debug_log("Starting the service init process now,work to be performed by service");
ServiceInit();
debug_log("Exiting Service Start now..");
CleanupHandles();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////******
all the variables used have been declared and there are no compilation error...
using visual studio 2008 for service application development for windows vista...
Thankuou...
-
June 30th, 2009, 05:50 AM
#2
Re: Windown service not starting
First of all debugging the services is really hard work.
Second: use code outlining it will be much more readable for us mortals.
Third: Install on Vista Visual Studio 2008 redistributable, to get the appropriate dll-s
Forth: don't use MessageBox or any GUI unless "Allow service to interact with desktop" is checked.
And last if you want to make your life easier with the services check this service template:
http://www.naughter.com/serv.html
-
June 30th, 2009, 06:44 AM
#3
Re: Windown service not starting
Making your post readable using these and removing all those empty lines helps ALOT.
-
June 30th, 2009, 02:27 PM
#4
Re: Windown service not starting
Is your goal to learn how to hand write a service or is the goal to get the service code running quickly so you can get on to coding the rest of the functionality?
If the latter, then I suggest you start creating the service by using the ATL Service template. Using the ATL service template, you can get a service created, and registered in about 5 minutes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|