Ive tried everything i know. i give up!

a simple createfile results in ERROR_NOACCESS..dont know what is going on.

simple instruction in my WndProc
	invoke CreateFile,addr fSaved2,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL


fSaved2 is defined in data section
fSaved2			db 'MyFile.exe',0


all your ideas will be welcomed.
Ray.
Posted on 2006-04-21 03:57:10 by Ray
Maybe the filename string gets corrupted by some other code? Try using messagebox or something like that to see if the string is valid before it gets passed to createfile.
Posted on 2006-04-21 04:51:59 by arafel
hey arafel,
I traced the code with a debugger. Filename seems to be ok.
I tried changing the creation flags, share access, params, and a lot more, but something is wrong here & i cant see it.

Ray
Posted on 2006-04-21 06:10:24 by Ray
Hi arafel.
This seems to be a bug in windows.
If you still doubt about it, try creating this "myfile.exe" file from scratch by creating an empty .txt file and renaming it to "myfile.exe".
You'll soon see that you wont be able to access nor to remove that file anymore...
Posted on 2006-04-21 07:08:06 by Axial
I do not have this problem, Win2K SP4, file creates, opens and deletes normally.
Posted on 2006-04-21 07:29:33 by donkey
guys,
after some debugging this is what i have found in my code:


int 3
invoke CreateFile,addr fSaved2,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
invoke CloseHandle,eax

mov al, byte ptr ; Number of sections
mov SECTIONS, al ; Save it
invoke wsprintf,addr buffer, addr msg3, SECTIONS
invoke println,hWin, IDC_EDIT,addr buffer

int 3
invoke CreateFile,addr fSaved2,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
invoke CloseHandle,eax



The first call creates the file succesfully.
The second call fails with an ERROR_NOACCESS.
what those 4 lines of code do and mess up the next call is beyond my knowledge indeed.

I hope someone can shine a some light to how i can fix this.

Posted on 2006-04-21 07:31:19 by Ray

I do not have this problem, Win2K SP4, file creates, opens and deletes normally.


Strange... this happens on both XP SP2 systems I own, but it indeed doesn't happen on  other systems I tested.
Maybe not a windows issue finally. :)
Posted on 2006-04-21 07:56:11 by Axial
Why are you closing the file before you are done constructing it?

If you have an invalid .exe file created in a folder that's open in explorer, explorer's background icon-extracting-and-other-wizardry-stuff thread will sometimes hang, causing the exe to be locked. Find the "wholockme" tool to see which process has your files and folders locked.
Posted on 2006-04-21 08:30:50 by f0dder
f0dder,
all those createfiles obviously are not needed. its just me debugging session. I placed all over the code calls to CreateFile just to find where it fails.

the problem remains: The call before those 4 lines of code completes successfuly.
the call after those 4 lines fails with an ERROR_NOACCESS.

I have no idea how to fix this.
Posted on 2006-04-21 09:30:21 by Ray
can you delete the .exe file manually?
Posted on 2006-04-21 09:33:44 by f0dder
actually no!
but thats another story, right??
Posted on 2006-04-21 09:34:59 by Ray
Two sides of the same story. Read my earlier post about explorer.exe locking on invalid EXE files.
Posted on 2006-04-21 09:36:45 by f0dder
ok, but what does this have to do with error_noaccess ??
is it related?
Posted on 2006-04-21 09:39:38 by Ray

ok, but what does this have to do with error_noaccess ??
is it related?


You write part of the exe file and CloseHandle. This exe file is only partially constructed, so it's invalid.

Before you re-open the file, explorer.exe or indexing service or whatever decides to take a look at your exe file, possible to extract icons. The exe file is invalid, the reading application gets confused and stuck in some endless loop - keeping the file open. Presto, because of sharing and locking, you now cannot access the file.

Same reason you should NEVER download executables directly to your desktop.
Posted on 2006-04-21 09:44:33 by f0dder
alright:!
lets do this:
forget the first call,
so the code becomes:
mov al, byte ptr ; Number of sections
mov SECTIONS, al; Save it
invoke wsprintf,addr buffer, addr msg3, SECTIONS
invoke println,hWin, IDC_EDIT,addr buffer

int 3  ;<<--trap debugger to trace
invoke CreateFile,addr fSaved2,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
;;;;invoke CloseHandle,eax  dont close the file ....


i get,again, ERROR_NOACCESS.

p.s.the file does not exists!
Posted on 2006-04-21 09:50:11 by Ray
Is this the first time you access the file in your code? Can you attach a .asm file that has this problem isolated?
Posted on 2006-04-21 09:57:12 by f0dder
thanks for the help f0d.
check your pms

EDIT: f0dder has found the problem. Its a stack align problem.

invoke CreateFile,addr fSaved2,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
invoke CloseHandle,eax

mov al, byte ptr ; Number of sections
mov SECTIONS, al; Save it
invoke wsprintf,addr buffer, addr msg3, SECTIONS <<--------- HERE
invoke println,hWin, IDC_EDIT,addr buffer



i have defined SECTIONS as a byte, according to f0dder masm converts it to word instead of a double!

If anybody has a nice tutorial on alignment please post here! I would like to learn these stuff. i often fall into these traps!

again, thanks f0d.
Posted on 2006-04-21 10:08:35 by Ray