How to create windows service for EXE??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

How to create windows service for EXE??

 
0
  #1
Jun 6th, 2008
Hi...
I want to create windows service for my abc.exe through programaticaly in c or c++
how to do this???
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to create windows service for EXE??

 
0
  #2
Jun 6th, 2008
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: How to create windows service for EXE??

 
0
  #3
Jun 6th, 2008
Not using any tool...
Programaticaly in c or c++
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to create windows service for EXE??

 
0
  #4
Jun 6th, 2008
So how many links did you actually read before giving up?
Because you spent at most 17 MINUTES before deciding that you needed to reply.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,847
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How to create windows service for EXE??

 
0
  #5
Jun 6th, 2008
I thought long and hard about how to solve this problem.
But after a while a decided to change Salem's searchstring ( " how to create a windows service ") to "how to create a windows service in c++" I got this:
http://clusty.com/search?input-form=clusty-simple&v%3Asources=webplus&query=how+to+create+a+windows+service+in+c%2B%2B


A lot of interesting search results.
Damn. That was hard
Last edited by niek_e; Jun 6th, 2008 at 4:36 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: How to create windows service for EXE??

 
0
  #6
Jun 6th, 2008
In my code it gives error like

  1. if (!StartService(
  2. schService,
  3. 0,
  4. NULL) )
  5. {
  6. printf("Hello");
  7. printf("\nStartService failed (%d)\n", GetLastError());
  8. CloseServiceHandle(schService);
  9. CloseServiceHandle(schSCManager);
  10. return;
  11. }

It gives error start service failed 1053
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,847
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How to create windows service for EXE??

 
0
  #7
Jun 6th, 2008
Post the code where you created the handle to the service (openservice() or createservice())
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: How to create windows service for EXE??

 
0
  #8
