Is it possible to detect if the main application thread is running as a Windows Service without using a compilation directive?-Collection of common programming errors

Yes, it is. At runtime, you might check whether the process parent is services.exe or the current process is svchost.exe. Or you could query the service control manager using WinApi whether your service is started and the current process id is equal to the one of the started service.

This answer has some sample code in C#:

How do we tell if a C++ application is launched as a Windows service?