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

C / C++ / MFC

 
GeneralRe: IPC on Vista (service and application) Pin
Mark Salsbery23-Sep-08 5:30
Mark Salsbery23-Sep-08 5:30 
GeneralRe: IPC on Vista (service and application) Pin
navaneethm23-Sep-08 20:53
navaneethm23-Sep-08 20:53 
GeneralRe: IPC on Vista (service and application) Pin
Mark Salsbery24-Sep-08 5:18
Mark Salsbery24-Sep-08 5:18 
GeneralRe: IPC on Vista (service and application) Pin
navaneethm24-Sep-08 18:02
navaneethm24-Sep-08 18:02 
GeneralRe: IPC on Vista (service and application) Pin
Mark Salsbery25-Sep-08 5:16
Mark Salsbery25-Sep-08 5:16 
GeneralRe: IPC on Vista (service and application) Pin
Mark Salsbery25-Sep-08 10:39
Mark Salsbery25-Sep-08 10:39 
GeneralRe: IPC on Vista (service and application) Pin
navaneethm25-Sep-08 19:48
navaneethm25-Sep-08 19:48 
GeneralRe: IPC on Vista (service and application) Pin
navaneethm24-Sep-08 18:30
navaneethm24-Sep-08 18:30 
i made a sample service and a console application for testing .i am putting complete code bellow.


Service ( Application 1)

#include <windows.h>

SERVICE_STATUS FireServiceStatus;
SERVICE_STATUS_HANDLE FireServiceStatusHandle;

HANDLE hevent;
HANDLE hFile;
HANDLE ghTestEvent;
HANDLE ghIPCthread = NULL ;

void WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
void ServiceInstall();
DWORD WINAPI Ipcthread(LPVOID lpParam);
void servicestop();
void WINAPI FireServiceHandler(DWORD dwctrl);

VOID CALLBACK ApcProc(ULONG_PTR dwparam)
{
}


void __cdecl _tmain(int argc, _TCHAR* argv[])
{
	   	
	if(lstrcmpi(argv[1],TEXT("Install"))==0)
	{
		ServiceInstall();
		return;
	}
	SERVICE_TABLE_ENTRY DispatchTable[]	=	{{TEXT("SampleService"),ServiceMain},{NULL,NULL}	};
	if(!StartServiceCtrlDispatcher(DispatchTable))
	{
		//Error

		//Error
	}
		

}


void WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
	
	
	FireServiceStatus.dwCheckPoint				=	1;	
	FireServiceStatus.dwControlsAccepted		=	SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN ;
	FireServiceStatus.dwCurrentState			=	SERVICE_START_PENDING;	
	FireServiceStatus.dwServiceSpecificExitCode	=	0;
	FireServiceStatus.dwServiceType				=	SERVICE_WIN32;
	FireServiceStatus.dwWaitHint				=	3000;
	FireServiceStatus.dwWin32ExitCode			=	0;

	FireServiceStatusHandle	=	RegisterServiceCtrlHandler(TEXT("SampleService"),FireServiceHandler);

	if(!FireServiceStatusHandle )
	{
		goto cleanup;
	}

	if(!SetServiceStatus(FireServiceStatusHandle,&FireServiceStatus))
	{
		goto cleanup;
	}


	FireServiceStatus.dwCheckPoint				=	0;
	FireServiceStatus.dwCurrentState			=	SERVICE_RUNNING;
	FireServiceStatus.dwWaitHint				=	0;
	FireServiceStatus.dwControlsAccepted		=	SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN ;
	SetServiceStatus(FireServiceStatusHandle,&FireServiceStatus);
	
	ghIPCthread = CreateThread(NULL,0,Ipcthread,NULL,0,NULL);	

	servicestop();
	


