News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

CreateFile and manipulations files problems

Started by statismeles, August 31, 2005, 08:55:34 PM

Previous topic - Next topic

statismeles

Hello, i'm sorry my english isn't correct, i'm french  :'(
I have build masm32 win32 application. One element menu open dialogbox to create, read and modify file.
When i open dialogbox first time the create is ok and the file exist in path specified on the name file's.
When i have closed and re-open dialogbox the file, i can modify the file and i have'nt error.
When close and re-open dialogbox second time i have ERROR_PATH_NOT_FOUND error, but when i make windows search the file exist...
I don't anderstand what is problem

Thanks for your help

Darrel

Welcome statismeles,

Please provide some code.

Regards,

Darrel

statismeles

Thanks Darrel,

I have changed a sequence of code and the problems is before a ReadFile.

The party's of code is :

Fconnexions         db "c:\cnsipcom\fconxion.cic",0

1) for creation file :
invoke CreateFile,addr Fconnexions,\
                            GENERIC_READ or GENERIC_WRITE ,\
                            FILE_SHARE_WRITE,\ ;FILE_SHARE_READ or
                            NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,\ ; or FILE_FLAG_OVERLAPPED
                            NULL
mov DlgProc1hfile,eax
                            invoke WriteFile,DlgProc1hfile,DlgProc1filea,ecx,addr DlgProc1filenbw,NULL
(DlgProc1filea is memory buffer reserved adress, ecx value is lenght of buffer)
invoke CloseHandle,DlgProc1hfile

2) For reading file :
invoke CreateFile,addr Fconnexions,\
                            GENERIC_READ or GENERIC_WRITE ,\
                            FILE_SHARE_READ or FILE_SHARE_WRITE,\
                            NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\
                            NULL
mov DlgProc1hfile,eax
invoke ReadFile,DlgProc1hfile,DlgProc1filea,DlgProc1filenb,addr DlgProc1filenbr,NULL
(DlgProc1filea is memory buffer reserved adress, DlgProc1filenb is lenght of buffer)
invoke CloseHandle,DlgProc1hfile

3) For extension data's file :
invoke CreateFile,addr Fconnexions,\
                            GENERIC_READ or GENERIC_WRITE ,\
                            FILE_SHARE_READ or FILE_SHARE_WRITE,\
                            NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,\
                            NULL
                            mov DlgProc1hfile,eax
invoke WriteFile,DlgProc1hfile,DlgProc1mtampa,eax,addr DlgProc1filenbw,NULL
(DlgProc1mtampa is memory buffer reserved adress, eax value is lenght of buffer)
invoke CloseHandle,DlgProc1hfile

Actions in application :
- open dialogbox
The part 1) work correctly
- close dialogbox
- re-open dialogbox
The part 2) work correctly
- run modifications of data's
The part 3) doesn't work, i have ERROR_PATH_NOT_FOUND error

The CloseHandle isn't problem source, on first post my code, pen, read, write, close in same operation, but before dialogbox closing i do restart application to make correctly (only  one time) modifications file.
I work on this problem 2 weeks, minima 2 hours/day, but i have frenchfrogbrain...

Thank you very much Darrel,

Statis

PBrennick

statismeles,
I don't know about anybody else, but I cannot make heads nor tails of that stuff.  Maybe you should attach the project.  The problem with code snippets is you already have documented that you do not know what is happening so how can you be certain that that is the code we need to see?

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Mark Jones

Hi, I didn't understand that very well either but I'll try anyways. One thing you could try is putting some of these at various points in your code. Then you'll know exactly where the problem occurs. :)


    invoke Beep,100,1000
    ....
    invoke Beep,200,1000
    ....
    invoke Beep,300,1000
    ...etc...


Using EAX,EDX,ECX in the code as buffer lengths is asking for trouble, because any other APIs will overwrite these registers. Try using ESI/EDI instead (preserving is required) or just use another dw? variable.

Also make sure all file handles are closed properly, and any allocated memory is freed before exiting. Windows may not always support closing NULL file handles either, so it's best to test for null file handles and free only if true:


    .if DlgProc1hfile != 0
         invoke CloseHandle,DlgProc1hfile
    .endif


If all else fails, put a NOP or INT 3 in the code where you think the problem is, and trace through it using OllyDbg. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ToutEnMasm

Hello,(salut)
I see you are french,so have a look on my site.Editmasm use windbg as debugger.Dowload the two and then you will find where is the problems.
Editmasm use symbolic compilation , so you can debug directly your source file (more easy).
Have a look also at the page on windbg ,he explain how to instal windbg an use it (in french)
                                      ToutEnMasm

statismeles

Thank you, Paul, Marquez Jones and ToutEnMasm,

All operation CreateFile, ReadFile, WriteFile and CloseHandle in my code are following by :
invoke GetLastError
invoke IntAsc,eax
invoke MessageBox,DlgProc1hWnd,eax,addr DlgProc1txt,MB_OK
invoke IntAsc,DlgProc1filenbw
invoke MessageBox,DlgProc1hWnd,eax,addr DlgProc1txtwritea,MB_OK
IntAsc is personnal proc, to convert non-signed integer to string buffer with 0 ending, and return adresse buffer.
With this monitoring i can see if all operation are good.
I know the problem with registers and API call's, i have lost many hours to learn that.

I don't know to attach project on post...
The project is very long and i can't join all text...

"Also make sure all file handles are closed properly, and any allocated memory is freed before exiting."
With GetLastError i don't have error closed handle but with allocated memory i don't know...
The initial file is download in memory block (ReadFile operation), the modified data's arent in new block but the first block are free after saving new data's and after the CloseHandle...
I do trying to modified this point for make GlobalFree and GlobalUnlock before CloseHandle, and i go back after.

Note, i'm sorry my english is'nt good, in fisrt post i confused "before" and "after"  :'(

Best regards at alls,

Statis

Mark Jones

Hi Statis, another thing you could try is loading your app with MemProof. This will report any unfreed handles, memory, etc. The URL for it is in here: http://www.masmforum.com/simple/index.php?topic=431.msg17607#msg17607

There is also a  photo of MemProof two posts down. Have fun. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

PBrennick

statismeles,
To attach a project you need to click the Advanced Options link below the text box.  I really think you should attach the project because you seem to be good at all the trapping stuff used to locate errors so it sounds like you have a difficult one.  Sometimes, all it takes is a fresh pair of eyes.  There is no downside to obtaining help and we all want to help you.

If all else fails, email the project to me and I will post it for you.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

statismeles

Thank you for your help,Paul and Marquez Jones

I Have see MemProof but i don't anderstend the use, my english is very  :tdown

Thank you Paul for reading source proposition, i attach zip in this post.
This project is create with Radasm but i have save the files for use only with masm.

I have create a new winmain process to test if the problem is in parent process or in child dialogbox.
I have a same result, the first opening dialogbox create correctly file, the second opening read and extend correctly and after reopening the ERROR_PATH_NOT_FOUND is present.

Best regards at alls,
Statis



[attachment deleted by admin]

evlncrn8

hmm from looking at the snippets of code, all i can guess is that you are destroying the file handle in the next createfile call...

   mov DlgProc1hfile,eax

you are using DlgProc1hfile to store the file handle in both createfile calls...... so the second file open will destroy the file handle of the first, thus making it useless...

statismeles

Thank you evlncrn8,

I don't anderstand, if i call CreateFile, make the operations read and/or write and after CloseHandle, i can recall one CreateFile ?
After closing dialogbox, i can read the file with another exe...
In the last source after all CreateFile calls i call CloseHandle, i have try another way :
- setting the Dword position of handle in general .data?
- make verification operations to see if parent have open handle
- in dialogbox, verification calling Createfile only if handle don't exist
- make operation to call only one time CreateFile
- in WM_CLOSE parent if handle exist, call closeHandle
I HAVE SAME PROBLEM :
- i open dialog and the file creation work correctly, after i close dialogbox
- i reopen dialog box and the first extension data file write correctly, after i close dialogbox
- i reopen dialogboxand and i have ERROR_PATH_NOT_FOUND (code error=3) before make second extension data file

I have return to first source but with modified code for open messagebox error only when one error is detected.

Best Regards,
Statis

[attachment deleted by admin]

evlncrn8

oops, i didnt see the closehandle calls heh, thats what i get for posting at 4am in the morning, sorry about that
i will go through your code in the morning when i wake up and see if i can find the issue ;)

statismeles

Ten days later, no solution ???

Before i be alone, after i stay alone, but i have find one solution...