Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StartServiceCtrlDispatcher access denied on windows 7

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

like image 546
lironda Avatar asked Jul 25 '12 12:07

lironda


3 Answers

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)

like image 122
Zac Avatar answered Nov 19 '22 03:11

Zac


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.

like image 40
buzz3791 Avatar answered Nov 19 '22 05:11

buzz3791


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);
like image 1
Stacker Avatar answered Nov 19 '22 05:11

Stacker