2013-06-29 121 views
3

试图开发我的第一个Windows服务,并且我在Windows 7 MS VC++ 10.0中进行调试。只要它调用,StartServiceCtrlDispatcher(),我得到一个错误1063,它说,访问被拒绝。我是管理员,我究竟该如何通过这个?我是新来的服务。谢谢。代码:C++ Windows服务错误1063

// 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; 
} 

回答

0

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

许多服务在具有非常有限能力的“网络服务”帐户下运行。这很有意义,因为许多服务处理来自网络的请求。这就是为什么“网络服务”被微软选为默认值。

0

This post answers correct。只要你没有开始“作为服务”的服务,它将无法正常工作。

您需要注册它。要做到这一点,看看这个file,这是苹果bonjour服务的实现,它是开源的。

它给出了安装服务必须做什么的一个好主意。特别是方法InstallService - 和RemoveService(如果你想删除它)。