/install/xbt/linux/misc/windows/nt_service.cpp

http://torrentpier2.googlecode.com/ · C++ · 67 lines · 64 code · 3 blank · 0 comment · 6 complexity · c1dbee30b2ef0743e3fc4baf833a9161 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "nt_service.h"
  3. #include <windows.h>
  4. int nt_service_install(const char* name)
  5. {
  6. SC_HANDLE scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  7. if (!scm)
  8. return 1;
  9. char file_name[MAX_PATH];
  10. GetModuleFileName(NULL, file_name, MAX_PATH);
  11. SC_HANDLE service = CreateService(scm,
  12. name,
  13. name,
  14. SERVICE_ALL_ACCESS,
  15. SERVICE_WIN32_OWN_PROCESS,
  16. SERVICE_AUTO_START,
  17. SERVICE_ERROR_NORMAL,
  18. file_name,
  19. NULL,
  20. NULL,
  21. NULL,
  22. "NT AUTHORITY\\LocalService",
  23. NULL);
  24. if (!service)
  25. {
  26. service = CreateService(scm,
  27. name,
  28. name,
  29. SERVICE_ALL_ACCESS,
  30. SERVICE_WIN32_OWN_PROCESS,
  31. SERVICE_AUTO_START,
  32. SERVICE_ERROR_NORMAL,
  33. file_name,
  34. NULL,
  35. NULL,
  36. NULL,
  37. NULL,
  38. NULL);
  39. }
  40. if (!service)
  41. {
  42. CloseServiceHandle(scm);
  43. return 1;
  44. }
  45. CloseServiceHandle(service);
  46. CloseServiceHandle(scm);
  47. return 0;
  48. }
  49. int nt_service_uninstall(const char* name)
  50. {
  51. SC_HANDLE scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  52. if (!scm)
  53. return 1;
  54. int result = 1;
  55. SC_HANDLE service = OpenService(scm, name, DELETE);
  56. if (service)
  57. {
  58. if (DeleteService(service))
  59. result = 0;
  60. CloseServiceHandle(service);
  61. }
  62. CloseServiceHandle(scm);
  63. return result;
  64. }