cleanup:
		FireServiceStatus.dwCheckPoint				=	0;
		FireServiceStatus.dwCurrentState			=	SERVICE_STOP;
		FireServiceStatus.dwWaitHint				=	0;
		SetServiceStatus(FireServiceStatusHandle,&FireServiceStatus);

		if( ghIPCthread == NULL)
		{
			QueueUserAPC(ApcProc,ghIPCthread,NULL);
			ghIPCthread = NULL;
		}

}

//***************
//please see here

//here creates the event and waiting for the event
//***************

DWORD WINAPI Ipcthread(LPVOID lpParam)

{
		
	HANDLE ghTestEvent;
		
   	TCHAR buf[50];
	DWORD err;


	



	
	//for error checking ,write all errors to this file

	hFile = CreateFile(	TEXT("D:\\eventerror.txt"),
						GENERIC_WRITE,
						FILE_SHARE_READ,
						NULL,
						CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL,
						NULL);

	ghTestEvent		=	CreateEvent(NULL,FALSE,FALSE,TEXT("Global\\sampleEvent"));

	if(ghTestEvent	==	NULL)
	{
		wsprintf(buf,TEXT("CreateEvent failed %d"),GetLastError());
		WriteFile(hFile,buf,sizeof(buf),NULL,NULL);
		RtlZeroMemory(buf,sizeof(buf));
		goto Cleanup;
	}
 			
	 
			
	err = WaitForSingleObject(ghTestEvent,INFINITE);
	
	if( err == WAIT_FAILED)
	{
		wsprintf(buf, TEXT("WaitForSingleObject failed %d"),GetLastError());
		WriteFile(hFile,buf,sizeof(buf),NULL,NULL);
		RtlZeroMemory(buf,sizeof(buf));
		goto Cleanup;
	}
	wsprintf(buf, TEXT("WaitForSingleObject Success:Got the event") );
	WriteFile(hFile,buf,sizeof(buf),NULL,NULL);
	RtlZeroMemory(buf,sizeof(buf));
	

Cleanup:
CloseHandle(ghTestEvent);
CloseHandle(hFile);
return err;
}

void ServiceInstall()
{
	SC_HANDLE sc_manager;
	SC_HANDLE sc_service;
	TCHAR szPath[MAX_PATH];

	if(GetModuleFileName(NULL,szPath,MAX_PATH))
	{
		printf("GetModuleFileName failed %d",GetLastError());

	}
	sc_manager = OpenSCManager( NULL,NULL,SC_MANAGER_ALL_ACCESS);

	if(sc_manager == NULL)
	{
		printf("openSCManager failed with error code %d \n",GetLastError());
		return;
	}
	
	sc_service = CreateService(	sc_manager,
								TEXT("SampleService"),
								TEXT("SampleService"),
								SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,
								SERVICE_DEMAND_START,
								SERVICE_ERROR_NORMAL,
								szPath,
								NULL,
								NULL,
								NULL,
								NULL,
								NULL );
	if( sc_service == NULL )
	{
		printf("Create service failed %d\n",GetLastError());

		CloseServiceHandle(sc_manager);
		return;
	}
	else
		printf("service installed successfully\n");

	CloseServiceHandle(sc_service);
	CloseServiceHandle(sc_manager);
	
	
}

void WINAPI FireServiceHandler(DWORD dwCtrl)
{
	switch(dwCtrl)
	{
	case SERVICE_CONTROL_SHUTDOWN:
	case SERVICE_CONTROL_STOP:
		FireServiceStatus.dwCheckPoint				=	0;
		FireServiceStatus.dwCurrentState			=	SERVICE_STOP_PENDING;
		FireServiceStatus.dwWaitHint				=	0;
		FireServiceStatus.dwControlsAccepted		=	0 ;
		SetServiceStatus(FireServiceStatusHandle,&FireServiceStatus);

		SetEvent(hevent);
		
		break;
	case SERVICE_CONTROL_INTERROGATE:
		break;
	default:
		break;

	}
	return ;
	

}


