Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys.
I have created an application that should be run as a service and as an application.
When I click and run it, It should run as an application. when called from service controller, It should detect being it and branch to run as a service. How do I detect this, without passing arguments?
mr.abzadeh
Edit: My application is a win32 application.
Edit: Question title changed because it wasn't clear. The previous title was: How to detect if a process is being installed as a service
Posted
Updated 9-Oct-15 20:23pm
v4

1 solution

You need to implement the OnStart and OnStop as you inherit from ServiceBase and also just implement a Main method. Have a look at the link for an example:
http://einaregilsson.com/run-windows-service-as-a-console-program/[^]

Good luck!
 
Share this answer
 
Comments
mr.abzadeh 8-Oct-15 9:37am    
The link you provided refers to .Net applications. My application is Win32, The tag was incorrect, So I corrected it.
E.F. Nijboer 8-Oct-15 9:51am    
I think the solution would be the same idea. To be a service you need to have a SvcMain. By adding a normal Main you also create a console application. You would need to compile it to an application (not a library).

https://msdn.microsoft.com/en-us/library/ms687414%28v=vs.85%29.aspx
mr.abzadeh 11-Oct-15 15:47pm    
This link is about services and has no Information about my question
E.F. Nijboer 11-Oct-15 16:44pm    
The idea is to simply create a service and also add a normal main method. Compile it to exe so it will be a valid program and can also be used as a service because it has the needed entry point.
E.F. Nijboer 12-Oct-15 3:47am    
This link might hopefully also give you some idea:

https://code.google.com/p/thesnow/source/browse/trunk/Code/CPP/tftpd32/_main/Svc+main.c

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900