Index: /trunk/include/VBox/log.h
===================================================================
--- /trunk/include/VBox/log.h	(revision 71138)
+++ /trunk/include/VBox/log.h	(revision 71139)
@@ -650,4 +650,6 @@
     /** Main group, IVirtualBoxClient. */
     LOG_GROUP_MAIN_VIRTUALBOXCLIENT,
+    /** Main group, IVirtualBoxClientList. */
+    LOG_GROUP_MAIN_VIRTUALBOXCLIENTLIST,
     /** Main group, IVirtualBoxSDS. */
     LOG_GROUP_MAIN_VIRTUALBOXSDS,
@@ -1128,4 +1130,5 @@
     "MAIN_VIRTUALBOX", \
     "MAIN_VIRTUALBOXCLIENT", \
+    "MAIN_VIRTUALBOXCLIENTLIST", \
     "MAIN_VIRTUALBOXSDS", \
     "MAIN_VIRTUALSYSTEMDESCRIPTION", \
Index: /trunk/src/VBox/Main/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Main/Makefile.kmk	(revision 71138)
+++ /trunk/src/VBox/Main/Makefile.kmk	(revision 71139)
@@ -297,4 +297,5 @@
  	src-all/VirtualBoxBase.cpp \
  	src-all/VirtualBoxErrorInfoImpl.cpp \
+    src-all/VirtualBoxClientListImpl.cpp \
  	src-global/VirtualBoxSDSImpl.cpp
  VBoxSDS_SOURCES.win = \
@@ -1201,5 +1202,6 @@
  VBoxProxyStub_SDKS     = VBOX_NTDLL
  VBoxProxyStub_DEFPATH  = $(VBoxCOM_0_OUTDIR)/# Hack to workaround drive letter in $(VBoxCOM_0_OUTDIR)/ for CFLAGS hack below.
- VBoxProxyStub_INCS     = $(VBoxCOM_0_OUTDIR)/
+ VBoxProxyStub_INCS     = $(VBoxCOM_0_OUTDIR)/ \
+    $(VBOX_PATH_MAIN_SRC)/include/ 
  VBoxProxyStub_SOURCES := \
  	VirtualBox_p.c \
@@ -1207,5 +1209,6 @@
  	$(PATH_SUB_CURRENT)/src-all/win/VBoxProxyStub.def \
  	$(PATH_SUB_CURRENT)/src-all/win/VBoxProxyStub.rc \
- 	$(PATH_SUB_CURRENT)/src-all/win/VBoxProxyStub.c
+    $(PATH_SUB_CURRENT)/src-all/win/RpcChannelHook.cpp \
+    $(PATH_SUB_CURRENT)/src-all/win/VBoxProxyStub.c
  src-all/win/VBoxProxyStub.rc_DEPS = $(VBoxCOM_0_OUTDIR)/VirtualBox.tlb
  VBoxProxyStub_VirtualBox_p.c_CFLAGS.x86 = /Gz # Workaround for incorrect ObjectStublessClient* prototypes in SDK v7.1.
@@ -1215,5 +1218,6 @@
  VBoxProxyStubLegacy_EXTENDS  = VBoxProxyStub
  VBoxProxyStubLegacy_DEFS     = $(VBoxProxyStub_DEFS) VBOX_IN_PROXY_STUB_LEGACY
- VBoxProxyStubLegacy_INCS     = $(VBoxCOM_0_OUTDIR)/legacy/
+ VBoxProxyStubLegacy_INCS     = $(VBoxCOM_0_OUTDIR)/legacy/ \
+    $(VBOX_PATH_MAIN_SRC)/include/ 
  VBoxProxyStubLegacy_SOURCES  = \
  	$(VBoxCOM_0_OUTDIR)/legacy/VirtualBox_p.c \
@@ -1221,5 +1225,6 @@
  	src-all/win/VBoxProxyStub.def \
  	src-all/win/VBoxProxyStubLegacy.rc \
- 	src-all/win/VBoxProxyStub.c
+    src-all/win/RpcChannelHook.cpp \
+  	src-all/win/VBoxProxyStub.c
  src-all/win/VBoxProxyStubLegacy.rc_DEPS = $(VBoxCOM_0_OUTDIR)/legacy/VirtualBox.tlb
 
