Access to StartServiceCtrlDispatcher is denied in Windows 7

I have a C ++ project in visual studio 2008 on Windows 7 where I am trying to launch a new service. I am running visual studio as an administrator. I can not 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; } 

output:

. ,.

'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 in 0x7638b9bc in SessionMonitor.exe: 0x00000005: access is denied. The thread "Win32 Thread" (0x129c) exited with code 0 (0x0). The program '[2492] SessionMonitor.exe: Native' exited with code 0 (0x0).

during debugging, j - 1063 - ERROR_FAILED_SERVICE_CONTROLLER_CONNECT

Has anyone encountered this problem before? any solution?

Thank you liron

+4
source share
6 answers

The problem is that you are running the service inside the visual studio.

It's impossible. You just need to compile the service with visual studio, and then register it on the command line using the sc command (or programmatically, as described here ). The whole correct path is described in the accepted answer of this question.

If you want to debug the service code, you must immediately send the ServiceMain, 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 occur if StartServiceCtrlDispatcher ERROR_FAILED_SERVICE_CONTROLLER_CONNECT (1063) and GetLastError returns ERROR_FAILED_SERVICE_CONTROLLER_CONNECT (1063)

+3
source

If you are trying to start a Windows service from an IDE such as Microsoft Visual Studio or from the command line, you need to configure the ConsoleHandler and call ServiceStart manaully, for example.

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

In our application, we pass the -debug flag, which tells the application that it runs as a console program, not a Windows service.

+2
source

Access to StartServiceCtrlDispatcher is denied in Windows 7

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

You can catch and ignore this particular type of exception using AddVectoredExceptionHandler to get around 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); 
+1
source

It is not right:

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

You missed the final NUL. Use

 wchar_t str[] = L"sesm"; 
0
source

How do you start the service?

Even if your user is in the Administrators group, the programs will not run completely until you go through the UAC, or they are launched from an already elevated context. If you are 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 start the service from Explorer, you need to set requestExecutionLevel to 'level = requireAdministrator' in the manifest. Starting from the command line will require the same thing, except when you use "net start yourservice" when you need to execute a command prompt. Starting from the plugin system services does not require special training and has a hidden uplift for applications signed by MS under Windows 7 (not Vista).

0
source

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

Go to Run and enter services.msc Find the sesm service, start it, check if what you did in ServiceMain works.

0
source

All Articles