StartServiceCtrlDispatcher access denied on windows 7
access-denied, service, windows
Solution
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)`
Problem
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