1

I have written an application and using a service to start it. All in C++, on Windows 7. My query is similar to How can a Windows Service determine its ServiceName?

Is there a way to determine (in C++) in an executable at run time, the name of the service that started that executable.

Just need a pointer in the right direction.

Thanks

Community
  • 1
  • 1
ababeel
  • 435
  • 1
  • 8
  • 25
  • You have two different questions here: first, how to get the process that started an exe. Second, how to get the service name of a process. – Mahmoud Al-Qudsi May 22 '12 at 06:47
  • Only 1 question. When my service starts an exe, I want to know what is the name of the service that started that executable (inside the exe). – ababeel May 22 '12 at 07:40
  • *Like I said*, it's a two step process because first you need to identify the process that started your exe (aka the PID of your service), then you need to identify the name of the service associated with that process. – Mahmoud Al-Qudsi May 22 '12 at 07:42
  • Possible duplicate of http://stackoverflow.com/questions/1841790/how-can-a-windows-service-determine-its-servicename . See my answer there – vivek.m Jun 13 '12 at 13:41

1 Answers1

0

You could use EnumServicesStatus to get a list of all services then filter those, I guess the best way is to compare the binary name. Then call QueryServiceConfig to get the service configuration including the service name.

I hope this is what you're looking for. Of course to do this your application must be run as an administrator in Windows Vista and 7 or the call to OpenSCManager will definitely fail.

Edit: Another quick way is to either have the service name hard-coded in a def file shared between the service and the executable or in a separate configuration file, hence eliminating all the fuss of the SCManager

Zaid Amir
  • 4,727
  • 6
  • 52
  • 101