Windows C++, [09.12.2011]

, -, . , , Windows. - .

, , , , .

, ( ). /, / , , . , , - , . , . . .

, , . , . . , , , , , . , , .

ProcLimit

, Test, , , , , . , Visual Studio C++.

Parameters . , , . . , .

windows register

100% Windows XP SP3, Windows 2003, Windows 7, Windows 2008 R2. , requireAdministrator .

Visual Studio 2008

Visual Studio 2008. .

Service.cpp , , .

// Service.cpp: implementation of the CService class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Service.h"
 
#include "Psapi.h"
#include "shlwapi.h"
 
#include "shellapi.h"
 
 
// ,
// ,
 
CService* psrv; // ,
// CService
// ProcLimit.cpp
 
VOID WINAPI ThisServiceHandler(DWORD fdwControl)
{
psrv->ThisServiceHandler(fdwControl);
}
 
VOID WINAPI ThisServiceMain(DWORD dwArgc,LPTSTR *lpszArgv)
{
psrv->ThisServiceMain(dwArgc,lpszArgv);
}
 
 
// , ,
// , . Text
// . Param
// ,
//
 
CService::CService(const wchar_t* name,BOOL finteractive)
{
fShutdown=FALSE;
hSrv=NULL;
psrv=this;
wcscpy_s(srvName, 255,name);
 
fInteractive=finteractive;
 
sstat.dwServiceType=SERVICE_WIN32_OWN_PROCESS|(fInteractive?SERVICE_INTERACTIVE_PROCESS:0);
sstat.dwControlsAccepted=SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;//|SERVICE_ACCEPT_PAUSE_CONTINUE;
sstat.dwWin32ExitCode=NO_ERROR;
sstat.dwCheckPoint=0;
 
Param[0] = '\0';
Param[1] = '\0';
 
wcscpy_s(Text, MAX_TEXT_SIZE, L"start");
}
 
CService::~CService()
{
 
}
//
// /service,
int CService::Start()
{
ste[0].lpServiceName=srvName;
ste[0].lpServiceProc=::ThisServiceMain;
 
ste[1].lpServiceName=NULL;
ste[1].lpServiceProc=NULL;
 
//
//
return StartServiceCtrlDispatcher(ste);
}
 
//
void CService::ThisServiceMain(DWORD dwArgc,LPTSTR *lpszArgv)
{
hSrv=RegisterServiceCtrlHandler(srvName,::ThisServiceHandler);
 
SetStatus(SERVICE_START_PENDING,OP_TIMEOUT);
 
OnStartService();
 
SetStatus(SERVICE_RUNNING,0);
//
OnMainLoop();
}
 
// ,
// ThisServiceHandler
// .
//
// SetStatus
// "", " "
//SetStatus SetServiceStatus
// ,
void CService::OnMainLoop()
{
do
{
switch(sstat.dwCurrentState)
{
case SERVICE_RUNNING:
OnIdle();
break;

case SERVICE_PAUSE_PENDING:
OnPause();
SetStatus(SERVICE_PAUSED,0);
break;

case SERVICE_PAUSED:
OnPauseIdle();
break;

case SERVICE_CONTINUE_PENDING:
OnContinue();
SetStatus(SERVICE_RUNNING,0);
break;

case SERVICE_STOP_PENDING:
OnStop();
SetStatus(SERVICE_STOPPED,0);
break;
default:
sstat.dwCurrentState=SERVICE_STOPPED;
break;
};
}
while(sstat.dwCurrentState!=SERVICE_STOPPED);
// ,
}
 
//
// , .
// SetStatus ,
// OnMainLoop
void CService::ThisServiceHandler(DWORD fdwControl)
{
switch(fdwControl)
{
case SERVICE_CONTROL_STOP:
fShutdown=FALSE;
SetStatus(SERVICE_STOP_PENDING,OP_TIMEOUT);
break;
case SERVICE_CONTROL_PAUSE:
SetStatus(SERVICE_PAUSE_PENDING,OP_TIMEOUT);
break;
case SERVICE_CONTROL_CONTINUE:
SetStatus(SERVICE_CONTINUE_PENDING,OP_TIMEOUT);
break;
case SERVICE_CONTROL_INTERROGATE:
SetStatus(sstat.dwCurrentState,0);
break;
case SERVICE_CONTROL_SHUTDOWN:
fShutdown=TRUE;
SetStatus(SERVICE_STOP_PENDING,OP_TIMEOUT);
break;
default:
break;
}
}
 
void CService::SetStatus(DWORD dwCurrentState, DWORD timeout)
{
sstat.dwCurrentState=dwCurrentState;
sstat.dwWaitHint=timeout;
SetServiceStatus(hSrv,&sstat);
}
 
