遍历系统服务和创建服务
/Files/tt_mc/catchser.rar这段代码是遍历系统中的服务,看里面是否有“HOOK”这个服务,如果没有就创建:
#include "StdAfx.h"
#include <string>
#include "ServConfig.h"
using namespace std;
int CatchSer()
{
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( !hSCM )
{
// MessageBoxEx(NULL,_TEXT("测试文本"),_TEXT("测试标题"),MB_OK,0);
}
CServItem *pServHeader = NULL, *pServPre = NULL, *pServNext = NULL;
LPENUM_SERVICE_STATUS pServStatus = NULL;
DWORD dwBytesNeeded = 0, dwServCound = 0, dwResume = 0, dwRealBytes = 0;
BOOL bRet = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &dwBytesNeeded, &dwServCound, &dwResume);
if ( !bRet && GetLastError() == ERROR_MORE_DATA )
{
dwRealBytes = dwBytesNeeded;
pServStatus = new ENUM_SERVICE_STATUS[dwRealBytes+1];
ZeroMemory(pServStatus, dwRealBytes+1);
bRet = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, pServStatus, dwRealBytes, &dwBytesNeeded, &dwServCound, &dwResume);
if ( !bRet )
{
CloseServiceHandle(hSCM);
return NULL;
}
}
else
{
CloseServiceHandle(hSCM);
return NULL;
}
string sh1,sh2 ;
for( DWORD dwIdx = 0; dwIdx < dwServCound; dwIdx++ )
{
char a[100];
strcpy(a,pServStatus[dwIdx].lpDisplayName);
sh1 = a;
if(sh1=="hook")
{
sh2="hook";
}
}
if(sh2!="hook")
{
char name[100]="hook";
char info[200]="hook";
char path[300]="C:\\map.exe";
SC_HANDLE manager=NULL;
SC_HANDLE service=NULL;
if((manager=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
{
printf("OpenSCManager Error");
}
service=CreateService(
manager,name,info,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, 0, 0, 0, 0, 0 );
if(service)
printf("服务创建成功\n\n");
else
printf("服务创建失败\n\n");
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CatchSer();
return TRUE;
}
#include <string>
#include "ServConfig.h"
using namespace std;
int CatchSer()
{
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( !hSCM )
{
// MessageBoxEx(NULL,_TEXT("测试文本"),_TEXT("测试标题"),MB_OK,0);
}
CServItem *pServHeader = NULL, *pServPre = NULL, *pServNext = NULL;
LPENUM_SERVICE_STATUS pServStatus = NULL;
DWORD dwBytesNeeded = 0, dwServCound = 0, dwResume = 0, dwRealBytes = 0;
BOOL bRet = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &dwBytesNeeded, &dwServCound, &dwResume);
if ( !bRet && GetLastError() == ERROR_MORE_DATA )
{
dwRealBytes = dwBytesNeeded;
pServStatus = new ENUM_SERVICE_STATUS[dwRealBytes+1];
ZeroMemory(pServStatus, dwRealBytes+1);
bRet = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, pServStatus, dwRealBytes, &dwBytesNeeded, &dwServCound, &dwResume);
if ( !bRet )
{
CloseServiceHandle(hSCM);
return NULL;
}
}
else
{
CloseServiceHandle(hSCM);
return NULL;
}
string sh1,sh2 ;
for( DWORD dwIdx = 0; dwIdx < dwServCound; dwIdx++ )
{
char a[100];
strcpy(a,pServStatus[dwIdx].lpDisplayName);
sh1 = a;
if(sh1=="hook")
{
sh2="hook";
}
}
if(sh2!="hook")
{
char name[100]="hook";
char info[200]="hook";
char path[300]="C:\\map.exe";
SC_HANDLE manager=NULL;
SC_HANDLE service=NULL;
if((manager=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
{
printf("OpenSCManager Error");
}
service=CreateService(
manager,name,info,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, 0, 0, 0, 0, 0 );
if(service)
printf("服务创建成功\n\n");
else
printf("服务创建失败\n\n");
CloseServiceHandle(service);
CloseServiceHandle(manager);
}
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CatchSer();
return TRUE;
}