bonjour

dans le cadre de mon projet , qui consistait a automatiser un service , j ai du faire des fonctions pour pouvoir effectuer ce traitement automatique

on ma donc donne un modele de service a ameliorer par des fonctions , mais je ne vois pas ou les placer dans le service de maniere a appeler ses fonction des qu on lance le service. Je n ai jamais encore etudie une telle structure donc je ne sais pas comment faire


merci beaucoup de votre aide

mon service:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
 
#include <windows.h>
#include <stdlib.h>
#include <process.h>    /* _beginthread, _endthread */
#include <stdio.h>
#include <time.h>
#include <string.h>
#include<wininet.h>
#include <time.h>
#include <locale.h>
 
 
/***********************************************************************************/
void myThread( void *ch );
char * formatAff(char * chaine,int i,int j);
char * couperDebut(char * chaine, int n);
char * couperFin(char * chaine, int n);
 
int * timer(char sheure,char sdate);
void modifXML(int * lignes );
char * recupheure();
char * recupdate();
int compareheures(char * heure1_p, char * heure2_p);
int comparedates(char * date1, char * date2);
typedef struct _MyData {
    const char *site;
    const char *url;
    const char *script;
    DWORD loopMs;
} MYDATA, *PMYDATA;
 
/***********************************************************************************/
 
// *** Some usefull defines
#define _YES_   0x01
#define _NO_    0x00
#define _OK_    0x00
#define _ERROR_ 0x01
 
int nId;
// *** User Functions
short isServiceInstalled(void);
short installService(void);
short uninstallService(void);
 
// *** API Functions
// These functions are prototype pre-defined. You can name them as you want.
void  WINAPI sendIPMain(DWORD dwNumServiceArgs, LPTSTR *lpServiceArgs);
void  WINAPI XControlHandler(DWORD dwControl);
 
// *** Global Variables
/***********************************************************************************/
BOOL repeat = TRUE;     /* Global repeat flag and video variable */
/***********************************************************************************/
SERVICE_STATUS_HANDLE ghCtrlStatus;
SERVICE_STATUS        gStatus;
HANDLE                ghevDoForever = NULL;
 
