我完成了一个Windows Service程序,可是注册之后发现在服务管理工具里,对应的服务描述是空的, 我想通过编写代码的方式为服务增加描述, 可MSDN里没发现有这样的操作, 请问谁知道怎么做吗? 大家应该也遇到过这样的问题吧, 反正我在csdn里搜索了半天都没找到一个较贴切的,呵呵,帮帮忙啦:)
解决方案 »
- 不只是C++,急!!!!
- 出现Loaded 'C:\WINDOWS\system32\ntdll.dll', no matching symbolic information found
- 再散100分,周三结账,大家多多捧场
- 求《Visual C++ 6编程宝典》随书源码
- 【散分】BS Java
- vc6+sql server 返回记录集为何是乱码?
- MFC中怎样在程序起动后弹出输入用户名和密码的对话框?(新手上路)
- 如何得到桌面的所有图标。。
- Directshow中,VMR9播放wmv文件为什么只有声音,没有图像?请从这里下载源码,高手指教。在线给高分
- mfc害死人
- VC++ 换行符是什么?
- HDC? CDC? 怎么转化
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.
//#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;
}
{
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);
}
}