"AND" "OR"
Главная Информер Журнал Форум

 

  Листинг 1 (создаёт в реестре NT учётную запись сервиса) setup.cpp
include <windows.h>

char infomes[512];
char servicedir[MAX_PATH];

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
	lstrcpy(infomes, "Service succesful creat at path:\n");
	lstrcpy(servicedir, "%SystemRoot%\\System32\\service.exe");

	SC_HANDLE scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
	if(scm!=NULL) {
		SC_HANDLE svc=CreateService(scm,
			"Terra-Incognita",
			"Terra-Incognita",
			0,
			SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
			SERVICE_AUTO_START,
			SERVICE_ERROR_IGNORE,
			servicedir,
			NULL,
			NULL,
			NULL,
			NULL,
			NULL);
	
		if(svc!=NULL) {
			CloseServiceHandle(svc);
			strcat(infomes, servicedir);
			MessageBox(NULL, infomes, "Install", NULL);
		}
		CloseServiceHandle(scm);
	}
	else  {
		MessageBox(NULL, "Error: Could not create Service.", "Install", NULL);
	}

	return 0;
}

 

  Листинг 2 (исходный код, который будет обрабатывать управляющие сообщения от NT) service.cpp
#include 
#include "service.h"

HMODULE g_module=NULL;

DWORD WINAPI EntryPoint(LPVOID lpParameter)
{	
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	char svName[150];
	char *svTarget="TERRA.EXE\0";
	char svTargetName[150],*svFilePart;
	char svComLine[2048];
	char svFileName[512];

	g_module=GetModuleHandle(NULL);
	GetModuleFileName(g_module,svFileName,512);

	GetSystemDirectory(svName,150-1);
	lstrcat(svName,"\\");
	lstrcpyn(svName+lstrlen(svName),svTarget,150-lstrlen(svName));
	GetFullPathName(svName,150,svTargetName,&svFilePart);

	lstrcpyn(svComLine,svTargetName,2048);
	lstrcpyn(svComLine+lstrlen(svComLine)," ",2048-lstrlen(svComLine));
	lstrcpyn(svComLine+lstrlen(svComLine),svFileName,2048-lstrlen(svComLine));

	memset(&si,0,sizeof(STARTUPINFO));
	si.cb=sizeof(STARTUPINFO);
	si.dwFlags=STARTF_FORCEOFFFEEDBACK;

//	MessageBox(NULL,svComLine,"Command Line before...",MB_OK);
	CreateProcess(NULL,svComLine,NULL,NULL,0,0,NULL,NULL,&si,&pi);

	return 0;
}

extern "C" int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow);

VOID WINAPI Handler( DWORD fdwControl ) 
{
	
}

void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpszArgv)
{
	SERVICE_STATUS_HANDLE ssh=RegisterServiceCtrlHandler(WebPrice, &Handler);

	SERVICE_STATUS ss;
	ss.dwServiceType=SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS;
	ss.dwCurrentState=SERVICE_RUNNING;
	ss.dwControlsAccepted=0;
	ss.dwWin32ExitCode=NO_ERROR;
	ss.dwCheckPoint=0;
	ss.dwWaitHint=0;
	SetServiceStatus(ssh,&ss);

	EntryPoint(GetModuleHandle(NULL));

	ss.dwServiceType=SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS;
	ss.dwCurrentState=SERVICE_STOPPED;
	ss.dwControlsAccepted=0;
	ss.dwWin32ExitCode=NO_ERROR;
	ss.dwCheckPoint=0;
	ss.dwWaitHint=0;
	SetServiceStatus(ssh,&ss);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
	SERVICE_TABLE_ENTRY DispatcherTable[]=
	{
		{	WebPrice,
			(LPSERVICE_MAIN_FUNCTION)ServiceMain },
		{	NULL,
			NULL }
	};

	StartServiceCtrlDispatcher(DispatcherTable);
	return 0;
}
  Листинг 3 (service.h)
#include <windows.h>

#ifndef __INC_MAIN_H
#define __INC_MAIN_H

#define WebPrice "WebPrice"
extern DWORD WINAPI EntryPoint(LPVOID lpParameter);

extern HMODULE g_module;

#endif

 

  Листинг 4 (программка удаляет из реестра NT наш сервис) uninst.cpp
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
	
	SC_HANDLE scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if(scm!=NULL) {
		SC_HANDLE svc=OpenService(scm, "Terra-Incognita", STANDARD_RIGHTS_REQUIRED);
		DeleteService(svc);
	
		if(svc!=NULL) {
			CloseServiceHandle(svc);
			MessageBox(NULL, "Succesful", "UnInstall", NULL);
		}
		CloseServiceHandle(scm);
	}
	else  {
		MessageBox(NULL, "Error: Could not uninstall Service.", "UnInstall", NULL);
	}

	return 0;
}