@@ -1229,5 +1234,6 @@
   VBoxProxyStub-x86_SDKS     = VBOX_NTDLL
   VBoxProxyStub-x86_DEFS     = $(VBoxProxyStub_DEFS) VBOX_PROXY_STUB_32_ON_64 WIN32
-  VBoxProxyStub-x86_INCS     = $(VBoxCOM-x86_0_OUTDIR)/
+  VBoxProxyStub-x86_INCS     = $(VBoxCOM-x86_0_OUTDIR)/ \
+    $(VBOX_PATH_MAIN_SRC)/include/ 
   VBoxProxyStub-x86_SOURCES  = \
   	$(VBoxCOM-x86_0_OUTDIR)/VirtualBox_p.c \
@@ -1235,4 +1241,5 @@
  	src-all/win/VBoxProxyStub.c \
  	src-all/win/VBoxProxyStub-x86.rc \
+    src-all/win/RpcChannelHook.cpp \
  	src-all/win/VBoxProxyStub.def
   src-all/win/VBoxProxyStub-x86.rc_DEPS = $(VBoxCOM-x86_0_OUTDIR)/VirtualBox-x86.tlb
Index: /trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 71138)
+++ /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 71139)
@@ -24481,4 +24481,8 @@
       </method>
 
+      <method name="notifyClientsFinished">
+          <desc>Notify service that their clients finished.</desc>
+      </method>
+
   </interface>
 
@@ -24530,5 +24534,37 @@
       </method>
 
+      <method name="notifyClientsFinished">
+          <desc>Notify SDS that clients finished.</desc>
+      </method>
+
   </interface>
+
+    <!--
+  // IVirtualBoxClientList
+  //////////////////////////////////////////////////////////////////////////
+  -->
+
+    <interface
+      name="IVirtualBoxClientList" extends="$unknown"
+      uuid="6e7f09c9-da13-4126-9cc8-9aab81fed413"
+      wsmap="managed"
+      reservedMethods="2" reservedAttributes="2"
+    >
+        <desc>
+            The IVirtualBoxClientList interface represents a list of VirtualBox API clients.
+        </desc>
+
+        <method name="registerClient">
+            <desc>Register VirtualBox API Client.</desc>
+            <param name="pid" type="long" dir="in">
+                <desc>Process ID of VirtualBox API client.</desc>
+            </param>
+        </method>
+
+        <attribute name="clients" readonly="yes" type="long" dir="out" safearray="yes">
+            <desc>List of registered VirtualBox API clients.</desc>
+        </attribute>
+
+    </interface>
 
 </if> <!-- bird: shouldn't this if-element at leat include the VBoxSDS module, if not the whole system service application? -->
@@ -24544,8 +24580,11 @@
      -->
   <module name="VBoxSDS" context="LocalService">
-    <class name="VirtualBoxSDS" uuid="74ab5ffe-8726-4435-aa7e-876d705bcba5"
-           namespace="virtualbox.org">
-      <interface name="IVirtualBoxSDS" default="yes"/>
-    </class>
+      <class name="VirtualBoxSDS" uuid="74ab5ffe-8726-4435-aa7e-876d705bcba5"
+             namespace="virtualbox.org">
+          <interface name="IVirtualBoxSDS" default="yes"/>
+      </class>
+      <class name="VirtualBoxClientList" uuid="b2313c29-07eb-4613-ab27-f86bda7a0cae" namespace="virtualbox.org" >
+          <interface name="IVirtualBoxClientList" default="yes" />
+      </class>
   </module>
 
Index: /trunk/src/VBox/Main/include/VirtualBoxSDSImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxSDSImpl.h	(revision 71138)
+++ /trunk/src/VBox/Main/include/VirtualBoxSDSImpl.h	(revision 71139)
@@ -74,4 +74,5 @@
     STDMETHODIMP_(HRESULT) RegisterVBoxSVC(IVBoxSVCRegistration *aVBoxSVC, LONG aPid, IUnknown **aExistingVirtualBox);
     STDMETHODIMP_(HRESULT) DeregisterVBoxSVC(IVBoxSVCRegistration *aVBoxSVC, LONG aPid);
+    STDMETHODIMP_(HRESULT) NotifyClientsFinished();
 
 
Index: /trunk/src/VBox/Main/src-all/win/VBoxProxyStub.c
===================================================================
--- /trunk/src/VBox/Main/src-all/win/VBoxProxyStub.c	(revision 71138)
+++ /trunk/src/VBox/Main/src-all/win/VBoxProxyStub.c	(revision 71139)
@@ -41,4 +41,5 @@
 #include <iprt/uuid.h>
 #include <iprt/utf16.h>
+#include "RpcChannelHook.h"
 
 
@@ -147,4 +148,16 @@
 };
 
+BOOL IsVBoxServiceProcess(void)
+{
+    if (GetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", NULL, 0) == 0)
+    {
+        int res = GetLastError();
+        if (res != ERROR_ENVVAR_NOT_FOUND)
+            LogRel(("Error: cannot get service environment variable: %Rrwa\n", res));
+        return false;
+    }
+    return true;
+}
+
 
 /**
@@ -171,4 +184,9 @@
             RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
             Log12(("VBoxProxyStub[%u]/DllMain: DLL_PROCESS_ATTACH\n", GetCurrentProcessId()));
+
+            /* Install RPC channel hook to intercept a moment just after VirtualBox object activation.
+               It's reports to VBoxSDS that a new VirtualBox API client started. */
+            if(!IsVBoxServiceProcess())
+                SetupClientRpcChannelHook();
 
 #ifdef VBOX_STRICT
@@ -1346,4 +1364,9 @@
     VbpsRegisterClassId(pState, &CLSID_VirtualBoxSDS, "VirtualBoxSDS Class", pszSdsAppId, "VirtualBox.VirtualBoxSDS", ".1",
                         &LIBID_VirtualBox, "LocalServer32", pwszVBoxDir, pszSdsExe, NULL /*N/A*/);
+
+    VbpsRegisterClassName(pState, "VirtualBox.VirtualBoxClientList.1", "VirtualBoxClientList Class", &CLSID_VirtualBoxClientList, NULL);
+    VbpsRegisterClassName(pState, "VirtualBox.VirtualBoxClientList", "VirtualBoxClientList Class", &CLSID_VirtualBoxClientList, ".1");
+    VbpsRegisterClassId(pState, &CLSID_VirtualBoxClientList, "VirtualBoxClientList Class", pszSdsAppId, "VirtualBox.VirtualBoxClientList", ".1",
+        &LIBID_VirtualBox, "LocalServer32", pwszVBoxDir, pszSdsExe, NULL /*N/A*/);
 #endif
 }
Index: /trunk/src/VBox/Main/src-global/VirtualBoxSDSImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-global/VirtualBoxSDSImpl.cpp	(revision 71138)
+++ /trunk/src/VBox/Main/src-global/VirtualBoxSDSImpl.cpp	(revision 71139)
@@ -53,4 +53,5 @@
      * This is NULL if not set. */
     ComPtr<IVBoxSVCRegistration>    m_ptrTheChosenOne;
+    ComPtr<IVirtualBoxClientList>   m_ptrClientList;
 private:
     /** Reference count to make destruction safe wrt hung callers.
@@ -178,4 +179,6 @@
                         LogRel(("VirtualBoxSDS::registerVBoxSVC: Seems VBoxSVC instance died.  Dropping it and letting caller take over.\n"));
                         pUserData->m_ptrTheChosenOne.setNull();
+                        /* Release the client list and stop client list watcher thread*/
+                        pUserData->m_ptrClientList.setNull();
                     }
                 }
@@ -190,4 +193,19 @@
                     {
                         pUserData->m_ptrTheChosenOne = aVBoxSVC;
+                        /*
+                        * Create instance of ClientList
+                        */
+                        HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxClientList, NULL, CLSCTX_LOCAL_SERVER,
+                            IID_IVirtualBoxClientList,
+                            (void **)pUserData->m_ptrClientList.asOutParam());
+                        if (SUCCEEDED(hrc))
+                        {
+                            LogFunc(("Created API client list instance in VBoxSDS : hr=%Rhrf\n", hrc));
+                        }
+                        else
+                        {
+                            LogFunc(("Error in creating API client list instance: hr=%Rhrf\n", hrc));
+                        }
+
                         hrc = S_OK;
                     }
@@ -235,4 +253,6 @@
                     pUserData->m_ptrTheChosenOne.setNull();
                     /** @todo consider evicting the user from the table...   */
+                    /* Release the client list and stop client list watcher thread*/
+                    pUserData->m_ptrClientList.setNull();
                 }
                 else
@@ -260,4 +280,31 @@
 }
 
+
+STDMETHODIMP_(HRESULT) VirtualBoxSDS::NotifyClientsFinished()
+{
+    LogRelFlowThisFuncEnter();
+
+    int vrc = RTCritSectRwEnterShared(&m_MapCritSect);
+    if (RT_SUCCESS(vrc))
+    {
+        for (UserDataMap_T::iterator it = m_UserDataMap.begin(); it != m_UserDataMap.end(); ++it)
+        {
+            VBoxSDSPerUserData *pUserData = it->second;
+            if (pUserData && pUserData->m_ptrTheChosenOne)
+            {
+                LogRelFunc(("Notify VBoxSVC that all clients finished\n"));
+                /* Notify VBoxSVC about finishing all API clients it should free references to VBoxSDS
+                   and clean up itself */
+                if (pUserData->m_ptrClientList)
+                    pUserData->m_ptrClientList.setNull();
+                pUserData->m_ptrTheChosenOne->NotifyClientsFinished();
+            }
+        }
+        RTCritSectRwLeaveShared(&m_MapCritSect);
+    }
+
+    LogRelFlowThisFuncLeave();
+    return S_OK;
+}
 
 // private methods
Index: /trunk/src/VBox/Main/src-global/win/VBoxSDS.cpp
===================================================================
--- /trunk/src/VBox/Main/src-global/win/VBoxSDS.cpp	(revision 71138)
+++ /trunk/src/VBox/Main/src-global/win/VBoxSDS.cpp	(revision 71139)
@@ -87,4 +87,5 @@
 
 #include "VirtualBoxSDSImpl.h"
+#include "VirtualBoxClientListImpl.h"
 #include "Logging.h"
 
@@ -694,4 +695,16 @@
 
 
+int SetServiceEnvFlag()
+{
+    int rc = VINF_SUCCESS;
+    if (!SetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", L""))
+    {
+        rc = RTErrConvertFromWin32(GetLastError());
+        LogRel(("Error: cannot set service environment flag:  %Rrs\n", rc));
+    }
+    return rc;
+}
+
+
 /**
  * Main function for the VBoxSDS process.
@@ -715,4 +728,6 @@
      */
     RTR3InitExe(argc, &argv, 0);
+
+    SetServiceEnvFlag();
 
     static const RTGETOPTDEF s_aOptions[] =
@@ -922,4 +937,5 @@
             BEGIN_OBJECT_MAP(s_aObjectMap)
                 OBJECT_ENTRY(CLSID_VirtualBoxSDS, VirtualBoxSDS)
+                OBJECT_ENTRY(CLSID_VirtualBoxClientList, VirtualBoxClientList)
             END_OBJECT_MAP()
             hrcExit = pServiceModule->init(s_aObjectMap, hInstance, &LIBID_VirtualBox,
Index: /trunk/src/VBox/Main/src-server/win/svcmain.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/win/svcmain.cpp	(revision 71138)
+++ /trunk/src/VBox/Main/src-server/win/svcmain.cpp	(revision 71139)
@@ -28,4 +28,5 @@
 #include "VBox/com/com.h"
 #include "VBox/com/VirtualBox.h"
+#include "VBox/com/array.h"
 
 #include "VirtualBoxImpl.h"
@@ -44,4 +45,5 @@
 #include <iprt/asm.h>
 
+#include <TlHelp32.h>
 
 /*********************************************************************************************************************************
@@ -94,4 +96,122 @@
 volatile uint32_t dwTimeOut = dwNormalTimeout; /* time for EXE to be idle before shutting down. Can be decreased at system shutdown phase. */
 
+
+BOOL CALLBACK CloseWindowProc(_In_ HWND   hWnd, _In_ LPARAM /* lParam */)
+{
+    _ASSERTE(hWnd);
+    DWORD_PTR dwResult;
+    // Close topmost windows in the thread
+    LRESULT lResult = SendMessageTimeout(hWnd, WM_CLOSE, NULL, NULL,
+        SMTO_ABORTIFHUNG | SMTO_BLOCK, 0, &dwResult);
+    if (lResult != 0)
+    {
+        LogRel(("EnumThreadWndProc: Close message sent to window %x successfully \n", hWnd));
+    }
+    else
+    {
+        LogRel(("EnumThreadWndProc: Cannot send event to window %x. result: %d, last error: %x\n",
+            hWnd, dwResult, GetLastError()));
+    }
+    return TRUE;
+}
+
+void SendCloseToAllThreads(DWORD dwTargetPid)
+{
+    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+    if (hSnapshot == NULL)
+    {
+        LogRel(("SendCloseToAllThreads: cannot get threads snapshot. error: 0x%x \n",
+            GetLastError()));
+        return;
+    }
+
+    THREADENTRY32 threadEntry;
+    ZeroMemory(&threadEntry, sizeof(threadEntry));
+    threadEntry.dwSize = sizeof(threadEntry);
+
+    if (Thread32First(hSnapshot, &threadEntry))
+    {
+        do
+        {
+            LogRel(("SendCloseToAllThreads: process: %d thread: %x \n",
+                threadEntry.th32OwnerProcessID, threadEntry.th32ThreadID));
+            if (threadEntry.th32OwnerProcessID == dwTargetPid)
+            {
+                BOOL bRes = EnumThreadWindows(threadEntry.th32ThreadID, CloseWindowProc, NULL);
+                if (!bRes)
+                {
+                    LogRel(("SendCloseToAllThreads: EnumThreadWindows() failed to enumerate threads. error: %x \n",
+                        GetLastError()));
+                }
+                else
+                {
+                    LogRel(("SendCloseToAllThreads: about to close window in thread %x of process d\n",
+                        threadEntry.th32ThreadID, dwTargetPid));
+                }
+            }
+        } while (Thread32Next(hSnapshot, &threadEntry));
+    }
+    CloseHandle(hSnapshot);
+}
+
+static int CloseActiveClients()
+{
+    ComPtr<IVirtualBoxClientList> ptrClientList;
+    /*
+    * Connect to VBoxSDS.
+    */
+    // TODO: here we close all API client processes: our own and customers
+    LogRelFunc(("Forcibly close API clients during system shutdown on Windows 7:\n"));
+    HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxClientList, NULL, CLSCTX_LOCAL_SERVER, IID_IVirtualBoxClientList,
+        (void **)ptrClientList.asOutParam());
+    if (SUCCEEDED(hrc))
+    {
+        com::SafeArray<LONG> aCllients;
+        hrc = ptrClientList->get_Clients(aCllients.__asOutParam());
+        RTCList<LONG> clientsList = aCllients.toList();
+        LogRel(("==========Client list begin ========\n"));
+        for (int i = 0; i < clientsList.size(); i++)
+        {
+            LogRel(("About to close client pid: %d\n", clientsList[i]));
+            SendCloseToAllThreads(clientsList[i]);
+        }
+        LogRel(("==========Client list end ========\n"));
+    }
+    else
+    {
+        LogFunc(("Error to connect to VBoxSDS: hr=%Rhrf\n", hrc));
+    }
+    return 0;
+}
+
+// These are copies of functions defined in VersionHelpers.h
+bool IsWindowsVersionOrGreaterWrap(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
+{
+    OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0 };
+    DWORDLONG        const dwlConditionMask = VerSetConditionMask(
+        VerSetConditionMask(
+            VerSetConditionMask(
+                0, VER_MAJORVERSION, VER_GREATER_EQUAL),
+            VER_MINORVERSION, VER_GREATER_EQUAL),
+        VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
+
+    osvi.dwMajorVersion = wMajorVersion;
+    osvi.dwMinorVersion = wMinorVersion;
+    osvi.wServicePackMajor = wServicePackMajor;
+
+    return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
+}
+
+
+#if !defined _WIN32_WINNT_WIN8
+
+#define _WIN32_WINNT_WIN8                   0x0602
+
+#endif  // #if !defined _WIN32_WINNT_WIN8
+
+bool IsWindows8OrGreaterWrap()
+{
+    return IsWindowsVersionOrGreaterWrap(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
+}
 
 
@@ -241,4 +361,5 @@
     HRESULT VirtualBoxClassFactory::i_registerWithSds(IUnknown **ppOtherVirtualBox);
     void    VirtualBoxClassFactory::i_deregisterWithSds(void);
+    void    VirtualBoxClassFactory::i_finishVBoxSvc();
 
     friend VBoxSVCRegistration;
@@ -311,4 +432,19 @@
             return m_pFactory->i_getVirtualBox(ppResult);
         return E_FAIL;
+    }
+
+    // IVBoxSVCRegistration: called from
+    STDMETHOD(NotifyClientsFinished)()
+    {
+        LogRelFunc(("All clients gone - shutdown sequence initiated\n"));
+
+        m_pFactory->i_finishVBoxSvc();
+
+        // This is not enough to finish VBoxSvc such as reference to crashed client still is in action
+        // So I forcebly shutdown VBoxSvc
+        while (g_pModule->Unlock() > 0)
+        {};
+
+        return S_OK;
     }
 };
@@ -354,13 +490,6 @@
             NOREF(hrc);
         }
-        m_ptrVirtualBoxSDS.setNull();
-        g_fRegisteredWithVBoxSDS = false;
-    }
-    if (m_pVBoxSVC)
-    {
-        m_pVBoxSVC->m_pFactory = NULL;
-        m_pVBoxSVC->Release();
-        m_pVBoxSVC = NULL;
-    }
+    }
+    i_finishVBoxSvc();
 }
 
@@ -381,4 +510,21 @@
     Log(("VirtualBoxClassFactory::GetVirtualBox: E_FAIL\n"));
     return E_FAIL;
+}
+
+
+void    VirtualBoxClassFactory::i_finishVBoxSvc()
+{
+    LogRelFunc(("Finish work of VBoxSVc and VBoxSDS\n"));
+    if (m_ptrVirtualBoxSDS.isNotNull())
+    {
+        m_ptrVirtualBoxSDS.setNull();
+        g_fRegisteredWithVBoxSDS = false;
+    }
+    if (m_pVBoxSVC)
+    {
+        m_pVBoxSVC->m_pFactory = NULL;
+        m_pVBoxSVC->Release();
+        m_pVBoxSVC = NULL;
+    }
 }
 
@@ -536,4 +682,12 @@
                     Log(("VBoxSVCWinMain: WM_QUERYENDSESSION: VBoxSvc has active connections. bActivity = %d. Loc count = %d\n",
                          g_pModule->bActivity, g_pModule->GetLockCount()));
+
+                    // On Windows 7 our clients doesn't receive right sequence of Session End events
+                    // So we send them all WM_QUIT to forcible close them.
+                    // Windows 10 sends end session events correctly
+                    // Note: the IsWindows8Point1() and IsWindows10OrGreater() doesnt work in
+                    // application without manifest so I use old compatible functions for detection of Win 7
+                    if(!IsWindows8OrGreaterWrap())
+                        CloseActiveClients();
                 }
                 rc = !fActiveConnection;
@@ -629,4 +783,16 @@
         }
     }
+}
+
+
+int SetServiceEnvFlag()
+{
+    int rc = VINF_SUCCESS;
+    if (!SetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", L""))
+    {
+        rc = RTErrConvertFromWin32(GetLastError());
+        LogRel(("Error: cannot set service environment flag:  %Rrs\n", rc));
+    }
+    return rc;
 }
 
@@ -667,4 +833,6 @@
      */
     RTR3InitExe(argc, &argv, 0);
+
+    SetServiceEnvFlag();
 
     static const RTGETOPTDEF s_aOptions[] =
@@ -873,5 +1041,5 @@
             if (RT_FAILURE(vrc))
             {
-                Log(("SVCMAIN: Failed to process Helper request (%Rrc).", vrc));
+                Log(("SVCMAIN: Failed to process Helper request (%Rrc).\n", vrc));
                 nRet = 1;
             }
