1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <wtsapi32.h>
//Déclaration des variables
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
int bRunning=1;
//Déclaration des fonctions
void ShowMessage(LPWSTR lpszMessage, LPWSTR lpszTitle);
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
int InstalService();
int DeletService();
int Action(void);
int main(int argc, char* argv[])
{
if(argc>1)
{
if(strcmp(argv[1],"-i")==0) //Control de l'argument passé au prog
{InstalService();} //Rien lancement du service
//-i pour enregistement du service
if(strcmp(argv[1],"-d")==0) //-d pour désactivation du service
{DeletService();}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[]={{"srv_test",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
return 0;
}
void ShowMessage(LPWSTR lpszMessage, LPWSTR lpszTitle)
{
DWORD dwSession = WTSGetActiveConsoleSessionId();
DWORD dwResponse = 0;
WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, dwSession,
lpszTitle,
DWORD((wcslen(lpszTitle) + 1) * sizeof(wchar_t)),
lpszMessage,
DWORD(wcslen(lpszMessage) + 1) * sizeof(wchar_t)),
0, 0, &dwResponse, FALSE);
}
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
//Déclaration des variables
DWORD status;
DWORD specificError;
m_ServiceStatus.dwServiceType =SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler("srv_test",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{
}
bRunning=1;
FILE *fichier = NULL;
fichier = fopen("journal.log", "a");
if( fichier != NULL)
{
//C'est la qu'il faut mettre les commandes qui s'éxécuteront pendant tout le temps ou le service fontcionne
while(bRunning)
{
//Commande que le service execute
Sleep(6000); //Le temps est en millisseconde, sert à temporiser l'action pour pas quelle s'execute en boucle
//on peut aussi avoir une action qui se déclanche sur un évènement partculié
Action(); //Fonction où mettre le code a executer
fprintf(fichier, "LOOP");
ShowMessage("ecriture dans le fichier", "write");
}
}
else system("cmd.exe");
fprintf(fichier, "LOOP");
fclose(fichier);
return;
}
//Fonction de control de l'état du service
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
//Si le service reçoit la commande de se mettre en pause
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
//Si le service reçoit la commande de stopper
case SERVICE_CONTROL_STOP:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=0; //donc la boucle plus haut ne s'effectura plus
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
int InstalService()
{
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
strcat(strDir,"\\srv_test.exe");
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return 0;
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,"srv_test","srv_test_testa",
SERVICE_ALL_ACCESS, // Type d'acces
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // Type de service
SERVICE_AUTO_START, // Pour le demarrage automatique
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // Si null demarrer en tant que compte system
NULL); // Mot de passe : null si demarrer en tant que system
if (schService == NULL)
return 0;
CloseServiceHandle(schService);
return 0;
}
int DeletService()
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return 0;
hService=OpenService(schSCManager,"srv_test",SERVICE_ALL_ACCESS);
if (hService == NULL)
return 0;
if(DeleteService(hService)==0)
return 0;
if(CloseServiceHandle(hService)==0)
return 0;
else
return 0;
}
int Action(void)
{
printf("service");
} |
Partager