我完成了一个Windows Service程序,可是注册之后发现在服务管理工具里,对应的服务描述是空的, 我想通过编写代码的方式为服务增加描述, 可MSDN里没发现有这样的操作, 请问谁知道怎么做吗? 大家应该也遇到过这样的问题吧, 反正我在csdn里搜索了半天都没找到一个较贴切的,呵呵,帮帮忙啦:)

解决方案 »

  1.   

    我也帮你找找,好像有个helpstring("")属性
      

  2.   

    CreateService
    The CreateService function creates a service object and adds it to the specified service control manager database. SC_HANDLE CreateService(
      SC_HANDLE hSCManager,  // handle to service control manager 
                             // database
      LPCTSTR lpServiceName, // pointer to name of service to start
      LPCTSTR lpDisplayName, // pointer to display name
      DWORD dwDesiredAccess, // type of access to service
      DWORD dwServiceType,   // type of service
      DWORD dwStartType,     // when to start service
      DWORD dwErrorControl,  // severity if service fails to start
      LPCTSTR lpBinaryPathName,  // pointer to name of binary file
      LPCTSTR lpLoadOrderGroup,  // pointer to name of load ordering 
                                 // group
      LPDWORD lpdwTagId,     // pointer to variable to get tag identifier
      LPCTSTR lpDependencies,  // pointer to array of dependency names
      LPCTSTR lpServiceStartName,
                               // pointer to account name of service
      LPCTSTR lpPassword       // pointer to password for service account
    );参数三:
    lpDisplayName 
    Pointer to a null-terminated string that is to be used by user interface programs to identify the service. This string has a maximum length of 256 characters. The name is case-preserved in the service control manager. display name comparisons are always case-insensitive. 
      

  3.   

    服务工具 - 注册/删除/启动/停止学习编写服务程序, 先写一个这个便于调试程序, 呵呵, 各位指点一二// ServiceTool.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include <windows.h>
    #include "winsvc.h"#include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>using namespace std;//
    //安装服务
    void InstallService( LPCTSTR lpszName, LPCTSTR lpszDescription, LPCTSTR lpszPath )
    {
        SC_HANDLE    scm;
        scm    = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
        if ( !scm )
            cerr << "in OpenSCMager: " << GetLastError() << endl;    //安装服务
        cout << "开始注册服务..." << endl;    SC_HANDLE    newService;
        newService    = CreateService( scm, lpszName, lpszDescription, 
            SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, 
            SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
            lpszPath, 0, 0, 0, 0, 0 );    if ( !newService )
            cerr << "In CreateService: " << GetLastError() << endl;
        else
            cout << "服务已经注册完毕..." << endl;    CloseServiceHandle( newService );
        CloseServiceHandle( scm );}//
    //删除服务
    void RemoveService( LPCTSTR lpszName )
    {
        SC_HANDLE    scm;
        scm    = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
        if ( !scm )
            cerr << "in OpenSCMager: " << GetLastError() << endl;    //安装服务
        cout << "开始删除服务..." << endl;    SC_HANDLE        service;
        service    = OpenService( scm, lpszName, SERVICE_ALL_ACCESS | DELETE );
        if ( !service )
            cerr << "In OpenService:" << GetLastError() << endl;    SERVICE_STATUS    status;
        BOOL            isSuccess    = QueryServiceStatus( service, &status );
        if ( !isSuccess )
            cerr << "In QueryServiceStatus: " << GetLastError() << endl;    if ( status.dwCurrentState!=SERVICE_STOPPED ){        cout << "停止服务..." << endl;        isSuccess    = ControlService( service, SERVICE_CONTROL_STOP, &status );
            if ( !isSuccess )
                cerr << "In ControlService: " << GetLastError() << endl;
            Sleep( 500 );    }    isSuccess    = DeleteService( service );
        if ( !isSuccess )
            cerr << "In DeleteService: " << GetLastError() << endl;
        else
            cout << "服务已经被删除..." << endl;    CloseServiceHandle( service );
        CloseServiceHandle( scm );}//
    //启动服务
    void StartService( LPCTSTR lpszName )
    {    SC_HANDLE    scm = NULL, service = NULL;    scm    = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
        if ( !scm ){        cerr << "Error Openning SCManager" << endl;
            return;    }
        service    = OpenService( scm, lpszName, SERVICE_ALL_ACCESS );
        if ( !service ){        cerr << "Error Openning " << lpszName << "Service" << endl;
            return;    }    if ( StartService( service, 0, NULL ) ){        cout << "启动服务成功..." << endl;
            
        }    CloseServiceHandle( service );
        CloseServiceHandle( scm );}//
    //停止服务
    void StopService( LPCTSTR lpszName )
    {
        SC_HANDLE    scm = NULL, service = NULL;    scm    = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
        if ( !scm ){        cerr << "Error Openning SCManager" << endl;
            return;    }
        service    = OpenService( scm, lpszName, SERVICE_ALL_ACCESS );
        if ( !service ){        cerr << "Error Openning " << lpszName << "Service" << endl;
            return;    }    SERVICE_STATUS    status;
        ControlService( service, SERVICE_CONTROL_STOP, &status);    CloseServiceHandle( service );
        CloseServiceHandle( scm );}int main(int argc, char* argv[])
    {    int        nChoice = 0;    while ( nChoice != 5 ){        cout << "请选择操作( 1 - 安装服务, 2 - 删除服务, 3 - 启动服务, 4 - 停止服务, 5 - 退出 ):";
            cin >> nChoice;        switch ( nChoice ) {
            case 1:
                {                char    szName[512]    = "";
                    char    szDes[512]    = "";
                    char    szPath[512]    = "";
                    cout << endl << "输入服务名称: ";
                    cin >> szName;                cout << endl << "输入服务描述: ";
                    cin >> szDes;                cout << endl << "输入服务文件路径: ";
                    cin >> szPath;                InstallService( szName, szDes, szPath );
                    break;            }
            case 2:
                {                char    szName[512]    = "";
                    cout << endl << "输入服务名称: ";
                    cin >> szName;                RemoveService( szName );
                    break;            }
            case 3:
                {                char    szName[512]    = "";
                    cout << endl << "输入服务名称: ";
                    cin >> szName;                StartService( szName );
                    break;            }
            case 4:
                {                char    szName[512]    = "";
                    cout << endl << "输入服务名称: ";
                    cin >> szName;                StopService( szName );
                    break;            }
            }    }
        return 0;
    }
      

  4.   

    使用ChangeServiceConfig2 API函数,具体实现如下:void ReconfigureService(LPSTR lpServiceName, LPSTR lpDesc) 

      SC_HANDLE schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
      if (schSCManager != NULL)
      {
        // Need to acquire database lock before reconfiguring. 
        SC_LOCK sclLock = LockServiceDatabase(schSCManager); 
        if (sclLock != NULL) 
        { 
          // Open a handle to the service. 
          SC_HANDLE schService = OpenService( 
    schSCManager,           // SCManager database 
    lpServiceName,          // name of service 
    SERVICE_CHANGE_CONFIG); // need CHANGE access 

          if (schService != NULL) 
          {
    SERVICE_DESCRIPTION sdBuf;
    sdBuf.lpDescription = lpDesc;
    if( ChangeServiceConfig2(
    schService, SERVICE_CONFIG_DESCRIPTION,
    &sdBuf) )
    {
      //MessageBox(NULL,"Change SUCCESS","",MB_SERVICE_NOTIFICATION); 
    }
    CloseServiceHandle(schService); 
          }
          UnlockServiceDatabase(sclLock); 
        } 
        CloseServiceHandle(schSCManager); 
      }
    }