导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

怎样增加一个服务到win2000中

Geoson 2002-07-24 01:47:41
用createservice,谁又例子给我一个好吗?lgx822@fm365.com
...全文
28 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hauck 2002-07-24
一个比较详细的例子,有注释,很清楚的,好好看吧!〉〉〉〉
//在服务控制管理器数据库中注册后台服务线程
void CmdRegisterService()
{
  SCHANDLE  schService;
  SCHANDLE  schSCManager;
  TCHAR szPath[512];
  //获得后台服务进程执行路径
  if(GetModuleFileName(NULL,szPath,512)==0)
  {tprintf(TEXT(“Unable to install %s - %s\n"),
  TEXT(“Service Example"), GetLastErrorText(szErr, 256));
   return;
  }
  //打开服务控制管理器数据库
  schSCManager=OpenSCManager(
         NULL,   //本地机器
         NULL,   //默认数据库
         SCMANAGERALLACCESS //访问权限
         );
  //在此数据库中创建后台服务线程对象
  if(schSCManager)
  { schService=CreateService(
    schSCManager, //服务控制管理器数据库
    TEXT(“ServiceExample"), //后台服务名称
    TEXT(“Service Example"),//在服务控制面板中显示的//服务名称
    SERVICE_ALL_ACCESS, //访问权限
    SERVICE_WIN32_OWN_PROCESS, //服务类型
    SERVICE_AUTO_START, //启动类型,随系统自动//加载
    SERVICE_ERROR_NORMAL,
    szPath,
    NULL,
    NULL,
TEXT(“"),
NULL,  //本地系统帐号
NULL);// 没有口令
  //在这里可以创建多个后台服务线程对象,完成不同的
//后台任务
  if(schService)
   {tprintf(TEXT(“%s installed.\n"),TEXT(“Service Example"));
    CloseServiceHandle(schService);
   }
   else
   {tprintf(TEXT(“CreateService failed-%s\n"),
   GetLastErrorText(szErr, 256));
   }
   CloseServiceHandle(schSCManager);
  }
  else
   tprintf(TEXT(“OpenSCManager failed - %s\n"),
GetLastErrorText(szErr,256));
}
//定义后台服务线程入口表
SERVICETABLEENTRY MyServiceTable[]=
{
   {
   TEXT(“ServiceExample"),//后台服务线程的名称
   (LPSERVICEMAINFUNCTION)MyServiceMain//后台服//务线程入口点
   },
   {NULL, NULL }   //标志表的结束
};
//服务线程入口函数
void WINAPI MyServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
  //注册服务控制处理函数
sshStatusHandle=RegisterServiceCtrlHandler(TEXT(“ServiceExample"), Servi ceControlHandler);
  if(!sshStatusHandle)
goto cleanup;
  //服务类型
  ssStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
  //指定的出错代码
  ssStatus.dwServiceSpecificExitCode=0;
  //启动自己定义的后台服务
  ServiceStart();
cleanup:
  if(sshStatusHandle)
(VOID)ReportStatusToSCMgr(
SERVICESTOPPED,dwErr,0);
  return;
}

//服务控制处理函数:负责接收和处理服务控制管理器发出的
//命令
VOID WINAPI ServiceControlHandler(DWORD dwCtrlCode)
{
  switch(dwCtrlCode)
  {
   //停止服务之前,应首先向服务控制管理器回送状态信息
   case SERVICE_CONTROLSTOP:
    ReportStatusToSCMgr(SERVICE_STOP_PENDING,NOE RROR, 0);
    ServiceStop();
    return;
   //更新服务状态
   case SERVICE_CONTROL_INTERROGATE:
    break;
   default:
    break;
  }
  //向服务控制面板管理器回送状态信息
  ReportStatusToSCMgr(ssStatus.dwCurrentState, NOERROR, 0);
}

//设置当前服务状态并将状态信息回送到服务控制管理器
BOOL ReportStatusToSCMgr(DWORD dwCurrentState,DWORD
dwWin32ExitCode,WORD dwWaitHint)
{
  static DWORD dwCheckPoint=1;
  BOOL fResult=TRUE;
  if(dwCurrentState==SERVICESTARTPENDING)
   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++;
  //将状态信息回送到服务控制管理器
  if(!(fResult=SetServiceStatus(sshStatusHandle,&ssStatus))){
   AddToMessageLog(TEXT(“SetServiceStatus"));//向NT事//件管理器报告出错消息
   }
   return fResult;
}

//服务进程入口
voidCRTAPI1 main(int argc, char **argv)
{
  if((argc>1)&&((*argv[1]==‘-')‖(*argv[1]==‘/')) )
  {
   if(stricmp(“register", argv[1]+1)==0)
    CmdRegisterService(); //注册后台服务线程对象
   else
    goto dispatch;
   exit(0);
  }
  dispatch:
   //启动服务控制分配器,建立主线程与服务控制面板的
//连接
   if(StartServiceCtrlDispatcher(MyServiceTable))
    AddToMessageLog(TEXT (“StartServiceCtrlDispatcher success."));
}void ServiceStart()
{
  //向服务控制管理器报告服务正在启动
  if(!ReportStatusToSCMgr(
SERVICE_START_PENDING, //service state
NOERROR,
dwWaitHint))
return;
  //下面是线程的初始化代码(注意:初始化代码执行时间
//不应超过设定的nWaitHi nt,否则服务控制管理器会认
//为服务线程已经没有响应
  //向服务控制管理器报告服务已经启动
  if(!ReportStatusToSCMgr(
SERVICE_RUNNING,  //service state
NOERROR,   //exit code
0))    //wait hint
   return;
//开始执行服务
}
//停止后台服务线程
void ServiceStop()
{
  //添加结束服务线程代码
}
回复
mfkzj 2002-07-24
给你了 你验收给分 呵呵
回复
kingzai 2002-07-24
Platform SDK: Directory Services
Installing a Service on a Host Computer
void InstallServiceOnLocalComputer(
LPTSTR szServiceAccountDN, // distinguished name of logon account.
LPTSTR szServiceAccountSAM, // SAM name of logon account.
LPTSTR szPassword) // Password of logon account.
{
SC_HANDLE schService = NULL;
SC_HANDLE schSCManager = NULL;
TCHAR szPath[512];
LPTSTR lpFilePart;
TCHAR szDNofSCP[MAX_PATH];
TCHAR szServiceClass[]=TEXT("ADSockAuth");

DWORD dwStatus;
TCHAR **pspn=NULL;
ULONG ulSpn=1;

// Get the full path of the service's executable.
// The sample assumes that the executable is in the current directory.
dwStatus = GetFullPathName(TEXT("service.exe"), 512, szPath, &lpFilePart);
if (dwStatus == 0) {
_tprintf(TEXT("Unable to install %s - %s\n"),
TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
return;
}
_tprintf(TEXT("path of service.exe: %s\n"), szPath);

// Open the Service Control Manager on the local computer.
schSCManager = OpenSCManager(
NULL, // computer (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (! schSCManager) {
_tprintf(TEXT("OpenSCManager failed - %s\n"),
GetLastErrorText(szErr,256));
goto cleanup;
}

// Install the service in the SCM database.
schService = CreateService(
schSCManager, // SCManager database
TEXT(SZSERVICENAME), // name of service
TEXT(SZSERVICEDISPLAYNAME), // 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
szServiceAccountSAM, // service account
szPassword); // account password
if (! schService) {
_tprintf(TEXT("CreateService failed - %s\n"),
GetLastErrorText(szErr,256));
goto cleanup;
}

_tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) );

// Create the service's Service Connection Point (SCP).
dwStatus = ScpCreate(
2000, // Service's default port number
szServiceClass, // Specifies the service class string.
szServiceAccountSAM, // SAM name of logon account for ACE
szDNofSCP // Buffer returns the DN of the SCP.
);
if (dwStatus != 0) {
_tprintf(TEXT("ScpCreate failed: %d\n"), dwStatus );
DeleteService(schService);
goto cleanup;
}

// Compose and register a service principal name for this service.
// We do this on the install path because this requires elevated
// privileges for updating the directory.
// If a local account of the format ".\username", skip the SPN.
if ( szServiceAccountSAM[0] == '.' )
{
_tprintf(TEXT("Don't register SPN for a local account.\n"));
goto cleanup;
}

dwStatus = SpnCompose(
&pspn, // Receives pointer to the SPN array.
&ulSpn, // Receives number of SPNs returned.
szDNofSCP, // Input: DN of the SCP.
szServiceClass); // Input: the service's class string.

if (dwStatus == NO_ERROR)
dwStatus = SpnRegister(
szServiceAccountDN, // Account on which SPNs are registered.
pspn, // Array of SPNs to register.
ulSpn, // Number of SPNs in array.
DS_SPN_ADD_SPN_OP); // Operation code: Add SPNs.

if (dwStatus != NO_ERROR)
{
_tprintf(TEXT("Failed to compose SPN: Error was %X\n"),
dwStatus);
DeleteService(schService);
ScpDelete(szDNofSCP, szServiceClass, szServiceAccountDN);
goto cleanup;
}

cleanup:
if (schSCManager)
CloseServiceHandle(schSCManager);
if (schService)
CloseServiceHandle(schService);
DsFreeSpnArray(ulSpn, pspn);
return;
}

回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

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