在调用StartServiceCtrlDispatcher函数时返回1063的错误是怎么回事啊?

Just4life 2007-04-04 02:45:22
如题
...全文
4041 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁月小龙 2011-09-09
  • 打赏
  • 举报
回复
太复杂了。
谁能给提供一个完整的代码呢?
developCpp 2007-06-02
  • 打赏
  • 举报
回复
楼主还有甚么疑问阿?
这么久了,怎么还不给我分阿?
$_$
smallcrocodile 2007-05-29
  • 打赏
  • 举报
回复
怎么复杂呀,看都费尽
developCpp 2007-05-28
  • 打赏
  • 举报
回复
printf("Creating Service .... ");
schService=CreateService(schSCManager,"sample","sample",SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,"sample.exe",NULL,NULL,NULL,NULL,NULL);
if(schService==NULL)
{
dwErrorCode=GetLastError();
if(dwErrorCode!=ERROR_SERVICE_EXISTS)
{
printf("Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
else
{
printf("already Exists !\n");
schService=OpenService(schSCManager,"ntkrnl",SERVICE_START);
if(schService==NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
}
}
else
{
printf("Success !\n");
}
printf("Starting Service .... ");
if(StartService(schService,0,NULL)==0)
{
dwErrorCode=GetLastError();
if(dwErrorCode==ERROR_SERVICE_ALREADY_RUNNING)
{
printf("already Running !\n");
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
return ;
}
}
else
{
printf("Pending ... ");
}
while(QueryServiceStatus(schService,&InstallServiceStatus)!=0)
{
if(InstallServiceStatus.dwCurrentState==SERVICE_START_PENDING)
{
Sleep(100);
}
else
{
break;
}
}
if(InstallServiceStatus.dwCurrentState!=SERVICE_RUNNING)
{
printf("Failure !\n");
}
else
{
printf("Success !\n");
}
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
developCpp 2007-05-28
  • 打赏
  • 举报
回复
好人做到底
只是提示一下
在CreateService成功后,
调用StartService启动服务
Just4life 2007-05-27
  • 打赏
  • 举报
回复
回头去试一下
developCpp 2007-05-24
  • 打赏
  • 举报
回复
楼主遇到的情况很正常
因为楼主是用VC调试阿,所以出现1063错误阿
ServiceMain()和main()函数都是入口函数,但是他们是不一样的
既然服务已经安装成功了,那就结束进程阿,(也就是退出main()函数阿)
这时候启动服务或者甚么都不用做,重新启动计算机,
楼主会发现你的程序已经运行啦,ServiceMain()函数中的任务在Runing呢
^_^
接分啦
Just4life 2007-04-05
  • 打赏
  • 举报
回复
错误1063:服务进程无法连接到服务控制器上。
检查参数是否正确。

参数错误?我看MSDN上面对于1063错误的解释是该程序在运行时不像一个服务,而像一个控制台服务

还有服务肯定是已经安装了的
Just4life 2007-04-05
  • 打赏
  • 举报
回复
// FUNCTION : CmdInstallService()
// PURPOSE : Installs the service
// PARAMETERS : none
// RETURN VALUE: none
VOID CmdInstallService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;

TCHAR szPath[512];

if ( CmdServiceIsInstall() ) {
_tprintf("the %s is install\n", TEXT(SZSERVICENAME));
return;
}

if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
{
_tprintf(TEXT("Unable to install %s - the error code is %ld\n"), TEXT(SZSERVICENAME), GetLastError() );
return;
}

schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE // access required
);
if ( schSCManager )
{
schService = CreateService(
schSCManager, // SCManager database
TEXT(SZSERVICENAME), // name of service
TEXT(SZSERVICENAME), // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
TEXT(SZDEPENDENCIES), // dependencies
NULL, // LocalSystem account
NULL); // no password

if ( schService )
{
_tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICENAME) );
CloseServiceHandle(schService);
}
else
{
_tprintf(TEXT("CreateService failed - the error code is %ld\n"), GetLastError() );
}

CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - the error code is %ld\n"), GetLastError() );
}

// FUNCTION : CmdRemoveService()
// PURPOSE : Stops and removes the service
// PARAMETERS : none
// RETURN VALUE : none
VOID CmdRemoveService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;

schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_CONNECT // access required
);
if ( schSCManager )
{
schService = OpenService(schSCManager, TEXT(SZSERVICENAME), DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
if (schService)
{
// try to stop the service
if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
{
_tprintf(TEXT("Stopping %s."), TEXT(SZSERVICENAME));
Sleep( 1000 );

while ( QueryServiceStatus( schService, &ssStatus ) )
{
if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
_tprintf(TEXT("."));
Sleep( 1000 );
}
else
break;
}

if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
_tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICENAME) );
else
_tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICENAME) );
}

// now remove the service
if ( DeleteService(schService) )
_tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICENAME) );
else
_tprintf(TEXT("DeleteService failed - the error code is %ld\n"), GetLastError());

CloseServiceHandle(schService);
}
else
_tprintf(TEXT("OpenService failed - the error code is %ld\n"), GetLastError());

CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - the error code is %ld\n"), GetLastError());
}