// *** Main - EntryPoint
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{                    
 
// Create Service Data Array (contains information about service)
   SERVICE_TABLE_ENTRY SrvTable[2];
 
// Verify if service is running
   ghevDoForever = OpenEvent( SYNCHRONIZE, FALSE, "sendIPRunEvt" );
   if ( ghevDoForever )
   {
     MessageBox( NULL, "sendIP is active ! Stop it.", "sendIP", MB_ICONINFORMATION );
     (void) CloseHandle( ghevDoForever );
     exit(-1);
   }
 
// Init Global Vars 
   (void) memset( &gStatus, 0x00, sizeof(gStatus) );
   ghevDoForever = NULL;
 
// Verify command line arguments
   if (  strlen( lpCmdLine ) > 0 )
   {
      if ( !strcmp( lpCmdLine, "-delete" ) || !strcmp( lpCmdLine, "-DELETE" ) )
      {
         if ( uninstallService() )
         {
           MessageBox( NULL, "sendIP couldn't be uninstalled !", "sendIP", MB_ICONINFORMATION );
           exit(-1);
         }
         exit(0);
      }
      else
      {
         char _tmp[50];
         (void) memset( _tmp, 0x00, sizeof(_tmp) );
         sprintf( _tmp, "[%s] is not valid !", lpCmdLine );
         MessageBox( NULL, _tmp, "sendIP", MB_ICONINFORMATION );
         exit(-1);
      }
   }
 
// if service is installed ...
   if ( isServiceInstalled() == _NO_ )
     if ( installService() == _OK_ )
     {
       MessageBox( NULL, "sendIP was installed sucessfully !", "sendIP", MB_ICONINFORMATION );
       exit(0);
     }
     else
     {
       MessageBox( NULL, "Process was aborted !", "sendIP", MB_ICONINFORMATION );
       exit(-1);
     }
 
// Load service 
   SrvTable[0].lpServiceName = "sendIP";   // Service name 
   SrvTable[0].lpServiceProc = sendIPMain;  // Service EntryPoint
   SrvTable[1].lpServiceName = NULL;         // Nothing
   SrvTable[1].lpServiceProc = NULL;         // Nothing
 
 
 
// Pass the control to sendIPMain
   if ( !StartServiceCtrlDispatcher(SrvTable) )
   {
     MessageBox( NULL, "Service Start Error !", "sendIP", MB_ICONERROR );
     exit(-1);
   }
}
/*-----------------------------------------------------------------------------*/
void WINAPI sendIPMain(DWORD dwNumServiceArgs, LPTSTR *lpServiceArgs)
{
   DWORD rc;
 
/***********************************************************************************/
HKEY regHandle=NULL;
unsigned long regType,regcbData;
char site[100]="TEST2";
char url[100]="dc2-web21.assortedinternet.com";
char script[100]="/~sendip/sendip.php";
DWORD loopMs = 900000;
PMYDATA pData;
 
 
 
/***********************************************************************************/
// Register function that will receive messages from service manager
// Must be the first action in Service Main
   ghCtrlStatus = RegisterServiceCtrlHandler( "sendIP", (LPHANDLER_FUNCTION)XControlHandler );
   if ( ghCtrlStatus == (SERVICE_STATUS_HANDLE)NULL )
    {
      MessageBox( NULL, "Error in sendIPMain-RegisterServiceCtrlHandler !", "sendIP", MB_ICONERROR );
      DebugBreak(); // Force an error 
    }
 
// Create a semaphore to keep service alive
   ghevDoForever = CreateEvent( NULL, FALSE, FALSE, "sendIPRunEvt" );
 
   gStatus.dwServiceType              = SERVICE_WIN32;
   gStatus.dwCurrentState             = SERVICE_RUNNING;
   gStatus.dwControlsAccepted         = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
   gStatus.dwWin32ExitCode            = NO_ERROR;
   gStatus.dwServiceSpecificExitCode  = NO_ERROR;
   gStatus.dwCheckPoint               = 0;
   gStatus.dwWaitHint                 = 0;
 
// Make the first Status update
   rc = SetServiceStatus( ghCtrlStatus, &gStatus );
   if (!rc)
    {
      MessageBox( NULL, "Error in sendIPMain-SetServiceStatus !", "sendIP", MB_ICONERROR );
      DebugBreak();
    }
 
// After this point the XControlHandler function will receive control requests.
// You can start your processes, threads and any other task that you want
// I use to start a main thread at this point
 
/**************************************************************************************/
 //   MessageBox( NULL, "Le service est démarré", "sendIP", MB_ICONERROR );
/***********************************************************************************/
	 if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\sendIP",  0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
		 &regHandle,NULL) == ERROR_SUCCESS) 
	{
		regcbData = 100;
		 if (RegQueryValueEx(regHandle, "site", NULL, &regType, (unsigned char *) site, &regcbData) != ERROR_SUCCESS)
			RegSetValueEx(regHandle, "site", 0, REG_SZ, (LPBYTE) site,  strlen(site));
		regcbData = 100;
		if (RegQueryValueEx(regHandle, "url", NULL, &regType, (unsigned char *) url, &regcbData) != ERROR_SUCCESS)
			RegSetValueEx(regHandle, "url", 0, REG_SZ, (LPBYTE) url,  strlen(url));
		regcbData = 100;
		if (RegQueryValueEx(regHandle, "script", NULL, &regType, (unsigned char *) script, &regcbData) != ERROR_SUCCESS)
			RegSetValueEx(regHandle, "script", 0, REG_SZ, (LPBYTE) script,  strlen(script));
		regcbData = sizeof(DWORD);
		if (RegQueryValueEx(regHandle, "loop", NULL, &regType, (LPBYTE) &loopMs, &regcbData) != ERROR_SUCCESS) ;
			RegSetValueEx(regHandle, "loop", 0, REG_DWORD, (LPBYTE) &loopMs,  sizeof(DWORD));
 
 
 
 
			RegCloseKey(regHandle);
	 }
 
 
	 // Allocate memory for thread data.
	pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));