void servicestop()
{
	hevent=CreateEvent(NULL,TRUE,FALSE,NULL);
	while(1)
	{
		WaitForSingleObject(hevent,INFINITE);
		

		FireServiceStatus.dwCheckPoint				=	0;
		FireServiceStatus.dwCurrentState			=	SERVICE_STOPPED;
		FireServiceStatus.dwWaitHint				=	0;
		SetServiceStatus(FireServiceStatusHandle,&FireServiceStatus);
		return;
		
	}
}</windows.h>




Console Application (Application 2)

//Here its open the event(but failed with error 5),I tried both way (in elvated and non elvated)
// 
#include<windows.h>
#include<stdio.h>
#include<tchar.h>

int main()
{

	HANDLE ghEvent;
	BOOL res;
	
	ghEvent	= OpenEvent( SYNCHRONIZE|EVENT_MODIFY_STATE  , FALSE, TEXT("Global\\sampleEvent"));
	
	if (ghEvent == NULL)
	{
		printf("error on opening the event %d ",GetLastError());
		getchar();	
		return 1;
	
	}

	res = SetEvent(ghEvent);

	if(!res)
	{
		printf("SetEvent failed,%d",GetLastError());
		getchar();
		return 1;
		
	}
	printf("SetEvent success\n");
	return 0;

}

QuestionHow to install updates from server system to clients systems. Pin
brucewayn18-Sep-08 21:15
brucewayn18-Sep-08 21:15 
AnswerRe: How to install updates from server system to clients systems. Pin
Rajesh R Subramanian18-Sep-08 21:53
professionalRajesh R Subramanian18-Sep-08 21:53 
GeneralRe: How to install updates from server system to clients systems. Pin
brucewayn18-Sep-08 22:31
brucewayn18-Sep-08 22:31 
GeneralRe: How to install updates from server system to clients systems. Pin
Rajesh R Subramanian19-Sep-08 1:30
professionalRajesh R Subramanian19-Sep-08 1:30 
QuestionAdd/Remove operation is impossible, because code element "MyDlg" is read only ! Pin
kapardhi18-Sep-08 20:46
kapardhi18-Sep-08 20:46 
AnswerRe: Add/Remove operation is impossible, because code element "MyDlg" is read only ! Pin
David Crow19-Sep-08 3:20
David Crow19-Sep-08 3:20 
QuestionDebug Assertion Failure Pin
bhanu_850918-Sep-08 20:39
bhanu_850918-Sep-08 20:39 
AnswerRe: Debug Assertion Failure Pin
Cedric Moonen18-Sep-08 20:46
Cedric Moonen18-Sep-08 20:46 
GeneralRe: Debug Assertion Failure Pin
bhanu_850918-Sep-08 21:52
bhanu_850918-Sep-08 21:52 
GeneralRe: Debug Assertion Failure Pin
Cedric Moonen18-Sep-08 21:54
Cedric Moonen18-Sep-08 21:54 
GeneralRe: Debug Assertion Failure Pin
bhanu_850918-Sep-08 22:03
bhanu_850918-Sep-08 22:03 
GeneralRe: Debug Assertion Failure Pin
Cedric Moonen18-Sep-08 22:13
Cedric Moonen18-Sep-08 22:13 
QuestionRe: Debug Assertion Failure Pin
Rajesh R Subramanian18-Sep-08 22:01
professionalRajesh R Subramanian18-Sep-08 22:01 
AnswerRe: Debug Assertion Failure Pin
bhanu_850918-Sep-08 22:10
bhanu_850918-Sep-08 22:10 
QuestionRe: Debug Assertion Failure Pin
Rajesh R Subramanian18-Sep-08 22:14
professionalRajesh R Subramanian18-Sep-08 22:14 
QuestionRe: Debug Assertion Failure Pin
David Crow19-Sep-08 3:23
David Crow19-Sep-08 3:23 
QuestionRe: Debug Assertion Failure Pin
Rajesh R Subramanian18-Sep-08 20:49
professionalRajesh R Subramanian18-Sep-08 20:49 

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.