Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if the current process is a service or not (C++, Win32)?

Tags:

c++

winapi

How can I tell if the application my code is running in, is it in a service or an application? Why do I want to know this - I'm writing some code that is injected into the target application and that code has no way of knowing this information up front, so it has to work it out itself.

I cannot rely on any code being called from the service control manager, start, stop, or command line parameters.

I'm currently looking at GetConsoleWindow() which I hope will return NULL for a service (no console) and a window handle for any application (has a console). Not sure how valid this assumption is.

Any ideas for a better solution?

like image 282
Stephen Kellett Avatar asked Oct 25 '25 21:10

Stephen Kellett


1 Answers

Search the current process id (GetCurrentProcessId) from the list of all running services (EnumServicesStatusEx)?

like image 99
kbjorklu Avatar answered Oct 28 '25 10:10

kbjorklu