pData->loopMs = loopMs;
pData->script = script;
pData->site = site;
pData->url = url;
 
 
/***********************************************************************************/
   _beginthread( myThread, 0, pData  );
 
 
 
/**************************************************************************************/
// end of your code
 
// Keep service running
   (void) WaitForSingleObject( ghevDoForever, INFINITE );
 
// Service received a termination command (see XControlHandler function )
   gStatus.dwCurrentState = SERVICE_STOPPED;
   (void) SetServiceStatus( ghCtrlStatus, &gStatus );
 
   return;
}
/*-----------------------------------------------------------------------------*/
void WINAPI XControlHandler(DWORD dwControl)
{
   DWORD rc;
 
// Receive messages from service control dispatcher
   switch (dwControl)
        {
		   case SERVICE_CONTROL_PAUSE :
		   case SERVICE_CONTROL_CONTINUE :
		   case SERVICE_CONTROL_INTERROGATE :
			    gStatus.dwWaitHint     = 0;
			    break;
		   case SERVICE_CONTROL_SHUTDOWN:
		   case SERVICE_CONTROL_STOP :
/**************************************************************************************/
			    repeat = 0;    /* _endthread implied */
//			    MessageBox( NULL, "Le service est stoppé", "sendIP", MB_ICONERROR );
/**************************************************************************************/
			    gStatus.dwCurrentState = SERVICE_STOP_PENDING;
			    gStatus.dwWaitHint     = 5000;
			    break;
	    }
 
 
// Update service status
   rc = SetServiceStatus( ghCtrlStatus, &gStatus );
   if (!rc)
    {
      MessageBox( NULL, "Error in XControlHandler-SetServiceStatus !", "sendIP", MB_ICONERROR );
      DebugBreak();
    }
 
// If message is a stop or shutdown so, service is going to be stoped
   if ( (dwControl == SERVICE_CONTROL_STOP) || (dwControl == SERVICE_CONTROL_SHUTDOWN) )
    (void) SetEvent( ghevDoForever );
 
}
/*-----------------------------------------------------------------------------*/
short installService(void)
{
   SC_HANDLE scHndl;
   SC_HANDLE scServ;
   char      cCurDir[256];
 
// Get the current directory (the directory where the service is)
   (void) memset( cCurDir, 0x00, sizeof(cCurDir) );
   (void) GetCurrentDirectory( 256, cCurDir );
   (void) strcat( cCurDir, cCurDir[strlen(cCurDir)-1] == 92 ? "sendIP.exe" : "\\sendIP.exe" );
 
// Install the sendIP to Service Control Manager Database
   scHndl = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE );
   if ( scHndl == NULL ) 
   {
     MessageBox( NULL, "Error in installService-OpenSCManager !", "sendIP", MB_ICONERROR );
     return _ERROR_;
   }
 
   scServ = CreateService( scHndl,
                           "sendIP",
                           "send IP Service",
                           SERVICE_ALL_ACCESS,
                           SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                           SERVICE_AUTO_START,
                           SERVICE_ERROR_NORMAL,
                           cCurDir,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL );
 
   if ( scServ == NULL )
   {
     MessageBox( NULL, "Error in installService-CreateService !", "sendIP", MB_ICONERROR );
     (void) CloseHandle( scHndl );
     return _ERROR_;
   }
 
   (void) CloseHandle( scServ );
   (void) CloseHandle( scHndl );
 
   return _OK_;
}
/*-----------------------------------------------------------------------------*/
short uninstallService(void)
{
 
   SC_HANDLE scHndl;
   SC_HANDLE scServ;
   DWORD _err = 0;
 
// UnInstall the sendIP from Service Control Manager Database
   scHndl = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE );
   if ( scHndl == NULL ) 
   {
     MessageBox( NULL, "Error in uninstallService-OpenSCManager !", "sendIP", MB_ICONERROR );
     return _ERROR_;
   }
 
   scServ = OpenService( scHndl, "sendIP", SERVICE_ALL_ACCESS  );
 
   _err = GetLastError();
 
   if ( scServ == NULL )
   {
      if  (  _err == ERROR_SERVICE_DOES_NOT_EXIST )
      {
         (void) CloseHandle( scServ );
         (void) CloseHandle( scHndl );
      }
      return _ERROR_;
   }
 
   if ( !DeleteService( scServ ) )
   {
     MessageBox( NULL, "Error in uninstallService-DeleteService !", "sendIP", MB_ICONERROR );
     return _ERROR_;
   }
 
   (void) CloseHandle(scServ);
   (void) CloseHandle(scHndl);
 
   MessageBox( NULL, "sendIP was uninstalled sucessfully !", "sendIP", MB_ICONINFORMATION );
 
   return _OK_;
}
/*-----------------------------------------------------------------------------*/
short isServiceInstalled(void)
{
 
   SC_HANDLE scHndl;
   SC_HANDLE scServ;
   DWORD          _err = 0;
 
   scHndl = OpenSCManager( NULL,  NULL, SC_MANAGER_CREATE_SERVICE );
   if ( scHndl == NULL )
    {
      MessageBox( NULL, "Error in isServiceInstalled-OpenSCManager !", "sendIP", MB_ICONERROR );
      exit(-1);
    }
 
   scServ = OpenService( scHndl, "sendIP", SERVICE_ALL_ACCESS  );
 
   _err = GetLastError();
 
   if ( scServ == NULL )
   {
      if  (  _err == ERROR_SERVICE_DOES_NOT_EXIST )
      {
         (void) CloseHandle( scServ );
         (void) CloseHandle( scHndl );
         return _NO_;
      }
      else
      {
         MessageBox( NULL, "Error in isServiceInstalled-OpenService !", "sendIP", MB_ICONERROR );
         exit(-1);
      }
   }
 
   (void) CloseHandle( scServ );
   (void) CloseHandle( scHndl );
 
   return _YES_;
 
}
 
