1063对应的解释是---服务进程无法连接到服务控制器上。 哪位大虾知道是什么原因吗?谢谢!

解决方案 »

  1.   

    不能以命令行方式运行服务所在的EXE文件,应该在SCM的服务列表中安装.
      

  2.   

    ok?
    BOOL InstallService(DWORD dwArgc,LPTSTR *lpszArgv)
    {
        BOOL bRet=FALSE;
        __try
        {
            //Open Service Control Manager on Local or Remote machine
            hSCManager=OpenSCManager(szTarget,NULL,SC_MANAGER_ALL_ACCESS);
            if(hSCManager==NULL)
            {
                printf("\nOpen Service Control Manage failed:%d",GetLastError());
                __leave;
            }
            //printf("\nOpen Service Control Manage ok!");
            //Create Service
            hSCService=CreateService(hSCManager,// handle to SCM database 
                ServiceName,// name of service to start
                ServiceName,// display name
                SERVICE_ALL_ACCESS,// type of access to service
                SERVICE_WIN32_OWN_PROCESS,// type of service
                SERVICE_AUTO_START,// when to start service
                SERVICE_ERROR_IGNORE,// severity of service 
                failure
                EXE,// name of binary file
                NULL,// name of load ordering group
                NULL,// tag identifier
                NULL,// array of dependency names
                NULL,// account name 
                NULL);// account password
            //create service failed
            if(hSCService==NULL)
            {
                //如果服务已经存在,那么则打开
                if(GetLastError()==ERROR_SERVICE_EXISTS)
                {
                    //printf("\nService %s Already exists",ServiceName);
                    //open service
                    hSCService = OpenService(hSCManager, ServiceName, 
                        SERVICE_ALL_ACCESS);
                    if(hSCService==NULL)
                    {
                        printf("\nOpen Service failed:%d",GetLastError());
                        __leave;
                    }
                    //printf("\nOpen Service %s ok!",ServiceName);
                }
                else
                {
                    printf("\nCreateService failed:%d",GetLastError());
                    __leave;
                }
            }
            //create service ok
            else
            {
                //printf("\nCreate Service %s ok!",ServiceName);
            }
            
            // 起动服务
            if ( StartService(hSCService,dwArgc,lpszArgv)) 
            { 
                //printf("\nStarting %s.", ServiceName); 
                Sleep(20);//时间最好不要超过100ms
                while( QueryServiceStatus(hSCService, &ssStatus ) ) 
                { 
                    if ( ssStatus.dwCurrentState == SERVICE_START_PENDING) 
                    { 
                        printf("."); 
                        Sleep(20); 
                    } 
                    else 
                        break; 
                } 
                if ( ssStatus.dwCurrentState != SERVICE_RUNNING ) 
                    printf("\n%s failed to run:%d",ServiceName,GetLastError()); 
            } 
            else if(GetLastError()==ERROR_SERVICE_ALREADY_RUNNING)
            {
                //printf("\nService %s already running.",ServiceName);
            }
            else
            {
                printf("\nStart Service %s failed:%d",ServiceName,GetLastError());
                __leave;
            }
            bRet=TRUE;
        }//enf of try
        __finally
        {
            return bRet;
        }
        return bRet;
    }
    /////////////////////////////////////////////////////////////////////////
    BOOL WaitServiceStop(void)
    {
        BOOL bRet=FALSE;
        //printf("\nWait Service stoped");
        while(1) 
        { 
            Sleep(100);
            if(!QueryServiceStatus(hSCService, &ssStatus))
            {
                printf("\nQueryServiceStatus failed:%d",GetLastError());
                break;
            }
            if(ssStatus.dwCurrentState==SERVICE_STOPPED)
            {
                bKilled=TRUE;
                bRet=TRUE;
                break;
            }
            if(ssStatus.dwCurrentState==SERVICE_PAUSED)
            { 
                //停止服务
                bRet=ControlService(hSCService,SERVICE_CONTROL_STOP,NULL);
                break;
            } 
            else 
            {
                //printf(".");
                continue;
            } 
        }
        return bRet;
    }