// FUNCTION: AddToMessageLog(LPTSTR lpszMsg)
// PURPOSE: Allows any thread to log an error message
//
// PARAMETERS:
// lpszMsg - text for message
//
// RETURN VALUE:
// none
void AddToMessageLog(LPTSTR lpszMsg)
{
LPCTSTR ErrorEventStr = lpszMsg;
HANDLE EventResourceHandle;

EventResourceHandle = RegisterEventSource( NULL, SZSERVICENAME );
if ( EventResourceHandle != NULL ) {
ReportEvent( EventResourceHandle, // handle of event source
EVENTLOG_INFORMATION_TYPE, // event type
0, // event category
0, // event ID
NULL, // current user's SID
1, // strings in lpszStrings
0, // no bytes of raw data
&ErrorEventStr, // array of error strings
NULL); // no raw data
}
(VOID)DeregisterEventSource( EventResourceHandle );
}


// FUNCTION : ReportStatusToSCMgr()
// PURPOSE : Sets the current status of the service and
// reports it to the Service Control Manager
// PARAMETERS : dwCurrentState - the state of the service
// dwWin32ExitCode - error code to report
// dwWaitHint - worst case estimate to next checkpoint
// RETURN VALUE : TRUE - success
// FALSE - failure
BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
BOOL fResult = TRUE;

if (dwCurrentState == SERVICE_START_PENDING)
ssStatus.dwControlsAccepted = 0;
else
ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

ssStatus.dwCurrentState = dwCurrentState;
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
ssStatus.dwWaitHint = dwWaitHint;

if ( ( dwCurrentState == SERVICE_RUNNING ) || ( dwCurrentState == SERVICE_STOPPED ) )
ssStatus.dwCheckPoint = 0;
else
ssStatus.dwCheckPoint = dwCheckPoint++;

// Report the status of the service to the service control manager.
//
if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus)))
{
AddToMessageLog(TEXT("SetServiceStatus failed!"));
}

return fResult;
}
Just4life 2007-04-05
  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include "MyService.h"
#include "Service.h"

SERVICE_STATUS ssStatus; // current status of the service
SERVICE_STATUS_HANDLE sshStatusHandle;
TCHAR szErr[256];

// internal function prototypes
VOID WINAPI service_ctrl(DWORD dwCtrlCode);
VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv);
BOOL CmdServiceIsInstall();
VOID CmdInstallService();
VOID CmdRemoveService();

void _cdecl main(int argc, char **argv)
{
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{ TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
{ NULL, NULL}
};

if ( argc > 1 ) {
if ( strcmp(argv[1], "/install") == 0 ) {
CmdInstallService();
}
else if ( strcmp(argv[1], "/uninstall") == 0 ) {
CmdRemoveService();
}
else{
printf( "%s /install to install the service\n", SZSERVICENAME );
printf( "%s /uninstall to remove the service\n", SZSERVICENAME );
printf( "\nStartServiceCtrlDispatcher being called.\n" );
printf( "This may take several seconds. Please wait.\n" );
}
exit(0);
}

if ( !::StartServiceCtrlDispatcher( dispatchTable )) {
_stprintf(szErr, TEXT("StartServiceCtrlDispatcher failed. The error code is [%ld]"), GetLastError());
AddToMessageLog(szErr);
}
}

VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv)
{
// register our service control handler:
sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), service_ctrl);
if (!sshStatusHandle)
{
(VOID)ReportStatusToSCMgr( SERVICE_STOPPED,
GetLastError(),
0);
return;
}

// SERVICE_STATUS members that don't change in example
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode = 0;

// report the status to the service control manager.
if (!ReportStatusToSCMgr( SERVICE_START_PENDING, // service state
NO_ERROR, // exit code
3000)) // wait hint
{
(VOID)ReportStatusToSCMgr( SERVICE_STOPPED,
GetLastError(),
0);
return;
}

// ServiceStart( dwArgc, lpszArgv);

}


// FUNCTION : service_ctrl
// PURPOSE : This function is called by the SCM whenever
// ControlService() is called on this service.
// PARAMETERS : dwCtrlCode - type of control requested
// RETURN VALUE : none
VOID WINAPI service_ctrl(DWORD dwCtrlCode)
{
// Handle the requested control code.
switch (dwCtrlCode)
{
// Stop the service.
// SERVICE_STOP_PENDING should be reported before
// setting the Stop Event - hServerStopEvent - in
// ServiceStop(). This avoids a race condition
// which may result in a 1053 - The Service did not respond...
// error.
case SERVICE_CONTROL_STOP:
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
ServiceStop();
return;
// Update the service status.
case SERVICE_CONTROL_INTERROGATE:
break;
// invalid control code
default:
break;
}
ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
}

//Function : CmdServiceIsInstall()
//Purpose : check-up the service is install
//PARAMETERS: none
//Return : true ---- the service install
// false ---- the service isn't install
BOOL CmdServiceIsInstall()
{
BOOL ServiceFlag = FALSE;
//打开服务控制管理器
SC_HANDLE hScm = ::OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( hScm != NULL ) {
//打开服务
SC_HANDLE MyServiceHandle = ::OpenService( hScm, SZSERVICENAME, SERVICE_QUERY_CONFIG);
if ( MyServiceHandle != NULL ) {
ServiceFlag = TRUE;
::CloseServiceHandle( MyServiceHandle );
}
::CloseServiceHandle( hScm );
}
return ServiceFlag;
}





















菜牛 2007-04-04
  • 打赏
  • 举报
回复
错误1063:服务进程无法连接到服务控制器上。
检查参数是否正确。
CrazyAzreal 2007-04-04
  • 打赏
  • 举报
回复
LZ可以看这里``以前有个这样的贴子`
http://topic.csdn.net/t/20060303/11/4590305.html

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