Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- void PrintUsage();
- SC_HANDLE schSCManager;
- SC_HANDLE schService;
- char * szSvcName;
- void DoCreateSvc()
- {
- SC_HANDLE schSCManager;
- SC_HANDLE schService;
- // TCHAR szPath[MAX_PATH];
- // if( !GetModuleFileName( "", szPath, MAX_PATH ) )
- // {
- // printf("Cannot install service (%d)\n", (unsigned int)GetLastError());
- // return;
- // }
- // Get a handle to the SCM database.
- schSCManager = OpenSCManager(
- NULL, // local computer
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access rights
- if (NULL == schSCManager)
- {
- printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
- return;
- }
- // Create the service
- char aCurrentDirectory[515];
- char aPath[1024];
- GetCurrentDirectory( 512, aCurrentDirectory);
- _snprintf(aPath, 1022, "%s\\%s", aCurrentDirectory, szSvcName);
- // Create the service
- schService = CreateService(schSCManager,
- szSvcName,
- szSvcName,
- SERVICE_ALL_ACCESS,
- SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START,
- SERVICE_ERROR_NORMAL,
- aPath,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL);
- // schService = CreateService(
- // schSCManager, // SCM database
- // SVCNAME, // name of service
- // SVCNAME, // 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
- // szPath, // path to service's binary
- // NULL, // no load ordering group
- // NULL, // no tag identifier
- // NULL, // no dependencies
- // NULL, // LocalSystem account
- // NULL); // no password
- if (schService == NULL)
- {
- printf("CreateService failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schSCManager);
- return;
- }
- else printf("Service installed successfully\n");
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- }
- void __stdcall DoStartSvc()
- {
- SERVICE_STATUS_PROCESS ssStatus;
- DWORD dwOldCheckPoint;
- DWORD dwStartTickCount;
- DWORD dwWaitTime;
- DWORD dwBytesNeeded;
- // Get a handle to the SCM database.
- schSCManager = OpenSCManager(
- NULL, // local computer
- NULL, // servicesActive database
- SC_MANAGER_ALL_ACCESS); // full access rights
- if (NULL == schSCManager)
- {
- printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
- return;
- }
- // Get a handle to the service.
- schService = OpenService(
- schSCManager, // SCM database
- szSvcName, // name of service
- SERVICE_ALL_ACCESS); // full access
- if (schService == NULL)
- {
- printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schSCManager);
- return;
- }
- // Check the status in case the service is not stopped.
- if (!QueryServiceStatusEx(
- schService, // handle to service
- SC_STATUS_PROCESS_INFO, // information level
- (LPBYTE) &ssStatus, // address of structure
- sizeof(SERVICE_STATUS_PROCESS), // size of structure
- &dwBytesNeeded ) ) // size needed if buffer is too small
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- // Check if the service is already running. It would be possible
- // to stop the service here, but for simplicity this example just returns.
- if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
- {
- printf("Cannot start the service because it is already running\n");
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- // Save the tick count and initial checkpoint.
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- // Wait for the service to stop before attempting to start it.
- while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
- {
- // Do not wait longer than the wait hint. A good interval is
- // one-tenth of the wait hint but not less than 1 second
- // and not more than 10 seconds.
- dwWaitTime = ssStatus.dwWaitHint / 10;
- if( dwWaitTime < 1000 )
- dwWaitTime = 1000;
- else if ( dwWaitTime > 10000 )
- dwWaitTime = 10000;
- Sleep( dwWaitTime );
- // Check the status until the service is no longer stop pending.
- if (!QueryServiceStatusEx(
- schService, // handle to service
- SC_STATUS_PROCESS_INFO, // information level
- (LPBYTE) &ssStatus, // address of structure
- sizeof(SERVICE_STATUS_PROCESS), // size of structure
- &dwBytesNeeded ) ) // size needed if buffer is too small
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
- {
- // Continue to wait and check.
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- }
- else
- {
- if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
- {
- printf("Timeout waiting for service to stop\n");
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- }
- }
- // Attempt to start the service.
- if (!StartService(
- schService, // handle to service
- 0, // number of arguments
- NULL) ) // no arguments
- {
- printf("StartService failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- else printf("Service start pending...\n");
- // Check the status until the service is no longer start pending.
- if (!QueryServiceStatusEx(
- schService, // handle to service
- SC_STATUS_PROCESS_INFO, // info level
- (LPBYTE) &ssStatus, // address of structure
- sizeof(SERVICE_STATUS_PROCESS), // size of structure
- &dwBytesNeeded ) ) // if buffer too small
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return;
- }
- // Save the tick count and initial checkpoint.
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
- {
- // Do not wait longer than the wait hint. A good interval is
- // one-tenth the wait hint, but no less than 1 second and no
- // more than 10 seconds.
- dwWaitTime = ssStatus.dwWaitHint / 10;
- if( dwWaitTime < 1000 )
- dwWaitTime = 1000;
- else if ( dwWaitTime > 10000 )
- dwWaitTime = 10000;
- Sleep( dwWaitTime );
- // Check the status again.
- if (!QueryServiceStatusEx(
- schService, // handle to service
- SC_STATUS_PROCESS_INFO, // info level
- (LPBYTE) &ssStatus, // address of structure
- sizeof(SERVICE_STATUS_PROCESS), // size of structure
- &dwBytesNeeded ) ) // if buffer too small
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- break;
- }
- if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
- {
- // Continue to wait and check.
- dwStartTickCount = GetTickCount();
- dwOldCheckPoint = ssStatus.dwCheckPoint;
- }
- else
- {
- if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
- {
- // No progress made within the wait hint.
- break;
- }
- }
- }
- // Determine whether the service is running.
- if (ssStatus.dwCurrentState == SERVICE_RUNNING)
- {
- printf("Service started successfully.\n");
- }
- else
- {
- printf("Service not started. \n");
- printf(" Current State: %d\n", (unsigned int)ssStatus.dwCurrentState);
- printf(" Exit Code: %d\n", (unsigned int)ssStatus.dwWin32ExitCode);
- printf(" Check Point: %d\n", (unsigned int)ssStatus.dwCheckPoint);
- printf(" Wait Hint: %d\n", (unsigned int)ssStatus.dwWaitHint);
- }
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- }
- VOID __stdcall DoStopSvc()
- {
- SERVICE_STATUS_PROCESS ssp;
- DWORD dwStartTime = GetTickCount();
- DWORD dwBytesNeeded;
- DWORD dwTimeout = 30000; // 30-second time-out
- DWORD dwWaitTime;
- // Get a handle to the SCM database.
- schSCManager = OpenSCManager(
- NULL, // local computer
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access rights
- if (NULL == schSCManager)
- {
- printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
- return;
- }
- // Get a handle to the service.
- schService = OpenService(
- schSCManager, // SCM database
- szSvcName, // name of service
- SERVICE_STOP |
- SERVICE_QUERY_STATUS |
- SERVICE_ENUMERATE_DEPENDENTS);
- if (schService == NULL)
- {
- printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schSCManager);
- return;
- }
- // Make sure the service is not already stopped.
- if ( !QueryServiceStatusEx(
- schService,
- SC_STATUS_PROCESS_INFO,
- (LPBYTE)&ssp,
- sizeof(SERVICE_STATUS_PROCESS),
- &dwBytesNeeded ) )
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- goto stop_cleanup;
- }
- if ( ssp.dwCurrentState == SERVICE_STOPPED )
- {
- printf("Service is already stopped.\n");
- goto stop_cleanup;
- }
- // If a stop is pending, wait for it.
- while ( ssp.dwCurrentState == SERVICE_STOP_PENDING )
- {
- printf("Service stop pending...\n");
- // Do not wait longer than the wait hint. A good interval is
- // one-tenth of the wait hint but not less than 1 second
- // and not more than 10 seconds.
- dwWaitTime = ssp.dwWaitHint / 10;
- if( dwWaitTime < 1000 )
- dwWaitTime = 1000;
- else if ( dwWaitTime > 10000 )
- dwWaitTime = 10000;
- Sleep( dwWaitTime );
- if ( !QueryServiceStatusEx(
- schService,
- SC_STATUS_PROCESS_INFO,
- (LPBYTE)&ssp,
- sizeof(SERVICE_STATUS_PROCESS),
- &dwBytesNeeded ) )
- {
- printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
- goto stop_cleanup;
- }
- if ( ssp.dwCurrentState == SERVICE_STOPPED )
- {
- printf("Service stopped successfully.\n");
- goto stop_cleanup;
- }
- if ( GetTickCount() - dwStartTime > dwTimeout )
- {
- printf("Service stop timed out.\n");
- goto stop_cleanup;
- }
- }
- // If the service is running, dependencies must be stopped first.
- // StopDependentServices();
- // Send a stop code to the service.
- if ( !ControlService(
- schService,
- SERVICE_CONTROL_STOP,
- (LPSERVICE_STATUS) &ssp ) )
- {
- printf( "ControlService failed (%d)\n", (unsigned int)GetLastError() );
- goto stop_cleanup;
- }
- // Wait for the service to stop.
- while ( ssp.dwCurrentState != SERVICE_STOPPED )
- {
- Sleep( ssp.dwWaitHint );
- if ( !QueryServiceStatusEx(
- schService,
- SC_STATUS_PROCESS_INFO,
- (LPBYTE)&ssp,
- sizeof(SERVICE_STATUS_PROCESS),
- &dwBytesNeeded ) )
- {
- printf( "QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError() );
- goto stop_cleanup;
- }
- if ( ssp.dwCurrentState == SERVICE_STOPPED )
- break;
- if ( GetTickCount() - dwStartTime > dwTimeout )
- {
- printf( "Wait timed out\n" );
- goto stop_cleanup;
- }
- }
- printf("Service stopped successfully\n");
- stop_cleanup:
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- }
- void __stdcall DoDeleteSvc()
- {
- SC_HANDLE schSCManager;
- SC_HANDLE schService;
- //SERVICE_STATUS ssStatus;
- // Get a handle to the SCM database.
- schSCManager = OpenSCManager(
- NULL, // local computer
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access rights
- if (NULL == schSCManager)
- {
- printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
- return;
- }
- // Get a handle to the service.
- schService = OpenService(
- schSCManager, // SCM database
- szSvcName, // name of service
- DELETE); // need delete access
- if (schService == NULL)
- {
- printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
- CloseServiceHandle(schSCManager);
- return;
- }
- // Delete the service.
- if (! DeleteService(schService) )
- {
- printf("DeleteService failed (%d)\n", (unsigned int)GetLastError());
- }
- else printf("Service deleted successfully\n");
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- }
- void LoadService()
- {
- DoCreateSvc();
- DoStartSvc();
- }
- void UnloadService()
- {
- DoStopSvc();
- DoDeleteSvc();
- }
- int main(int argc, char *argv[])
- {
- if(argc != 3)
- {
- PrintUsage(argv[0]);
- return 1;
- }
- szSvcName = argv[2];
- if(!strcmp(argv[1], "load"))
- {
- LoadService();
- }
- else if (!strcmp(argv[1], "unload"))
- {
- UnloadService();
- }
- else
- {
- PrintUsage(argv[0]);
- return 2;
- }
- return 0;
- }
- void PrintUsage(char *program)
- {
- printf("Usage: %s < load | unload > < driver path >\n", program);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement