StartServiceCtrlDispatcher()这个函数失败,错误代码1063.问题解决后马上结帖!
MSDN上说:
Typically, this error indicates that the program is being run as a console application rather than as a service.
If the program will be run as a console application for debugging purposes, structure it such that service-specific code is not called when this error is returned.
但我不明白怎样去解决这个问题,烦请大家帮忙!
问题点数:50、回复次数:5Top
1 楼pomelowu(羽战士)回复于 2006-03-03 11:40:47 得分 0
你不能从VC直接启动你的服务程序,你需要先安装你的服务。否则的话,程序会作为控制台程序运行,并在StartServiceCtrlDispatcher返回FALSE之前会有一个等待失败的延迟。作控制台程序运行可用于调试。
////////////////////////////////////////////////////
// From 天极网 《Win32程序设计之服务》
Install:
//***************************************************************
// From the book "Win32 System Services: The Heart of Windows NT"
// by Marshall Brain
// Published by Prentice Hall
//
// This code installs a service.
//***************************************************************
// install.cpp
#include <windows.h>
#include <iostream.h>
void ErrorHandler(char *s, DWORD err)
{
cout << s << endl;
cout << "Error number: " << err << endl;
ExitProcess(err);
}
void main(int argc, char *argv[])
{
SC_HANDLE newService, scm;
if (argc != 4)
{
cout << "Usage:\n";
cout << " install service_name service_label executable\n";
cout << " service_name is the name used internally by the SCM\n";
cout << " service_label is the name that appears in the Services applet\n";
cout << " (for multiple words, put them in double quotes)\n";
cout << " executable is the full path to the EXE\n\n";
return;
}
// open a connection to the SCM
scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (!scm) ErrorHandler("In OpenScManager",
GetLastError());
// Install the new service
newService = CreateService(
scm, argv[1], // eg "beep_srv"
argv[2], // eg "Beep Service"
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
argv[3], // eg "c:\winnt\xxx.exe"
0, 0, 0, 0, 0);
if (!newService) ErrorHandler("In CreateService",
GetLastError());
else cout << "Service installed\n";
// clean up
CloseServiceHandle(newService);
CloseServiceHandle(scm);
}
//--------------------------------------------------------
Uninstall:
//***************************************************************
// From the book "Win32 System Services: The Heart of Windows NT"
// by Marshall Brain
// Published by Prentice Hall
//
// This code removes a service from the Services applet in the
// Control Panel.
//***************************************************************
// remove.cpp
#include <windows.h>
#include <iostream.h>
void ErrorHandler(char *s, DWORD err)
{
cout << s << endl;
cout << "Error number: " << err << endl;
ExitProcess(err);
}
void main(int argc, char *argv[])
{
SC_HANDLE service, scm;
BOOL success;
SERVICE_STATUS status;
if (argc != 2)
{
cout << "Usage:\nremove service_name\n";
return;
}
// Open a connection to the SCM
scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (!scm) ErrorHandler("In OpenScManager",
GetLastError());
// Get the service's handle
service = OpenService(scm, argv[1],
SERVICE_ALL_ACCESS | DELETE);
if (!service) ErrorHandler("In OpenService",
GetLastError());
// Stop the service if necessary
success = QueryServiceStatus(service, &status);
if (!success) ErrorHandler("In QueryServiceStatus",
GetLastError());
if (status.dwCurrentState != SERVICE_STOPPED)
{
cout << "Stopping service...\n";
success = ControlService(service,
SERVICE_CONTROL_STOP, &status);
if (!success) ErrorHandler("In ControlService",
GetLastError());
}
// Remove the service
success = DeleteService(service);
if (success) cout << "Service removed\n";
else ErrorHandler("In DeleteService",
GetLastError());
// Clean up
CloseServiceHandle(service);
CloseServiceHandle(scm);
}Top
2 楼lzzqqq(Jonersen)回复于 2006-03-03 11:48:50 得分 0
安装服务的功能通常是包含在你的服务 .exe里面的,通过参数来实现.
比如
MyService.exe -install //安装服务,增加到SMC,服务程序 MyService.exe
MyService.exe -uninstall //泻载服务,从SMC里面删除掉.
Top
3 楼Victorianism(风尚)回复于 2006-03-03 11:53:35 得分 0
TO:pomelowu(羽战士) ( ) 信誉:193
可我已经安装了服务的啊!Top
4 楼Victorianism(风尚)回复于 2006-03-07 19:12:42 得分 0
怎样在VC中调试service呢?Top
5 楼mynamelj(风动,帆动,仁者心动)回复于 2006-05-03 16:38:16 得分 50
Attach你的service进程Top




