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))
{
}
}
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;
}
}
DWORD WINAPI Ipcthread(LPVOID lpParam)
{
HANDLE ghTestEvent;
TCHAR buf[50];
DWORD err;
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)
#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;
}
|