writefile() fail with hidden or system files

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
binocular222
Posts: 1424
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

writefile() fail with hidden or system files

Post by binocular222 »

Error: Access denied
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: writefile() fail with hidden or system files

Post by Filehero »

binocular222 wrote:Error: Access denied
What happens if you run XY as Admin?

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: writefile() fail with hidden or system files

Post by bdeshi »

actualy it just fails with readonly files. Maybe the target file had the readonly attribute set?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

binocular222
Posts: 1424
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: writefile() fail with hidden or system files

Post by binocular222 »

Tried with XY as Admin; file has no "Read-only" attribute. Still access denied
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: writefile() fail with hidden or system files

Post 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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

binocular222
Posts: 1424
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: writefile() fail with hidden or system files

Post 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)
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Filehero
Posts: 2734
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: writefile() fail with hidden or system files

Post 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

binocular222
Posts: 1424
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: writefile() fail with hidden or system files

Post by binocular222 »

Notepad / Sublime Text has no problem writing to that file
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: writefile() fail with hidden or system files

Post 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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: writefile() fail with hidden or system files

Post 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.

Post Reply