Hi everybody,

The progam below outputs three strings. It works correctly but I have to use CreateFile and CloseHandle three times. When I use CreateFile before outputting the first string and CloseHandle before ExitProcess the program prints only one string. Is there any way to use CreateFile and CloseHandle only once?

Thank you.
 
.data                                                       
      FileName      db "result.txt",0
      mes            db "Can't create file",0
      message1    db "String 1",13,10,0
      message2    db "String 2",13,10,0
      message3    db "String 3",13,10,0
     
.data?
      lpFileName  dd ?
      strLength   dd ?
      lpAddr      dd ?
      dwBytes      dw ? 
      hFile        HANDLE ?
      lpbuffer    dw ?
.code                                                       
      start:

      invoke  CreateFile, ADDR FileName,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
              NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
      mov   hFile, eax
      .IF (eax==INVALID_HANDLE_VALUE)
          invoke  MessageBox,0,ADDR mes,ADDR mes,0
          invoke  ExitProcess,0         
      .endif
      ; first message
      invoke  lstrlen,ADDR message1
      mov   strLength,eax
      invoke SetFilePointer,hFile,0,0, FILE_END
      invoke WriteFile, hFile, ADDR message1, strLength, addr dwBytes,NULL
      invoke CloseHandle, hFile
     
      ; second message
      invoke  CreateFile, ADDR FileName,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
              NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
      mov   hFile, eax
      invoke  lstrlen,ADDR message2
      mov   strLength,eax
      invoke SetFilePointer,hFile,0,0, FILE_END
      invoke WriteFile, hFile, ADDR message2, strLength, addr dwBytes,NULL
      invoke CloseHandle, hFile
                       
      ; third message
      invoke  CreateFile, ADDR FileName,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
              NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
      mov   hFile, eax
      invoke  lstrlen,ADDR message3
      mov   strLength,eax
      invoke SetFilePointer,hFile,0,0, FILE_END
      invoke WriteFile, hFile, ADDR message3, strLength, addr dwBytes,NULL
      invoke CloseHandle, hFile
     
invoke  ExitProcess,0                                 
end start

Posted on 2007-01-04 04:19:39 by approxime
Dunno why you're having trouble with consecutive writes - see attached file, it works :)
Attachments:
Posted on 2007-01-04 06:07:45 by f0dder
Thank you very much f0dder. I really appreciate your help. :D
Posted on 2007-01-04 06:27:59 by approxime
No problem :)

Btw, if you still have the code for the "doesn't work" program, post it - might be interesting troubleshooting it and finding the problem.
Posted on 2007-01-04 15:13:19 by f0dder