Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # define WINVER 0x501
- #include <stdio.h>
- #include <windows.h>
- #include <dbt.h>
- //#include <wtsapi32.h>
- #include <tchar.h>
- //#include <winsvc.h>
- //#include <Winuser.h>
- #define WTS_CURRENT_SERVER ((HANDLE)NULL)
- #define WTS_CURRENT_SERVER_HANDLE ((HANDLE)NULL)
- #define WTS_CURRENT_SERVER_NAME (NULL)
- #define WTS_CURRENT_SESSION ((DWORD)-1)
- #define SLEEP_TIME 5000
- typedef BOOL ( *fptr1 ) ( HANDLE ,DWORD , LPTSTR, DWORD , LPTSTR , DWORD , DWORD , DWORD , DWORD* , BOOL );
- typedef DWORD ( *fptr2 ) ( void );
- void start();
- DWORD ControlHandler(DWORD, DWORD, LPVOID, LPVOID);
- // Declarations for popup message at stop request
- fptr1 WTSSendMessage;
- DWORD res;
- fptr2 WTSGetActiveConsoleSessionId;
- //LPTSTR lpszTitle = L"Time Change";
- //LPTSTR lpszText = L"Sample";
- //const WTS_CURRENT_SERVER_HANDLE = 0;
- TCHAR* serviceName = TEXT ( "Beeper Service" );
- SERVICE_STATUS serviceStatus;
- SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
- HANDLE stopServiceEvent = 0;
- HDEVNOTIFY hDeviceNotify;
- FILE* Log;
- DWORD ControlHandler( DWORD dwControl, DWORD dwEventType,
- LPVOID lpEventData, LPVOID lpContext )
- {
- Log = fopen("C:\\mss.log", "a");
- fprintf(Log, "ControlHandler call with %d:%d\n", dwControl, dwEventType);
- HINSTANCE hinstLib = LoadLibrary( "C:\\MinGW\\msys\\1.0\\home\\user\\Sample\\NewF\\wtsapi32.dll" );
- //WTSGetActiveConsoleSessionId = (fptr2)GetProcAddress(hinstLib, "WTSGetActiveConsoleSessionIdW");
- //DWORD dwSession = WTSGetActiveConsoleSessionId();
- switch ( dwControl )
- {
- case SERVICE_CONTROL_INTERROGATE:
- break;
- case SERVICE_CONTROL_SHUTDOWN:
- case SERVICE_CONTROL_STOP:
- WTSSendMessage = (fptr1)GetProcAddress(hinstLib, "WTSSendMessageW");
- BOOL ret = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION, (LPTSTR)L"WARNING", 8*2 ,(LPTSTR)L"USB'S WONT DETECT AUTOMATICALLY, PLEASE START THE SERVICE AGAIN", 74*2 ,MB_ICONERROR|MB_TOPMOST|MB_SETFOREGROUND, 0, &res, FALSE);
- //BOOL ret = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION, (LPWSTR)L"WARNING", 8*2 ,(LPWSTR)L"USB'S WONT DETECT AUTOMATICALLY, PLEASE START THE SERVICE AGAIN", 74*2 ,MB_ICONERROR|MB_TOPMOST|MB_SETFOREGROUND, 0, &res, FALSE);
- /*if ( ret == 0 ) {
- printf ( "ERROR\n" );
- }*/
- //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
- serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- SetEvent( stopServiceEvent );
- return;
- case SERVICE_CONTROL_PAUSE:
- break;
- case SERVICE_CONTROL_CONTINUE:
- break;
- case SERVICE_CONTROL_DEVICEEVENT:
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- fprintf(Log, "SERVICE_CONTROL_DEVICEEVENT! ");
- switch(dwEventType)
- {
- case DBT_DEVICEARRIVAL:
- // Handle
- fprintf(Log, "DBT_DEVICEARRIVAL\n");
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- fprintf(Log, "DBT_DEVICEREMOVECOMPLETE\n");
- // Handle
- break;
- }
- default:
- fprintf(Log, "Unknown dwControl: %d\n", dwControl);
- if ( dwControl >= 128 && dwControl <= 255 )
- // user defined control code
- break;
- else
- // unrecognised control code
- break;
- }
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- }
- void WINAPI ServiceMain( DWORD argc, LPTSTR *argv)
- {
- // initialise service status
- Log = fopen("C:\\mss.log", "a");
- fprintf(Log, "Entering ServiceMain function...\n");
- serviceStatus.dwServiceType = SERVICE_WIN32;
- serviceStatus.dwCurrentState = SERVICE_STOPPED;
- serviceStatus.dwControlsAccepted = 0;
- serviceStatus.dwWin32ExitCode = NO_ERROR;
- serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
- serviceStatus.dwCheckPoint = 0;
- serviceStatus.dwWaitHint = 0;
- serviceStatusHandle = RegisterServiceCtrlHandlerEx( serviceName,(LPHANDLER_FUNCTION_EX)ControlHandler,0 );
- if ( serviceStatusHandle )
- {
- // service is starting
- serviceStatus.dwCurrentState = SERVICE_START_PENDING;
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- // Some initialization
- fprintf(Log, "Registering device notification...\n");
- DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
- hDeviceNotify = NULL;
- static const GUID GuidForMYDevice =
- { 0x4D36E978, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } };
- ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
- NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
- NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
- NotificationFilter.dbcc_classguid = GuidForMYDevice;
- hDeviceNotify = RegisterDeviceNotification( (HANDLE)serviceStatusHandle, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
- fprintf(Log, "RegisterDeviceNotification returned %p\n", hDeviceNotify);
- // do initialisation here
- stopServiceEvent = CreateEvent( 0, FALSE, FALSE, 0 );
- // running
- serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
- serviceStatus.dwCurrentState = SERVICE_RUNNING;
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- fprintf(Log, "Running...\n");
- /*while(serviceStatus.dwCurrentState == SERVICE_RUNNING)
- {
- Sleep(SLEEP_TIME);
- }*/
- do
- {
- Beep( 50, 10 );
- //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
- //printf ( "HI\n" );
- }
- while ( WaitForSingleObject( stopServiceEvent, 5000 ) == WAIT_TIMEOUT );
- //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
- // service was stopped
- serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- // do cleanup here
- CloseHandle( stopServiceEvent );
- stopServiceEvent = 0;
- // service is now stopped
- serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
- serviceStatus.dwCurrentState = SERVICE_STOPPED;
- SetServiceStatus( serviceStatusHandle, &serviceStatus );
- }
- }
- void RunService()
- {
- fprintf(Log, "In Run service...\n");
- SERVICE_TABLE_ENTRY serviceTable[] =
- {
- { serviceName, ServiceMain },
- { 0, 0 }
- };
- //BOOL success;
- // Register the service with the Service Control Manager
- fprintf(Log, "Invoking StartServiceCtrlDispatcher...\n");
- StartServiceCtrlDispatcher(serviceTable);
- }
- void InstallService()
- {
- printf ( "In install service\n" );
- SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
- printf ( "after sc handel\n" );
- if ( serviceControlManager )
- {
- //Log = fopen("C:\\mss.log", "a");
- //printf ( "In if\n" );
- fprintf(Log, "Installing Service...\n");
- printf ( "after fprintf\n" );
- TCHAR path[ _MAX_PATH + 1 ];
- if ( GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0 )
- {
- SC_HANDLE service = CreateService( serviceControlManager,
- serviceName, serviceName,
- SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
- SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
- 0, 0, 0, 0, 0 );
- if ( service )
- CloseServiceHandle( service );
- }
- CloseServiceHandle( serviceControlManager );
- }
- }
- void UninstallService()
- {
- SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
- if ( serviceControlManager )
- {
- //Log = fopen("C:\\mss.log", "a");
- fprintf(Log, "UnInstalling Service...\n");
- SC_HANDLE service = OpenService( serviceControlManager,
- serviceName, SERVICE_QUERY_STATUS | DELETE );
- if ( service )
- {
- SERVICE_STATUS serviceStatus;
- if ( QueryServiceStatus( service, &serviceStatus ) )
- {
- if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
- DeleteService( service );
- }
- CloseServiceHandle( service );
- }
- CloseServiceHandle( serviceControlManager );
- }
- }
- void start ( ) {
- SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
- if ( serviceControlManager )
- {
- //Log = fopen("C:\\mss.log", "a");
- fprintf(Log, "Start Service...\n");
- SC_HANDLE service = OpenService( serviceControlManager,
- serviceName, SERVICE_ALL_ACCESS );
- if ( service )
- {
- SERVICE_STATUS serviceStatus;
- if ( QueryServiceStatus( service, &serviceStatus ) )
- {
- if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
- {
- //StartService( service, 0, NULL );
- if (!StartService(service, 0, NULL) )
- {
- printf("StartService failed (%d)\n", GetLastError());
- } else {
- serviceStatus.dwCurrentState = SERVICE_RUNNING;
- }
- }
- CloseServiceHandle( service );
- }
- CloseServiceHandle( serviceControlManager );
- }
- }
- }
- int _tmain( int argc, TCHAR* argv[] )
- {
- Log = fopen("C:\\mss.log", "a");
- //fprintf(Log, "Logging started...\n");
- //fprintf(Log, "Service executable started...\n");
- if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "install" ) ) == 0 )
- {
- printf ( "In install\n");
- InstallService();
- }
- else if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "uninstall" ) ) == 0 )
- { printf ( "In uninstall\n");
- UninstallService();
- }
- else if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "start" ) ) == 0 )
- {
- printf ( "In uninstall\n");
- start();
- }
- else
- {
- RunService();
- }
- //fprintf(Log, "Logging finished!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment