Bonjour je suis pas un crac en C mais j'ai corrige un programme qui cre un serveur pipe et recupere des message dans le pipe pour les ecrire dans un fichier texte. Tout se passe bien jusqu'a la deconnexion ou j'ai un message d'erreur : The instruction at "0x7c(#$@^d" referenced memory at "0x65567473". The memory could not be read. Click OK to terminate......"
Voici le code de la'pplication console du pipe sous DEV C++ :
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
 *@title: Pipe server
 *
 *@author: AICARDI David 
 *
 *@description: This program establishes a pipe server
 */
 
//Include Standard libraries
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <stdio.h>
#include <time.h>
 
//Include Data
#include "phIAutomaticTestTool_Data.h"
 
//Overall variables
FILE *gpphAutomaticTestTool_ErrorFile;	
int gphAutomaticTestTool_MessageCommand;    
DWORD gphAutomaticTestTool_ThreadId;
HANDLE gphAutomaticTestTool_HandleThread; 
HANDLE gphAutomaticTestTool_HandleEventPipe, gphAutomaticTestTool_HandlePipe;
char *pMessageToSend;
 
//Function prototypes
int phAutomaticTestTool_PipeServer(char pipeName[]);
DWORD WINAPI phAutomaticTestTool_Process_PipeReader(LPVOID lpParam);
DWORD WINAPI phAutomaticTestTool_Process_PipeWritter(LPVOID lpParam);
int phAutomaticTestTool_CloseThread();
int phAutomaticTestTool_InitErrorFile();  
int phAutomaticTestTool_CloseFile();
int phAutomaticTestTool_PrintErrorPipe(int errorCode);
 
/**
 *@title: phAutomaticTestTool_PipeServer
 *
 *@author: AICARDI David
 *
 *@description: This method opens Pipe and waits a client connection
 *
 *@return: int : 0
 *
 *@param: char pipeName []: Pipe name to open (default: ////.//PIPE//Pipe1)
 */
int phAutomaticTestTool_PipeServer(char pipeName [])
{
	//local variable
	int closeStatus;
	printf("passage dans PipeServer --> creation du pipe\n");
	/*Create Pipe*/
  	gphAutomaticTestTool_HandlePipe = CreateNamedPipe ( pipeName, // Pipe Name 
                              PIPE_ACCESS_DUPLEX , // Access Mode 
                              PIPE_TYPE_MESSAGE | //message type pipe 
                              PIPE_READMODE_MESSAGE | //message-read mode 
                              PIPE_WAIT,  // blocking mode
                              PIPE_UNLIMITED_INSTANCES, // max. instances 
                              PH_AUTOMATICTESTTOOL_BUFSIZE, // output buffer size 
                              PH_AUTOMATICTESTTOOL_BUFSIZE, // input buffer size 
                              PH_AUTOMATICTESTTOOL_PIPE_TIMEOUT, // client time-out 
                              NULL); // no security attribute     
 
    	if (INVALID_HANDLE_VALUE == gphAutomaticTestTool_HandlePipe) 
    	{
    		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CREATE_PIPE);
    		return 1;
    	}
    	else
    	{    
             //Start Threads
             SetEvent(gphAutomaticTestTool_HandleEventPipe);
    		//Disconnect Handle
		closeStatus = DisconnectNamedPipe(gphAutomaticTestTool_HandlePipe); 
		if(0 == closeStatus)
		{
			phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CLOSE_HANDLE);
		}
 
    		/* Wait a client connection */   
	 	while (0 == ConnectNamedPipe(gphAutomaticTestTool_HandlePipe, NULL)) { }
	 }
	 return 0; 
}
 
/**
 *@title: phAutomaticTestTool_Process_PipeReader
 *
 *@author: AICARDI David
 *
 *@description: This process reads Commands from Pipe
 *
 *@return: DWORD : 0
 *
 *@param: LPVOID lpParam : Process Number
 */
DWORD WINAPI phAutomaticTestTool_Process_PipeReader(LPVOID lpParam)
{
	//local variables
	BOOL terminate = FALSE, fileRead;
	char messageToReceive[PH_AUTOMATICTESTTOOL_BUFSIZE];
	char eventError;
	DWORD bytesRead;
	int written, i, cursor = 0;
 
	//Wait Event to start
	WaitForSingleObject(gphAutomaticTestTool_HandleEventPipe,INFINITE);
 
	while ( FALSE == terminate )
   	{            
            if(INVALID_HANDLE_VALUE == gphAutomaticTestTool_HandlePipe)
    		{ 
         		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CREATE_PIPE);
    		}
    		//Pipe created
    		else
    		{
    			//Read from Pipe
          		fileRead  = ReadFile (gphAutomaticTestTool_HandlePipe,
                  messageToReceive,
                  PH_AUTOMATICTESTTOOL_BUFSIZE,
                  &bytesRead,
                  NULL); 
 
                //Command received              
                if(fileRead && (PH_AUTOMATICTESTTOOL_COMMAND_OFFSET < bytesRead) && (PH_AUTOMATICTESTTOOL_MAX_COMMAND_SIZE >= (bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET)))
                {    
            		//Stop Pipe Server
            		if((char)PH_AUTOMATICTESTTOOL_STOP_COMMAND_CODE == (char)messageToReceive[1])
            		{
                        phAutomaticTestTool_OpenFile();
                        cursor = phAutomaticTestTool_SetIndex();
 
            			//Pull pipe buffer
            			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
            			pMessageToSend = malloc(bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET);
            			if(NULL == pMessageToSend)
            			{
            				phAutomaticTestTool_PrintErrorPipe(ERROR_MEM);
            				return 1;
            			}
    	                written = fwrite(PH_AUTOMATICTESTTOOL_STOP,
                        1,strlen(PH_AUTOMATICTESTTOOL_STOP),
                        gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        for(i = 0; i < bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET; i++)
                        {
                              sprintf(&pMessageToSend[i],
                              "%c",
                              messageToReceive[PH_AUTOMATICTESTTOOL_COMMAND_OFFSET+i]);
                        }
                        written = fwrite(pMessageToSend,1,strlen(pMessageToSend),gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
               	        printf("\nEnd Script: %s\n",pMessageToSend);
 
               	        written = fwrite(PH_AUTOMATICTESTTOOL_BACK_2,
                           1,
                           strlen(PH_AUTOMATICTESTTOOL_BACK_2),
                           gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
 
                        phAutomaticTestTool_CloseFile();
                        Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 );
                        free(pMessageToSend);
 
           			}
            		else if((char)PH_AUTOMATICTESTTOOL_START_COMMAND_CODE == (char)messageToReceive[1])
            		{
            	        //Pull pipe buffer
            			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
						phAutomaticTestTool_OpenFile();
						cursor = phAutomaticTestTool_SetIndex();
 
            			pMessageToSend = malloc(bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET);
            			if(NULL == pMessageToSend)
            			{
            				phAutomaticTestTool_PrintErrorPipe(ERROR_MEM);
            				return 1;
            			}
    	                written = fwrite(PH_AUTOMATICTESTTOOL_START,
                        1,
                        strlen(PH_AUTOMATICTESTTOOL_START),
                        gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        for(i = 0; i < bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET; i++)
                        {
                              sprintf(&pMessageToSend[i],
                              "%c",
                              messageToReceive[PH_AUTOMATICTESTTOOL_COMMAND_OFFSET+i]);
                        }
                        written = fwrite(pMessageToSend,1,strlen(pMessageToSend),gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
               	        printf("\nStart Script: %s\n",pMessageToSend);
               	        written = fwrite(PH_AUTOMATICTESTTOOL_BACK_2,
                           1,
                           strlen(PH_AUTOMATICTESTTOOL_BACK_2),
                           gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        phAutomaticTestTool_CloseFile(); 
                        Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 );
                        free(pMessageToSend);
 
           			}
           			else if((char)PH_AUTOMATICTESTTOOL_CONNECTION_COMMAND_CODE == (char)messageToReceive[1])
            		{
            			//Pull pipe buffer
            			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
						phAutomaticTestTool_OpenFile();
						cursor = phAutomaticTestTool_SetIndex();
 
            			pMessageToSend = malloc(bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET);
            			if(NULL == pMessageToSend)
            			{
            				phAutomaticTestTool_PrintErrorPipe(ERROR_MEM);
            				return 1;
            			}
    	                written = fwrite(PH_AUTOMATICTESTTOOL_CONNECTION,
                        1,
                        strlen(PH_AUTOMATICTESTTOOL_CONNECTION),
                        gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        for(i = 0; i < bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET; i++)
                        {
                              sprintf(&pMessageToSend[i],
                              "%c",
                              messageToReceive[PH_AUTOMATICTESTTOOL_COMMAND_OFFSET+i]);
                        }
                        written = fwrite(pMessageToSend,
                        1,strlen(pMessageToSend),
                        gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
               	        printf("\nConnection Initialized: %s\n",pMessageToSend);
               	        written = fwrite(PH_AUTOMATICTESTTOOL_BACK_2,
                           1,
                           strlen(PH_AUTOMATICTESTTOOL_BACK_2),
                           gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        phAutomaticTestTool_CloseFile();
                        Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 );
                        free(pMessageToSend);
					}
           			else if((char)PH_AUTOMATICTESTTOOL_STOP_COMMAND_BATCH_CODE == (char)messageToReceive[1])
           			{
                        //Pull pipe buffer
            			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
						phAutomaticTestTool_OpenFile();
						cursor = phAutomaticTestTool_SetIndex();
 
            			pMessageToSend = malloc(bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET);
            			if(NULL == pMessageToSend)
            			{
            				phAutomaticTestTool_PrintErrorPipe(ERROR_MEM);
            				return 1;
            			}
    	                written = fwrite(PH_AUTOMATICTESTTOOL_ENDBATCH,
                        1,
                        strlen(PH_AUTOMATICTESTTOOL_ENDBATCH),
                        gpphAutomaticTestTool_ErrorFile);
 
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        for(i = 0; i < bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET; i++)
                        {
                              sprintf(&pMessageToSend[i],
                              "%c",
                              messageToReceive[PH_AUTOMATICTESTTOOL_COMMAND_OFFSET+i]);
                        }
                        written = fwrite(pMessageToSend,1,strlen(pMessageToSend),gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
               	        printf("\nEnd Batch: %s\n",pMessageToSend);
               	        written = fwrite(PH_AUTOMATICTESTTOOL_BACK_2,
                           1,
                           strlen(PH_AUTOMATICTESTTOOL_BACK_2),
                           gpphAutomaticTestTool_ErrorFile);
                        if(0 == written)
       	                {
	                          phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
               	        }
                        phAutomaticTestTool_CloseFile(); 
                        free(pMessageToSend);                        
                        terminate = TRUE;
 
                    }
            		//Post gphAutomaticTestTool_MessageCommand message
            		else
            		{
            			//Pull pipe buffer
            			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
            			phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_READ_PIPE);
                        Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 );
                    }
		       } 
		       //BAD message format
		       else if(fileRead && (PH_AUTOMATICTESTTOOL_COMMAND_OFFSET >= bytesRead))
		       {
                    //Pull pipe buffer
        			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
	            	phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_READ_PIPE);  
	            	Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 );
 
		       }	   
		       else if(fileRead && (PH_AUTOMATICTESTTOOL_MAX_COMMAND_SIZE < (bytesRead-PH_AUTOMATICTESTTOOL_COMMAND_OFFSET)))
		       {
                    //Pull pipe buffer
        			FlushFileBuffers(gphAutomaticTestTool_HandlePipe);
 
	            	phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_COMMAND_SIZE_TOO_LONG);  
            	    Sleep ( PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1 ); 	
		       } 	
    		}
    	}
 
	return 0;
}
 
/**
 *@title: phAutomaticTestTool_SetIndex
 *
 *@author: AICARDI David
 *
 *@description: This method Close all threads and Pipe Server
 *
 *@return: int : 0
 */
int phAutomaticTestTool_SetIndex()
{	
    int cursor = 0;
    fseek(gpphAutomaticTestTool_ErrorFile, 0, SEEK_END);
	cursor = ftell(gpphAutomaticTestTool_ErrorFile);
 
	return cursor;
}
 
/**
 *@title: phAutomaticTestTool_CloseThread
 *
 *@author: AICARDI David
 *
 *@description: This method Close all threads and Pipe Server
 *
 *@return: int : 0
 */
int phAutomaticTestTool_CloseThread()
{	
	//local variables
	int closeStatus;
 
    //Disconnect Named pipe Handle
	closeStatus = DisconnectNamedPipe(gphAutomaticTestTool_HandlePipe); 
	if(0 == closeStatus)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CLOSE_HANDLE);
		return 1;
	}
	//Close all threads
	closeStatus = CloseHandle(gphAutomaticTestTool_HandleThread);
	if(0 == closeStatus)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CLOSE_HANDLE);
		return 1;
	} 
 
	//Close Thread Handle
	closeStatus = CloseHandle(gphAutomaticTestTool_HandleEventPipe);
	if(0 == closeStatus)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CLOSE_HANDLE);
		return 1;
	}
 
	//Close Pipe Handle
	closeStatus = CloseHandle(gphAutomaticTestTool_HandlePipe);
    	if(0 == closeStatus)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_CLOSE_HANDLE);
		return 1;
	}
 
	return 0;
}
 
/**
 *@title: phAutomaticTestTool_CloseFile
 *
 *@author: AICARDI David
 *
 *@description: This method closes file
 *
 *@return: int : 0
 *
 *@param: u32 errorCode : Error Code
 */
int phAutomaticTestTool_CloseFile()
{
	//local variable 
	int fileClosed;
 
	/*Close Error File*/
	fileClosed = fclose(gpphAutomaticTestTool_ErrorFile); 
	if(0 != fileClosed)
	{
		return 1;
	}
 
	return 0;
}
 
/**
 *@title: phAutomaticTestTool_OpenFile
 *
 *@author: AICARDI David
 *
 *@description: This method prints Error string to console
 *
 *@return: int : 0
 *
 *@param: u32 errorCode : Error Code
 */
int phAutomaticTestTool_OpenFile()
{
    int initFileStatus;
    //Open Error File in Write access
	gpphAutomaticTestTool_ErrorFile=fopen(PH_AUTOMATICTESTTOOL_ERROR_FILE_NAME, "w");
	if(NULL  == gpphAutomaticTestTool_ErrorFile)
	{
        phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_OPEN_ERROR_FILE);
        phAutomaticTestTool_CloseFile();
        return 0;
	}
}
 
 
 
/**
 *@title: phAutomaticTestTool_PrintErrorPipe
 *
 *@author: AICARDI David
 *
 *@description: This method prints Error string to console
 *
 *@return: int : 0
 *
 *@param: u32 errorCode : Error Code
 */
int phAutomaticTestTool_PrintErrorPipe(int errorCode)
{
	//local variables
	int written;
 
	switch(errorCode)
	{
		case (int)PH_AUTOMATICTESTTOOL_CREATE_PIPE:{
 
			printf(PH_AUTOMATICTESTTOOL_OPEN_PIPE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_OPEN_PIPE,1,strlen(PH_AUTOMATICTESTTOOL_OPEN_PIPE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_READ_PIPE:{
 
			printf(PH_AUTOMATICTESTTOOL_READ);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_READ,1,strlen(PH_AUTOMATICTESTTOOL_READ),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_COMMAND_SIZE_TOO_LONG:{
 
			printf(PH_AUTOMATICTESTTOOL_COMMAND_TOO_LONG);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_COMMAND_TOO_LONG,1,strlen(PH_AUTOMATICTESTTOOL_COMMAND_TOO_LONG),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_OPEN_ERROR_FILE:{
 
			printf(PH_AUTOMATICTESTTOOL_OPEN_FILE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_OPEN_FILE,1,strlen(PH_AUTOMATICTESTTOOL_OPEN_FILE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_WRITE_TO_FILE:{
 
			printf(PH_AUTOMATICTESTTOOL_WRITE_TO_ERROR_FILE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_WRITE_TO_ERROR_FILE,1,strlen(PH_AUTOMATICTESTTOOL_WRITE_TO_ERROR_FILE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_THREAD_PIPE:{
 
			printf(PH_AUTOMATICTESTTOOL_ERROR_THREAD_PIPE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_ERROR_THREAD_PIPE,1,strlen(PH_AUTOMATICTESTTOOL_ERROR_THREAD_PIPE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_THREAD_EVENT:{
 
			printf(PH_AUTOMATICTESTTOOL_ERROR_EVENT);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_ERROR_EVENT,1,strlen(PH_AUTOMATICTESTTOOL_ERROR_EVENT),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_CLOSE_HANDLE:{
 
			printf(PH_AUTOMATICTESTTOOL_ERROR_CLOSE_HANDLE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_ERROR_CLOSE_HANDLE,1,strlen(PH_AUTOMATICTESTTOOL_ERROR_CLOSE_HANDLE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
		case (int)PH_AUTOMATICTESTTOOL_REGISTER_MESSAGE:{
 
			printf(PH_AUTOMATICTESTTOOL_ERROR_REGISTER_MESSAGE);
 
			written = fwrite(PH_AUTOMATICTESTTOOL_ERROR_REGISTER_MESSAGE,1,strlen(PH_AUTOMATICTESTTOOL_ERROR_REGISTER_MESSAGE),gpphAutomaticTestTool_ErrorFile);
			if(0 == written)
			{
				phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_WRITE_TO_FILE);
			}
 
			break;
		}
	}
	return 0;
}
 
 
/**
 *@title: main
 *
 *@author: AICARDI David
 *
 *@description: Main of program
 *
 *@return: int : 0
 *
 *@param: int argc : Argument Number
 *@param: char** argv : Arguments
 */
int main(int argc, char** argv)
{
	//local variables
	char pipeName[PH_AUTOMATICTESTTOOL_PIPE_NAME_SIZE];
	int argCounter, pipeStatus, threadClosed, initFileStatus;
 
	//Init Handle
	gphAutomaticTestTool_HandleEventPipe = INVALID_HANDLE_VALUE;
 
	//Init default value
	strcpy(pipeName,PH_AUTOMATICTESTTOOL_PIPE_NAME);
 
	//Management of line command arguments
	for(argCounter=0;argCounter<argc;argCounter++)
	{
		if((0 == strcmp(PH_AUTOMATICTESTTOOL_PIPE_OPTION,argv[argCounter])) && (argc > argCounter))
		{
			strcpy(pipeName,PH_AUTOMATICTESTTOOL_PIPE_ROOT);
			strcat(pipeName,argv[argCounter+1]);
		}
	}
 
	//Set a value for each message
	gphAutomaticTestTool_MessageCommand = RegisterWindowMessage(PH_AUTOMATICTESTTOOL_MESSAGE_COMMAND_STRING);
	if(0 == gphAutomaticTestTool_MessageCommand)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_REGISTER_MESSAGE);
		phAutomaticTestTool_CloseFile();
		return 1;
	}
 
	//Create Handle
	gphAutomaticTestTool_HandleEventPipe=CreateEvent(NULL,TRUE,FALSE,NULL);
	if(NULL == gphAutomaticTestTool_HandleEventPipe)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_THREAD_EVENT);
		return 1;
	}
 
	/*Send - Receive BSE message*/
	gphAutomaticTestTool_HandleThread=CreateThread(NULL,0,phAutomaticTestTool_Process_PipeReader,(LPVOID)PH_AUTOMATICTESTTOOL_PIPE_READER_ID,0,&gphAutomaticTestTool_ThreadId);
	if(INVALID_HANDLE_VALUE == gphAutomaticTestTool_HandleThread)
	{
		phAutomaticTestTool_PrintErrorPipe(PH_AUTOMATICTESTTOOL_THREAD_PIPE);
		phAutomaticTestTool_CloseFile();
		return 1;
	}
 
	/* Start Pipe Server */
	pipeStatus = phAutomaticTestTool_PipeServer(pipeName);
	if(1 == pipeStatus)
	{
		phAutomaticTestTool_CloseFile();
		return 1;
	}
 
	/* Wait end of PipeReader Thread */
	WaitForSingleObject(gphAutomaticTestTool_HandleThread,INFINITE);
 
	//Close All threads
	threadClosed = phAutomaticTestTool_CloseThread();
	if(1 == threadClosed)
	{
		phAutomaticTestTool_CloseFile();
		return 1;
	}
 
	/*Close File*/
	phAutomaticTestTool_CloseFile();
 
	return 0;
}
et son *.h:
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
#ifndef PHIAUTOMATICTESTTOOL_DATA_H
#define PHIAUTOMATICTESTTOOL_DATA_H
 
//Thread variables 
#define PH_AUTOMATICTESTTOOL_THREAD_NUMBER             1
#define PH_AUTOMATICTESTTOOL_SLEEP_TIMER_1             100
#define PH_AUTOMATICTESTTOOL_SLEEP_TIMER_2             200
#define PH_AUTOMATICTESTTOOL_MESSAGE_COMMAND_STRING    "MESSAGE_COMMAND"
 
//Pipe variables
#define PH_AUTOMATICTESTTOOL_PIPE_READER_ID           0
#define PH_AUTOMATICTESTTOOL_MIN_TO_FREE              2
#define PH_AUTOMATICTESTTOOL_BUFFER_TIME_SIZE         2
#define PH_AUTOMATICTESTTOOL_EVENT_OFFSET             3
#define PH_AUTOMATICTESTTOOL_COMMAND_OFFSET           4
#define PH_AUTOMATICTESTTOOL_OPERATION_TYPE_OFFSET    6
#define PH_AUTOMATICTESTTOOL_HEADER_SIZE              8
#define PH_AUTOMATICTESTTOOL_PIPE_ROOT_SIZE           13
#define PH_AUTOMATICTESTTOOL_PIPE_NAME_SIZE           20
#define PH_AUTOMATICTESTTOOL_MAX_COMMAND_SIZE         252
#define PH_AUTOMATICTESTTOOL_FACTOR                   256
#define PH_AUTOMATICTESTTOOL_BUFSIZE                  4096
#define PH_AUTOMATICTESTTOOL_PIPE_TIMEOUT             5000
#define PH_AUTOMATICTESTTOOL_SET_POINTER_REQ          14616
#define PH_AUTOMATICTESTTOOL_DELETE_POINTER_REQ       14616
#define PH_AUTOMATICTESTTOOL_PIPE_NAME                "\\\\.\\PIPE\\Pipe1"
#define PH_AUTOMATICTESTTOOL_PIPE_ROOT                "\\\\.\\PIPE\\"
#define PH_AUTOMATICTESTTOOL_PIPE_OPTION              "-pipe"
#define PH_AUTOMATICTESTTOOL_ERROR_FILE_NAME          "PipeMessages.txt"
#define PH_AUTOMATICTESTTOOL_STOP_COMMAND_CODE        0xFF
#define PH_AUTOMATICTESTTOOL_STOP_COMMAND_BATCH_CODE  0xFE
#define PH_AUTOMATICTESTTOOL_START_COMMAND_CODE       0xFD
#define PH_AUTOMATICTESTTOOL_CONNECTION_COMMAND_CODE  0xFC
 
//Error Code
#define PH_AUTOMATICTESTTOOL_CREATE_PIPE              0x02
#define PH_AUTOMATICTESTTOOL_READ_PIPE                0x08
#define PH_AUTOMATICTESTTOOL_PORT_NAME                0x0A
#define PH_AUTOMATICTESTTOOL_COMMAND_SIZE_TOO_LONG    0x11
#define PH_AUTOMATICTESTTOOL_OPEN_ERROR_FILE          0x12
#define PH_AUTOMATICTESTTOOL_WRITE_TO_FILE            0x13 
#define PH_AUTOMATICTESTTOOL_THREAD_PIPE              0x14
#define PH_AUTOMATICTESTTOOL_CLOSE_HANDLE             0x16
#define PH_AUTOMATICTESTTOOL_THREAD_EVENT             0x17
#define PH_AUTOMATICTESTTOOL_CLOSE_FILE               0x18
#define PH_AUTOMATICTESTTOOL_REGISTER_MESSAGE         0x19
#define PH_AUTOMATICTESTTOOL_SET_POINTER              0x1B
 
//Event error Code
#define PH_AUTOMATICTESTTOOL_BAD_BSE_COMMAND              (char)0x01
#define PH_AUTOMATICTESTTOOL_MEMORY_PROBLEM               (char)0x03
#define PH_AUTOMATICTESTTOOL_EVENT_PROBLEM                (char)0x04
#define ERROR_MEM                                         (char)0x05
 
//BSE Code
#define PH_AUTOMATICTESTTOOL_BSE_CODE_OFFSET             0
#define PH_AUTOMATICTESTTOOL_BSE_OPCODE_0_OFFSET         1
#define PH_AUTOMATICTESTTOOL_BSE_OPCODE_1_OFFSET         2
#define PH_AUTOMATICTESTTOOL_BSE_COMMAND_LENGTH_OFFSET   3
#define PH_AUTOMATICTESTTOOL_ERROR_CODE_OFFSET           3
#define PH_AUTOMATICTESTTOOL_SIZE_BYTETOSTRING           2
 
//Event Error Pipe String
#define PH_AUTOMATICTESTTOOL_OPEN_PIPE               "Error.Pipe.FailedToOpen \r\n"
#define PH_AUTOMATICTESTTOOL_READ                    "Error.Pipe.FailedToRead \r\n"
#define PH_AUTOMATICTESTTOOL_COMMAND_TOO_LONG        "Error.Pipe.CommandSizeTooLong \r\n"
#define PH_AUTOMATICTESTTOOL_ERROR_THREAD_PIPE       "Error.ThreadPipe.FailedToCreate \r\n"
#define PH_AUTOMATICTESTTOOL_ERROR_CLOSE_HANDLE      "Error.Handle.FailedToClose \r\n"
#define PH_AUTOMATICTESTTOOL_ERROR_EVENT             "Error.Thread.FailedToCreateEvent \r\n"
#define PH_AUTOMATICTESTTOOL_ERROR_REGISTER_MESSAGE  "Error.Window.FailedToRegisterWindowsMessage \r\n"
#define PH_AUTOMATICTESTTOOL_OPEN_FILE               "Error.ErrorFile.FailedToOpen \r\n"
#define PH_AUTOMATICTESTTOOL_WRITE_TO_ERROR_FILE     "Error.ErrorFile.FailedToWrite \r\n"
#define PH_AUTOMATICTESTTOOL_ERROR_CLOSE_FILE        "Error.ErrorFile.FailedToClose \r\n"
#define PH_AUTOMATICTESTTOOL_INIT_ERROR_FILE         "\r\n ****    STATUS FILE   **** \r\n \r\n ****   SCRIPT PASSED   **** \r\n \r\n"
#define PH_AUTOMATICTESTTOOL_BACK                    "\r\n\r\n"
#define PH_AUTOMATICTESTTOOL_BACK_2                  "\r\n"
#define PH_AUTOMATICTESTTOOL_START                   "Script_Started_"
#define PH_AUTOMATICTESTTOOL_STOP                    "Script_Finished_"
#define PH_AUTOMATICTESTTOOL_CONNECTION              "Connection_initialized_"
#define PH_AUTOMATICTESTTOOL_ENDBATCH                "END_BATCH_FILE_"
 
extern char *pMessageToSend;
 
#endif

Si quelqu'un peut jeter un oeil et me dire ou tout ca cloche ?

Merci d'avance a bientot.