Click here to Skip to main content
15,905,686 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow Can Get SystemTime? Pin
Le@rner13-May-08 19:45
Le@rner13-May-08 19:45 
AnswerRe: How Can Get SystemTime? Pin
ShilpiP13-May-08 20:01
ShilpiP13-May-08 20:01 
GeneralRe: How Can Get SystemTime? Pin
Le@rner13-May-08 20:09
Le@rner13-May-08 20:09 
GeneralRe: How Can Get SystemTime? [modified] Pin
Le@rner13-May-08 20:18
Le@rner13-May-08 20:18 
GeneralRe: How Can Get SystemTime? Pin
ShilpiP13-May-08 20:46
ShilpiP13-May-08 20:46 
GeneralRe: How Can Get SystemTime? Pin
Le@rner13-May-08 21:03
Le@rner13-May-08 21:03 
GeneralRe: How Can Get SystemTime? Pin
ShilpiP13-May-08 21:13
ShilpiP13-May-08 21:13 
QuestionGetting problem in writing windows service [ ERROR 1053 ] [modified] Pin
Soumyadipta13-May-08 19:43
Soumyadipta13-May-08 19:43 
I am using visual studio 2005 and i am trying to write a windows service which will basically check whether a particular exe is running or not.

I have down loaded a sample code and created a service called "Service1" and i am able to successfully install and uninstall the service in to my windows XP machine with service pack 2.

Problem:
I cant start the service from the win service list and i am getting an error message as below.

Could not start service on the local system.
ERROR 1053:The service did not respond to the start or control request in a timely fashion.

Below is my service code

#include "stdafx.h"
#include "Windows.h"
#include "Winsvc.h"
#include "time.h"
#include "tlhelp32.h"



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();

bool isExeRunning(WCHAR *zExeName, bool *pbRunning);

