title: MapViewOfFile-OpenFileMapping (need help)
body: <p>``i'm try to do my homework, but i cant continue,need your help.
  i have two processes which use same file (log.txt). when i tyr to write something at the same time, it overwrites and ONLY ONE  process write to txt. </p>
  
  <p>This my creater process,</p>
  
  <pre><code>enter code here
       #include &lt;Windows.h&gt;
      #include &lt;stdlib.h&gt;
     #include &lt;stdio.h&gt;
         #include "..\\def.h"
  
  
  
        int main(){
  
  HANDLE hReadPipe;
  HANDLE hWritePipe;
  HANDLE hMMap;
  DWORD bytesWritten;
  HANDLE hFile;
  STARTUPINFO si_e;
  PROCESS_INFORMATION pi_e;
  
  STARTUPINFO si_d;
  PROCESS_INFORMATION pi_d;
  
  
  SECURITY_ATTRIBUTES sa;
  
  sa.bInheritHandle = TRUE;
  sa.lpSecurityDescriptor = NULL;
  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  
      if(!CreatePipe(&amp;hReadPipe,&amp;hWritePipe, &amp;sa,0))
      fprintf(stderr,"Unable to create pipe.\n");
  ZeroMemory(&amp;si_e, sizeof(STARTUPINFO));
  ZeroMemory(&amp;si_d, sizeof(STARTUPINFO));
  
  
  
  if((hFile =CreateFile(  FILE_NAME,
                          GENERIC_READ|GENERIC_WRITE,
                          FILE_SHARE_READ|FILE_SHARE_WRITE,
                          NULL,
                          OPEN_ALWAYS,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL)) 
                                  == INVALID_HANDLE_VALUE){
      fprintf(stderr,"PARENT: Unable to open file %s: %d\n",FILE_NAME,GetLastError());
      exitPrompt();
  }
  
  if((hMMap = CreateFileMapping(      hFile,
                                  NULL,
                                  PAGE_READWRITE,
                                  0,
                                  BUFF_SIZE*2,
                                  "Global\\file_log_txt")) 
                                              == NULL){
      fprintf(stderr,"PARENT: Unable to create memory mapping: %d\n",GetLastError());
  
  }
  
  
  
  si_e.cb = sizeof(si_e);
  si_e.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  si_e.hStdOutput =hWritePipe;
  si_e.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  si_e.dwFlags = STARTF_USESTDHANDLES;
  
          if(!CreateProcess(  NULL,
                      "..\\Debug\\Encrypter.exe",
                      NULL,
                      NULL,
                      TRUE,
                      CREATE_NEW_CONSOLE,
                      NULL,
                      NULL,
                      &amp;si_e,
                      &amp;pi_e))
      fprintf(stderr,"Unable to create child process.\n");
  
  
  
  
  
  
  
  
  si_d.cb = sizeof(si_d);
  si_d.hStdInput= hReadPipe;
  si_d.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  si_d.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  si_d.dwFlags = STARTF_USESTDHANDLES;
  
           if(!CreateProcess( NULL,
                      "..\\Debug\\Decrypter.exe",
                      NULL,
                      NULL,
                      TRUE,
                      CREATE_NEW_CONSOLE,
                      NULL,
                      NULL,
                      &amp;si_d,
                      &amp;pi_d))
      fprintf(stderr,"Unable to create child process.\n");
  
  
  
  Sleep(5000000);
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>1.Process:</p>
  
  <pre><code>enter code here
         #include &lt;stdlib.h&gt;
       #include &lt;stdio.h&gt;
        #include &lt;Windows.h&gt;
            #include "..\\def.h"
                int main()
         {
  int i;
  HANDLE writeHandle;
  HANDLE hFile, hMMap;
  DWORD bytesToWrite,bytesWritten,a=50;
  char * pFile, *start;
  char message[50][15];   
  char code[]=" ";
  int index;
  writeHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  
      if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
      fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
  
  }
  
  if( ( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
  {
      fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
  
  }
  
      //student number =160201007(0+0+7=7)  +7encrypter
  while(1){
  
      gets(code);
      for(  i=0;i&lt;strlen(code);i++){
      code[i]=code[i]+7;
  }
      bytesToWrite = strlen(code);
      bytesToWrite++;
      WriteFile(writeHandle,code,bytesToWrite,&amp;bytesWritten,NULL);
  
  }
  start = pFile;
  *(pFile)=*(pFile)+50;
  while(pFile &lt; start+ 100){
          *(pFile=pFile+80);
      *(pFile++) = 'A';
      break;  
  }
  system("pause");
  return 0;
               }
  </code></pre>
  
  <p>2.process:</p>
  
  <pre><code>enter code here
           #include &lt;stdlib.h&gt;
              #include &lt;stdio.h&gt;
             #include &lt;Windows.h&gt;
                  #include "..\\def.h"
            int main()
  
            { 
  
  char message[80];
  int index,i;
  HANDLE hFile, hMMap;
  char * pFile, *start;
      if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
      fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
  
  }
  
  if( ( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
  {
      fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
  
  }
  
  
      for(index = 0;(message[index]=getchar()) != 0;index++);
      for(  i=0;i&lt;strlen(message);i++){
      message[i]=message[i]-7;
                   }
      printf("Decrypter : %s\n",message);
  start = pFile;
  while(pFile &lt; start + 50){
  
      *(pFile++) = 'A';
      break;
  
  }
  system("pause");
  return 0;
             }
  </code></pre>
  
  <p>"..\def.h"</p>
  
  <pre><code>enter code here
  
            #ifndef __DEF_H
            #define __DEF_H
            #include &lt;Windows.h&gt;
             #include &lt;stdio.h&gt;
            //#include &lt;stdlib.h&gt;
            #define BUFF_SIZE 1024 *64
             #define FILE_NAME "log.txt"
  
  
            void exitPrompt(){
        system("pause");
       exit(0);
                }
                    #endif
  </code></pre>