Page 1 of 1

writefile() fail with hidden or system files

Posted: 23 Jun 2014 06:53
by binocular222
Error: Access denied

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 07:10
by Filehero
binocular222 wrote:Error: Access denied
What happens if you run XY as Admin?

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 07:34
by bdeshi
actualy it just fails with readonly files. Maybe the target file had the readonly attribute set?

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 07:38
by binocular222
Tried with XY as Admin; file has no "Read-only" attribute. Still access denied

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 07:42
by bdeshi
then the file maybe locked by the system (or other process). I can do writefile() on any file, except for the ones with readonly.

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 08:58
by binocular222
Strange, I tried this:
- Create new text file
- Set as hidden via Info Panel
- Close Info Panel
- writefile() still fail
Surely there's no thing access that file (used Lock Hunter)

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 08:59
by Filehero
binocular222 wrote:Tried with XY as Admin; file has no "Read-only" attribute. Still access denied
You get the same message when trying to edit with e.g. Notepad/++?


Cheers,
Filehero

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 09:11
by binocular222
Notepad / Sublime Text has no problem writing to that file

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 10:05
by bdeshi
binocular222 wrote:Strange, I tried this:
- Create new text file
- Set as hidden via Info Panel
- Close Info Panel
- writefile() still fail
Surely there's no thing access that file (used Lock Hunter)
okay, even more strange, writefile() fails, but appending data ( writefile("<curitem>", "data", a) ) works fine, even in quoted event.

Re: writefile() fail with hidden or system files

Posted: 23 Jun 2014 10:45
by admin
Wow, very interesting! I did not know that. There is apparently some hard-coded safety function in Windows that makes overwriting HIDDEN and SYSTEM functions fails unless the programmer explicitly allows it. Fix comes.

See http://msdn.microsoft.com/en-us/library ... 85%29.aspx:
If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile fails and sets the last error to ERROR_ACCESS_DENIED if the file exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM attribute. To avoid the error, specify the same attributes as the existing file.