Jun 6th, 2008
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include<conio.h>
  4.  
  5. #define szSvcName TEXT("abc")
  6. #define SVCNAME TEXT("abc")
  7. SC_HANDLE schSCManager;
  8. SC_HANDLE schService;
  9. VOID DoStartSvc();
  10.  
  11. VOID SvcInstall()
  12. {
  13. SC_HANDLE schSCManager;
  14. SC_HANDLE schService;
  15. TCHAR szPath[MAX_PATH];
  16.  
  17. if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
  18. {
  19. printf("Cannot install service (%d)\n", GetLastError());
  20. return;
  21. }
  22.  
  23. schSCManager = OpenSCManager(
  24. NULL, NULL, SC_MANAGER_ALL_ACCESS);
  25.  
  26. if (NULL == schSCManager)
  27. {
  28. printf("OpenSCManager failed (%d)\n", GetLastError());
  29. return;
  30. }
  31.  
  32. schService = CreateService(
  33. schSCManager,
  34. SVCNAME,
  35. SVCNAME,
  36. SERVICE_ALL_ACCESS,
  37. SERVICE_WIN32_OWN_PROCESS,
  38. SERVICE_AUTO_START ,
  39. SERVICE_ERROR_NORMAL,
  40. szPath,
  41. NULL,
  42. NULL,
  43. NULL,
  44. NULL,
  45. NULL);
  46.  
  47. if (schService == NULL)
  48. {
  49. printf("CreateService failed (%d)\n", GetLastError());
  50. CloseServiceHandle(schSCManager);
  51. return;
  52. }
  53. else printf("Service installed successfully\n");
  54. DoStartSvc();
  55. CloseServiceHandle(schService);
  56. CloseServiceHandle(schSCManager);
  57. return;
  58. }
  59.  
  60. VOID DoStartSvc()
  61. {
  62. printf("\nIn DoStart");
  63. SERVICE_STATUS_PROCESS ssStatus;
  64. DWORD dwOldCheckPoint;
  65. DWORD dwStartTickCount;
  66. DWORD dwWaitTime;
  67. DWORD dwBytesNeeded;
  68.  
  69. schSCManager = OpenSCManager(
  70. NULL,
  71. NULL,
  72. SC_MANAGER_ALL_ACCESS);
  73.  
  74. if (NULL == schSCManager)
  75. {
  76. printf("OpenSCManager failed (%d)\n", GetLastError());
  77. return;
  78. }
  79.  
  80. printf("\nIn open service");
  81. schService = OpenService(
  82. schSCManager,
  83. szSvcName,
  84. SERVICE_ALL_ACCESS);
  85. if (schService == NULL)
  86. {
  87. printf("OpenService failed (%d)\n", GetLastError());
  88. printf("\nIn service Null");
  89. CloseServiceHandle(schSCManager);
  90. return;
  91. }
  92.  
  93. if (!QueryServiceStatusEx(
  94. schService,
  95. SC_STATUS_PROCESS_INFO,
  96. (LPBYTE) &ssStatus,
  97. sizeof(SERVICE_STATUS_PROCESS),
  98. &dwBytesNeeded ) )
  99. {
  100. printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
  101.  
  102. CloseServiceHandle(schService);
  103. CloseServiceHandle(schSCManager);
  104. return;
  105. }
  106.  
  107. if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
  108. {
  109. printf("Cannot start the service because it is already running\n");
  110. CloseServiceHandle(schService);
  111. CloseServiceHandle(schSCManager);
  112. return;
  113. }
  114.  
  115. while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
  116. {
  117. dwStartTickCount = GetTickCount();
  118. dwOldCheckPoint = ssStatus.dwCheckPoint;
  119.  
  120.  
  121. dwWaitTime = ssStatus.dwWaitHint / 10;
  122.  
  123. if( dwWaitTime < 1000 )
  124. dwWaitTime = 1000;
  125. else if ( dwWaitTime > 10000 )
  126. dwWaitTime = 10000;
  127.  
  128. Sleep( dwWaitTime );
  129.  
  130.  
  131. if (!QueryServiceStatusEx(
  132. schService,
  133. SC_STATUS_PROCESS_INFO,
  134. (LPBYTE) &ssStatus,
  135. sizeof(SERVICE_STATUS_PROCESS),
  136. &dwBytesNeeded ) )
  137. {
  138. printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
  139. CloseServiceHandle(schService);
  140. CloseServiceHandle(schSCManager);
  141. return;
  142. }
  143.  
  144. if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
  145. {
  146. dwStartTickCount = GetTickCount();
  147. dwOldCheckPoint = ssStatus.dwCheckPoint;
  148. }
  149. else
  150. {
  151. if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  152. {
  153. printf("Timeout waiting for service to stop\n");
  154. CloseServiceHandle(schService);
  155. CloseServiceHandle(schSCManager);
  156. return;
  157. }
  158. }
  159. }
  160.  
  161. if (!StartService(
  162. schService,
  163. 0,
  164. NULL) )
  165. {
  166. printf("Hello");
  167. printf("\nStartService failed (%d)\n", GetLastError());
  168. CloseServiceHandle(schService);
  169. CloseServiceHandle(schSCManager);
  170. return;
  171. }
  172. else printf("Service start pending...\n");
  173.  
  174.  
  175.  
  176. if (!QueryServiceStatusEx(
  177. schService,
  178. SC_STATUS_PROCESS_INFO,
  179. (LPBYTE) &ssStatus,
  180. sizeof(SERVICE_STATUS_PROCESS),
  181. &dwBytesNeeded ) )
  182. {
  183. printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
  184. CloseServiceHandle(schService);
  185. CloseServiceHandle(schSCManager);
  186. return;
  187. }
  188. dwStartTickCount = GetTickCount();
  189. dwOldCheckPoint = ssStatus.dwCheckPoint;
  190.  
  191. while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
  192. {
  193.  
  194.  
  195. dwWaitTime = ssStatus.dwWaitHint / 10;
  196.  
  197. if( dwWaitTime < 1000 )
  198. dwWaitTime = 1000;
  199. else if ( dwWaitTime > 10000 )
  200. dwWaitTime = 10000;
  201.  
  202. Sleep( dwWaitTime );
  203.  
  204. if (!QueryServiceStatusEx(
  205. schService,
  206. SC_STATUS_PROCESS_INFO,
  207. (LPBYTE) &ssStatus,
  208. sizeof(SERVICE_STATUS_PROCESS),
  209. &dwBytesNeeded ) )
  210. {
  211. printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
  212. break;
  213. }
  214.  
  215. if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
  216. {
  217. dwStartTickCount = GetTickCount();
  218. dwOldCheckPoint = ssStatus.dwCheckPoint;
  219. }
  220. else
  221. {
  222. if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  223. {
  224.  
  225. break;
  226. }
  227. }
  228. }
  229.  
  230. if (ssStatus.dwCurrentState == SERVICE_RUNNING)
  231. {
  232. printf("Service started successfully.\n");
  233. }
  234. else
  235. {
  236. printf("Service not started. \n");
  237. printf(" Current State: %d\n", ssStatus.dwCurrentState);
  238. printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
  239. printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
  240. printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
  241. }
  242.  
  243. CloseServiceHandle(schService);
  244. CloseServiceHandle(schSCManager);
  245. }
  246.  
  247.  
  248. int main()
  249. {
  250. SvcInstall();
  251.  
  252. getch();
  253. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: How to create windows service for EXE??

 
0
  #9
Jun 6th, 2008
Actually this code from msdn .. i m trying to execute this code for my exe
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,847
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How to create windows service for EXE??

 
0
  #10
Jun 6th, 2008
Originally Posted by Aamit View Post
Actually this code from msdn.
No way! I thought you made it yourself

Anyway: #define szSvcName TEXT("abc")
"abc" isn't really a service is it? So it can't be started.

These kind programs are quite difficult for a beginner. (your getch() gave you away)
I suggest you start of with some simpler programs...
Last edited by niek_e; Jun 6th, 2008 at 7:56 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC