StartServiceCtrlDispatcher access denied on windows 7

4

I have a c++ project in visual studio 2008 on windows 7 where I try to start a new service. I'm running visual studio as administrator. I cant start the service (serviceMain is not even called).

this is my main function:

wchar_t str[] = {'s','e','s','m'};

int _tmain(int argc, _TCHAR* argv[])
{
    SERVICE_TABLE_ENTRY dispTable[] =
    {
        {(wchar_t*)str, ServiceWork::ServiceMain}, 
        {NULL, NULL}
    };

    int i = StartServiceCtrlDispatcher(dispTable);
    int j = GetLastError();
    return 0; 
}

the output is:

. . .

'SessionMonitor.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll'

'SessionMonitor.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'

'SessionMonitor.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'

First-chance exception at 0x7638b9bc in SessionMonitor.exe: 0x00000005: Access is denied. The thread 'Win32 Thread' (0x129c) has exited with code 0 (0x0). The program '[2492] SessionMonitor.exe: Native' has exited with code 0 (0x0).

on debug, j is 1063 - ERROR_FAILED_SERVICE_CONTROLLER_CONNECT

does anyone encountered this problem before? any solution?

thank you, Liron

windows
service
access-denied
asked on Stack Overflow Jul 25, 2012 by lironda

6 Answers

5

The problem lies in the fact that you are launching the service inside visual studio.

This cannot be done. You have to just compile the service with visual studio and then register it on the command prompt by using sc command (or programmarically as described here). All the correct way is described in the accepted answer of this question.

If you wish to debug the service code, you must issue the ServiceMain directly, for example:

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef AS_SERVICE
    SERVICE_TABLE_ENTRY dispTable[] =
    {
        {(wchar_t*)str, ServiceWork::ServiceMain}, 
        {NULL, NULL}
    };

    int i = StartServiceCtrlDispatcher(dispTable);
    int j = GetLastError();
    return 0;
#else
    ServiceMain(argc, argv);
#endif
}

The same problem can show up also when StartServiceCtrlDispatcher fails and GetLastError returns ERROR_FAILED_SERVICE_CONTROLLER_CONNECT (1063)

answered on Stack Overflow Oct 3, 2014 by Zac • edited May 23, 2017 by Community
2

If you're trying to start a Windows service from an IDE like Microsoft Visual Studio or from a command line, then you'll need to setup a ConsoleHandler and call ServiceStart manaully e.g.

SetConsoleCtrlHandler( myConsoleHandler, TRUE ); ServiceStart( argc, argv, TRUE );

In our app, we pass a -debug flag which tells the app to run as a console program not a Windows service.

answered on Stack Overflow Jan 24, 2014 by buzz3791
1

StartServiceCtrlDispatcher access denied on windows 7

I believe this is a bug in Windows. According to MSDN StartServiceCtrlDispatcher should return zero on failure, but someone at Microsoft thought it would be a great idea to throw a custom (non-C++) exception across API boundaries instead.

You can catch and ignore this special type of exception using AddVectoredExceptionHandler to workaround the problem:

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

LONG WINAPI handle_exception(EXCEPTION_POINTERS* exception_data)
{
  switch (exception_data->ExceptionRecord->ExceptionCode)
  {
  case 0x00000005:  // thrown by StartServiceCtrlDispatcher for fun.
    // Ignore these specific type of exceptions and continue execution.
    // Note: There are several more interesting exceptions to catch here,
    // which are out of scope of this question.
    return EXCEPTION_CONTINUE_SEARCH;

  case 0xE06D7363:  // C++ exception code.
  default:
    // Pass all other type of exceptions to their regular exception handlers.
    return EXCEPTION_EXECUTE_HANDLER;
  }
}

auto handle = AddVectoredExceptionHandler(1, &handle_exception);
// Your code here. Now you can check for the return value of
// StartServiceCtrlDispatcher to see whether the application
// was started as a service or not without crashing.
RemoveVectoredExceptionHandler(handle);
answered on Stack Overflow May 29, 2017 by Stacker
0

This is wrong:

wchar_t str[] = {'s','e','s','m'};

You've left out the terminating NUL. Use

wchar_t str[] = L"sesm";
answered on Stack Overflow Jul 25, 2012 by Harry Johnston
0

How are you starting the service?

Even with your user in the Administrators group, programs won't run fully elevated until you've been through UAC, or they're launched from an already-elevated context. If you're trying to debug through Visual Studio, you may need to right-click on Visual Studio and run it as Administrator for it to work.

If you want to be able to launch the service from Explorer you'll need to set the requestedExecutionLevel to 'level=requireAdministrator' in the manifest. Launching from Command Prompt will need the same, except if you're using 'net start yourservice', when the Command Prompt will need to be elevated. Launching from the system services plugin requires no special preparation, and has a hidden elevation boost for MS signed apps under Windows 7 (not Vista).

answered on Stack Overflow Aug 24, 2012 by simonobo • edited Aug 24, 2012 by simonobo
0

Once you finish the code, don't debug. Build it. When Build succeeds, SessionMonitor.exe file will be created inside Debug. Go to command prompt and install the service. sc create "sesm" binPath= "the location of your SessionMonitor.exe\SessionMonitor.exe"

The go to Run and type services.msc Find the service sesm, Run it, Check if what you have done in the ServiceMain works.

answered on Stack Overflow Feb 18, 2015 by Easwar Aiyer

User contributions licensed under CC BY-SA 3.0