Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. BOOL
  5. AddService(
  6. LPTSTR ServiceName,
  7. LPTSTR ImageName
  8. )
  9. {
  10. SC_HANDLE hService;
  11. SC_HANDLE hOldService;
  12. SERVICE_STATUS ServStat;
  13. if( !( hService = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ) ) ) {
  14. return FALSE;
  15. }
  16. if( hOldService = OpenService( hService, ServiceName, SERVICE_ALL_ACCESS ) ) {
  17. if( ! ControlService( hOldService, SERVICE_CONTROL_STOP, & ServStat ) ) {
  18. int fError = GetLastError();
  19. if( ( fError != ERROR_SERVICE_NOT_ACTIVE ) && ( fError != ERROR_INVALID_SERVICE_CONTROL ) ) {
  20. return FALSE;
  21. }
  22. }
  23. if( ! DeleteService( hOldService ) ) {
  24. return FALSE;
  25. }
  26. if( ! CloseServiceHandle( hOldService ) ) {
  27. return FALSE;
  28. }
  29. }
  30. if( ! CreateService( hService, ServiceName, ServiceName, SERVICE_ALL_ACCESS,
  31. SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
  32. SERVICE_ERROR_NORMAL, ImageName, NULL, NULL, NULL, NULL, NULL ) ) {
  33. int fError = GetLastError();
  34. if( fError != ERROR_SERVICE_EXISTS ) {
  35. return FALSE;
  36. }
  37. }
  38. return TRUE;
  39. }
  40. int _cdecl
  41. main(
  42. int argc,
  43. char *argvA[]
  44. )
  45. {
  46. if (!AddService( TEXT("Fax"), TEXT("faxsvc.exe") )) {
  47. _tprintf( TEXT("could not add service\n") );
  48. }
  49. return 0;
  50. }