C ++ Windows服务错误1063

时间:2013-06-29 05:34:04

标签: c++ service startup

尝试开发我的第一个Windows服务,我在Windows 7 MS VC ++ 10.0中进行调试。一旦它调用,StartServiceCtrlDispatcher(),我得到一个错误1063,它说Access被拒绝。我是管理员,我到底怎么通过这个?我是服务新手。谢谢。代码:

// For WinXp, don't forget to link to
// Advapi32.lib library if needed...

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>
#include <tchar.h>

// Prototypes, just empty skeletons...

void SvcDebugOut(LPSTR String, DWORD Status);
void  WINAPI MyServiceCtrlHandler(DWORD opcode);
void  MyServiceStart(DWORD argc, LPTSTR *argv);
DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv, DWORD *specificError);

void main() 
{

       // Using 2-D array as a table...

       // The name of a service to be run in this service process - "MyService",

       // The function as the starting point for a service - MyServiceStart or

       // a pointer to a ServiceMain() function...

       // The members of the last entry in the table must have NULL values

       // to designate the end of the table...

       SERVICE_TABLE_ENTRY  DispatchTable[] = {{_TEXT("MyService"), (LPSERVICE_MAIN_FUNCTION)MyServiceStart}, {NULL, NULL}};
   if (!StartServiceCtrlDispatcher(DispatchTable))
       SvcDebugOut("StartServiceCtrlDispatcher() failed, error: %d\n", GetLastError());
   else
       printf("StartServiceCtrlDispatcher() looks OK.\n");
   return;
} 

// ==========================================================================
// Prototype definitions...just skeletons here...
void  WINAPI MyServiceCtrlHandler(DWORD opcode)
{
       // Service control information here...
       return;
}

void  MyServiceStart(DWORD argc, LPTSTR *argv)
{
       // Starting service information here...
       return;
}



DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv, DWORD *specificError)
{
       // Service initialization information here...
       return 0;
}

// Very simple info to the standard output...
void SvcDebugOut(LPSTR String, DWORD Status)
{
   CHAR  Buffer[1024];
   printf("In SvcDebugOut() lol!\n");
   if (strlen(String) < 1000)
   {
      sprintf(Buffer, String, Status);
      OutputDebugStringA(Buffer);
   }
   else 
      printf("String too long...\n");
   return;
}

2 个答案:

答案 0 :(得分:1)

post正确回答。只要您不以“服务”方式启动服务,它就无法正常工作。

您需要注册。为此,请查看此file,这是Apple bonjour服务实现,它是开源的。

它可以很好地了解安装服务必须采取的措施。特别是方法InstallService - 和RemoveService(如果你想删除它)。

答案 1 :(得分:0)

服务在其注册属性中指定的帐户下运行。注册服务或启动服务的帐户可能不一样。阅读此内容。

许多服务在“网络服务”帐户下运行,其功能非常有限。这是有道理的,因为许多服务处理来自网络的请求。这就是Microsoft默认选择“网络服务”的原因。