/**************************************************************************************/
void myThread( PVOID lpParam )
{
HINTERNET Initialize,Connection,File;
PMYDATA pData;
char adresse[100];
char timestamp[16];
struct tm *tm_ptr;
time_t myTime;
 
 
 
    // Cast the parameter to the correct data type.
    pData = (PMYDATA)lpParam;
 
	while( repeat )
    {
 
/*initialize the wininet library*/
Initialize = InternetOpen("HTTPGET",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
/*connect to the server*/
Connection = InternetConnect(Initialize,pData->url,INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
/*open up an HTTP request*/
//récupère heure système
time(&myTime);
tm_ptr = localtime(&myTime); 
strftime(timestamp,16,"%Y%m%d%H%M%S",tm_ptr);
sprintf(adresse, "%s?site=%s&date=%s",pData->script,pData->site,timestamp);
//sprintf(adresse, "%s?site=%s&timestamp=",pData->script,pData->site);
File = HttpOpenRequest(Connection,NULL,adresse,NULL,NULL,NULL,0,0);
/*close file , terminate server connection and
deinitialize the wininet library*/
HttpSendRequest(File,NULL,0,NULL,0);
InternetCloseHandle(Connection);
InternetCloseHandle(Initialize);
 
 
 
	   /* Pause between loops. 15 minutes*/
        Sleep( pData->loopMs ); 
//        Sleep( 900000L ); 
 
    }
    /* _endthread given to terminate */
    _endthread();
 
}
mes lignes a placer

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
//effectuer toutes les 5 min
 
   while(1)
   {
   Sleep(5);
   modifXML(timer(recupDate(),recupheure()));
   }
je vous remercie d avance de votre aide