// ( ThisServiceMain)
//
void CService::OnStartService()
{
QueryPrivileges();
this->GetParameters();
}
// OnMainLoop
// . ,
// OnIdle. ,
// , OnMainLoop
// SERVICE_RUNNING. " "
// -
void CService::OnIdle()
{
Sleep(1000);
Test();
}
 
void CService::OnPauseIdle()
{
 
}
 
void CService::OnPause()
{
 
}
 
void CService::OnContinue()
{
 
}
// OnMainLoop
void CService::OnStop()
{
 
}
 
// ,
//
//
int CService::Register()
{
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
if(hSCM==NULL)return 0;

wchar_t szModuleName[256];
GetModuleFileName(NULL,szModuleName,255);

if(wcschr(szModuleName,' '))
{
wchar_t str[256];
wcscpy_s(str,255,L"\"");
wcscat_s(str,255,szModuleName);
wcscat_s(str,255,L"\"");
wcscpy_s(szModuleName,255,str);
}
wcscat_s(szModuleName,255,L" /service");
 
SC_HANDLE hS=CreateService(hSCM,srvName,srvName,SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS|(fInteractive?SERVICE_INTERACTIVE_PROCESS:0),
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
szModuleName,NULL,NULL,NULL,NULL,NULL);
 
if(hS==NULL)
{
wcscpy_s(Text, MAX_TEXT_SIZE, L"error");
 
CloseServiceHandle(hSCM);
return 0;
}
 
CloseServiceHandle(hS);
 
CloseServiceHandle(hSCM);
 
CreateFirstStartParams();
 
wcscpy_s(Text, MAX_TEXT_SIZE, L"service registered");
 
return 1;
}
 
int CService::Unregister()
{
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
if(hSCM==NULL)return 0;

SC_HANDLE hS=OpenService(hSCM,srvName,SERVICE_ALL_ACCESS);
 
if(hS==NULL)
{
wcscpy_s(Text, MAX_TEXT_SIZE, L"error");
 
CloseServiceHandle(hSCM);
return 0;
};
 
BOOL res=DeleteService(hS);
 
CloseServiceHandle(hS);
 
CloseServiceHandle(hSCM);
 
wcscpy_s(Text, MAX_TEXT_SIZE, L"service unregistered");
 
return res;
}
 
int CService::Start_Service(void)
{
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
if(hSCM==NULL)return 0;

SC_HANDLE hS=OpenService(hSCM,srvName,SERVICE_ALL_ACCESS);
 
if(hS==NULL)
{
wcscpy_s(Text, MAX_TEXT_SIZE, L"error");
 
CloseServiceHandle(hSCM);
return 0;
}
 
BOOL res = StartService(hS,0,NULL);
 
CloseServiceHandle(hS);
 
CloseServiceHandle(hSCM);
 
wcscpy_s(Text, MAX_TEXT_SIZE, L"service started");
 
return res;
}
 
int CService::Stop_Service(void)
{
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
if(hSCM==NULL)return 0;

SC_HANDLE hS=OpenService(hSCM,srvName,SERVICE_ALL_ACCESS);
 
if(hS==NULL)
{
wcscpy_s(Text, MAX_TEXT_SIZE, L"error");
 
CloseServiceHandle(hSCM);
return 0;
}
 
SERVICE_STATUS ss;
BOOL res = ControlService(hS,SERVICE_CONTROL_STOP,&ss);
 
CloseServiceHandle(hS);
 
CloseServiceHandle(hSCM);
 
wcscpy_s(Text, MAX_TEXT_SIZE, L"service stopped");
 
return res;
}
 
// .
//
// . ,
// ,
// OnStartService
BOOL CService::QueryPrivileges()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE;

LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);

int err = GetLastError();
if (err != ERROR_SUCCESS) return FALSE;
 