int main(int argc, char* argv[])
{
	if(argc>1)
	{
		if(strcmp(argv[1],"-i")==0)
		{
			if(InstallService())
				printf("\n\nService Installed Sucessfully\n");
			else
				printf("\n\nError Installing Service\n");
		}
		if(strcmp(argv[1],"-d")==0)
		{
			if(DeleteService())
				printf("\n\nService UnInstalled Sucessfully\n");
			else
				printf("\n\nError UnInstalling Service\n");
		}
		else
		{
			printf("\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
		}
	}
	else
	{
		SERVICE_TABLE_ENTRY DispatchTable[]={{L"PointService",ServiceMain},{NULL,NULL}};
		StartServiceCtrlDispatcher(DispatchTable);		
	}

	return 0;
}

void WINAPI ServiceMain(DWORD argc, 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(L"PointService", ServiceCtrlHandler); 
	if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
	{
		return;
	}

	m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
	m_ServiceStatus.dwCheckPoint = 0;
	m_ServiceStatus.dwWaitHint = 0;
	if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
	{
	}

	bRunning=true;
	while(bRunning)
	{
		Sleep(3000);	

		bool pbRunning = true;

		isExeRunning(L"Point-HD.exe",&pbRunning);

		if(pbRunning == true)
		{
			printf("Point-HD running......\n");
		}
		else
		{
			printf("Point-HD running......\n");
		}	
	}
	
	return;
}

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
	switch(Opcode)
	{
		case SERVICE_CONTROL_PAUSE: 
			m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
			break;
		case SERVICE_CONTROL_CONTINUE:
			m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
			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()
{
	WCHAR strDir[1024];
	SC_HANDLE schSCManager,schService;
	GetCurrentDirectory(1024,strDir);
	wcscat(strDir,L"\\Point-HDListener.exe");
	schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

	if (schSCManager == NULL) 
		return false;
	LPCTSTR lpszBinaryPathName=strDir;

	schService = CreateServiceW(schSCManager,
								L"PointService", 
								L"Point HD Listener Service",	// 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(schService);
	return true;
}

BOOL DeleteService()
{
	SC_HANDLE schSCManager;
	SC_HANDLE hService;
	
	schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if (schSCManager == NULL)
		return false;
	
	hService=OpenService(schSCManager,L"PointService",SERVICE_ALL_ACCESS);
	if (hService == NULL)
		return false;

	if(DeleteService(hService)==0)
		return false;

	if(CloseServiceHandle(hService)==0)
		return false;

	return true;
}

bool isExeRunning(WCHAR *zExeName, bool *pbRunning)
{
	if (! pbRunning) return FALSE;		

	PROCESSENTRY32 pe32;
	BOOL bSuccess = TRUE;
	*pbRunning = FALSE;

	//Takes a snapshot of the specified processes, 
	//as well as the heaps, modules, and threads used by these processes.
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

	if (hSnapshot!=INVALID_HANDLE_VALUE) 
	{		
		// Set the size of the structure before using it.
		pe32.dwSize = sizeof( PROCESSENTRY32 );

		//Describes an entry from a list of the processes residing 
		//in the system address space when a snapshot was taken.

		BOOL bRes = Process32First(hSnapshot, &pe32);

		while (bRes==TRUE)
		{			
			wprintf(L"%s\n",pe32.szExeFile);
			if (0==wcscmp(zExeName, pe32.szExeFile)) 
			{
				*pbRunning = TRUE;
				break;
			}
			//Retrieves information about the next 
			//process recorded in a system snapshot.

			bRes = Process32Next(hSnapshot, &pe32);
		}

		if (bRes!=TRUE && GetLastError()!=ERROR_NO_MORE_FILES) 
		{
			bSuccess = FALSE;
		}
		
		CloseHandle(hSnapshot);
	} 
	else 
	{
		bSuccess = FALSE;
	}

	return bSuccess;
}


modified on Wednesday, May 14, 2008 8:53 AM

AnswerRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
Rajkumar R13-May-08 23:52
Rajkumar R13-May-08 23:52 
AnswerRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
Soumyadipta14-May-08 1:27
Soumyadipta14-May-08 1:27 
GeneralRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
Iain Clarke, Warrior Programmer14-May-08 1:53
Iain Clarke, Warrior Programmer14-May-08 1:53 
GeneralRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
Soumyadipta14-May-08 2:47
Soumyadipta14-May-08 2:47 
GeneralRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
netprotector31-Mar-09 20:08
netprotector31-Mar-09 20:08 
AnswerRe: Getting problem in writing windows service [ ERROR 1053 ] Pin
Soumyadipta14-May-08 4:23
Soumyadipta14-May-08 4:23 
QuestionMFC application crashes @ ConnectionTransact of DBNMPNTW.DLL Pin
Arun Kuriyakkotte Ramachandran13-May-08 19:13
Arun Kuriyakkotte Ramachandran13-May-08 19:13 
AnswerRe: MFC application crashes @ ConnectionTransact of DBNMPNTW.DLL Pin
kasturi_haribabu13-May-08 19:25
kasturi_haribabu13-May-08 19:25 
QuestionApplication gets slower when having ~50 dialogs in memory. Pin
Paresh Chitte13-May-08 19:07
Paresh Chitte13-May-08 19:07 
AnswerRe: Application gets slower when having ~50 dialogs in memory. Pin
Steve Echols13-May-08 19:26
Steve Echols13-May-08 19:26 
GeneralRe: Application gets slower when having ~50 dialogs in memory. Pin
Paresh Chitte13-May-08 20:19
Paresh Chitte13-May-08 20:19 
GeneralRe: Application gets slower when having ~50 dialogs in memory. Pin
Nelek13-May-08 21:28
protectorNelek13-May-08 21:28 
GeneralRe: Application gets slower when having ~50 dialogs in memory. Pin
Paresh Chitte14-May-08 0:02
Paresh Chitte14-May-08 0:02 
QuestionProblem in Mapping Memory(win98,P4) Pin
Srinivas Reddy CH13-May-08 19:01
Srinivas Reddy CH13-May-08 19:01 
Question[Message Deleted] Pin
projectip13-May-08 18:59
projectip13-May-08 18:59 
AnswerRe: Marking region of interest Pin
Iain Clarke, Warrior Programmer13-May-08 22:06
Iain Clarke, Warrior Programmer13-May-08 22:06 
QuestionRe: Marking region of interest Pin
CPallini13-May-08 22:21
mveCPallini13-May-08 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.