Click here to Skip to main content
15,922,407 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionSlow DTS package Pin
Member 290588913-May-08 21:05
Member 290588913-May-08 21:05 
QuestionUsing MFC class in a native application [modified] Pin
sabeeshcs13-May-08 20:22
sabeeshcs13-May-08 20:22 
AnswerRe: Using MFC class in a native application Pin
Nelek13-May-08 21:32
protectorNelek13-May-08 21:32 
AnswerRe: Using MFC class in a native application Pin
Iain Clarke, Warrior Programmer13-May-08 22:00
Iain Clarke, Warrior Programmer13-May-08 22:00 
RantRe: Using MFC class in a native application Pin
toxcct13-May-08 23:24
toxcct13-May-08 23:24 
GeneralRe: Using MFC class in a native application Pin
CPallini13-May-08 23:42
mveCPallini13-May-08 23:42 
GeneralRe: Using MFC class in a native application Pin
toxcct13-May-08 23:43
toxcct13-May-08 23:43 
JokeRe: Using MFC class in a native application Pin
Rajesh R Subramanian14-May-08 1:05
professionalRajesh R Subramanian14-May-08 1:05 
QuestionRe: Using MFC class in a native application Pin
David Crow14-May-08 3:16
David Crow14-May-08 3:16 
QuestionDrawing child controls in MetaFile Pin
sv1413-May-08 20:21
sv1413-May-08 20:21 
AnswerRe: Drawing child controls in MetaFile Pin
ShilpiP13-May-08 21:27
ShilpiP13-May-08 21:27 
GeneralRe: Drawing child controls in MetaFile Pin
sv1413-May-08 23:47
sv1413-May-08 23:47 
GeneralRe: Drawing child controls in MetaFile Pin
ShilpiP14-May-08 1:01
ShilpiP14-May-08 1:01 
GeneralRe: Drawing child controls in MetaFile Pin
sv1414-May-08 18:55
sv1414-May-08 18:55 
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 

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.