return TRUE;
}
 
 
// ,
// ,
//Param, .
// ,
void CService::Test()
{
wcscpy_s(Text, MAX_TEXT_SIZE, L"start process enum");
 
LPDWORD pIds = (LPDWORD)LocalAlloc(LMEM_FIXED, MAX_PROC * sizeof(DWORD));
DWORD dwSize;

EXCEPTION_ON_FALSE(EnumProcesses(pIds, MAX_PROC * sizeof(DWORD), &dwSize));
 
dwSize /= sizeof(DWORD);
int count = 0;
for(DWORD i = 0; i < dwSize; i ++)
{
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pIds[i]);
if(hProc != NULL)
{
TCHAR szProcName[MAX_IMAGE_LEN];
TCHAR szUserName[MAX_NAME_LEN];
TCHAR szDomainName[MAX_NAME_LEN];
szProcName[0] = '\0';
szUserName[0] = '\0';
szDomainName[0] = '\0';
 
GetProcessImageFileName(hProc, szProcName, MAX_IMAGE_LEN);
 
HANDLE hTok;
if(OpenProcessToken(hProc, TOKEN_QUERY, &hTok))
{
BYTE buff[256];
TOKEN_USER* ptu = (TOKEN_USER*)buff;
DWORD len, dlen;
if(GetTokenInformation(hTok, TokenUser, ptu, 256, &len))
{
len = MAX_NAME_LEN;
dlen = MAX_NAME_LEN;
SID_NAME_USE snu;
LookupAccountSid(NULL, ptu->User.Sid, szUserName, &len, szDomainName, &dlen, &snu);
}
CloseHandle(hTok);
}
// ,
// .
// , SetPriorityClass
DWORD dwPrio = GetPriorityClass(hProc);
DWORD dwNewPrio = QueryPriority(GetShortName(szProcName), szUserName);
if(dwNewPrio != 0 && dwPrio != dwNewPrio)
{
SetPriorityClass(hProc, dwNewPrio);
}
 
CloseHandle(hProc);
 
count++;
}
}
}
 
// , ,
//
DWORD CService::QueryPriority(const wchar_t* pszProcName, const wchar_t* pszUserName)
{
wcscat_s(Text, MAX_TEXT_SIZE, L" / ");
wcscat_s(Text, MAX_TEXT_SIZE, pszProcName);
wcscat_s(Text, MAX_TEXT_SIZE, L"-");
wcscat_s(Text, MAX_TEXT_SIZE, pszUserName);

int i = 0;
for(int i = 0; Param[i] != '\0'; i++)
{
if(NULL != StrStr(Param + i, pszProcName))
{
const wchar_t* pwc = StrStr(Param + i, L"+");
if(pwc == NULL) pwc = StrStr(Param + i, L"-");

if(pwc == NULL ||
*pwc == '+' && NULL != StrStr(pwc + 1, pszUserName) ||
*pwc == '-' && NULL == StrStr(pwc + 1, pszUserName))
{
wcscat_s(Text, MAX_TEXT_SIZE, L"==set_to_idle==\r\n");
return IDLE_PRIORITY_CLASS;
}
}
i += wcslen(Param + i);
}
 
return 0;
}
 
// Parameters\Idle
//
BOOL CService::GetParameters(void)
{
HKEY hKey = OpenRegParams();
if(hKey == NULL)return FALSE;
 
DWORD size = sizeof(Param);
if(ERROR_SUCCESS != RegQueryValueEx(hKey, L"Idle", NULL, NULL, (LPBYTE)Param, &size))
{
Param[0] = '\0';
Param[1] = '\0';
return FALSE;
}
 
return TRUE;
}
 
// Parameters\Idle
// ,
void CService::CreateFirstStartParams(void)
{
HKEY hKey = OpenRegParams();
if(hKey == NULL)return;
 
DWORD size = sizeof(Param);
if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"Idle", NULL, NULL, (LPBYTE)Param, &size))return;
 
memcpy(Param, L"iexplore.exe-\0firefox.exe\0chrome.exe+\0opera.exe-\0\0", 63 * sizeof(wchar_t));
RegSetValueEx(hKey, L"Idle", NULL, REG_MULTI_SZ, (LPBYTE)Param, 63 * sizeof(wchar_t));
}
 
//
HKEY CService::OpenRegParams(void)
{
wchar_t str[256];
 
wcscpy_s(str,255,L"SYSTEM\\CurrentControlSet\\Services\\");
wcscat_s(str,255,srvName);
wcscat_s(str,255,L"\\Parameters");
 
HKEY hKey;

if(ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE,str,0,L"",REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,NULL))
{
return NULL;
}
 
return hKey;
}
 
//
const wchar_t* CService::GetShortName(const wchar_t* name)
{
for(const wchar_t* pwc = name + wcslen(name); pwc > name; pwc--)
if(*pwc == '\\') return pwc + 1;
 
return name;
}
 
// ,
// /
void CService::OpenRegedit(void)
{
ShellExecute(NULL, NULL, L"regedit.exe", L"", NULL, SW_MAXIMIZE);
}
 
// ,
//
void CService::PrintText(HDC hdc, LPRECT prect)
{
DrawText(hdc, Text, wcslen(Text), prect, DT_WORDBREAK);
}

 

 

www.alvis.com.ua
, ,

m-t.com.ua
, ,

www.rdsrv.org

 -  -  © kuzmin@it.kharkov.ua +380504010794