Page 1 of 1

[Done] CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 3:32 am
by Dude
Both of these incorrectly report non-zero (success), but no such file is actually created or opened, because I don't have admin rights on my Window 7 PC. So, these commands should both really return 0 for failure instead, due to no file writing.

The docs even say "This function fails if the file cannot be opened with write permission" which is certainly the case here.

My app depends on users being able to save data wherever they want, so I can't say "Saved successfully!" if it didn't.

Code: Select all

f=CreateFile(#PB_Any,"c:\test.txt")
If f
  WriteStringN(f,"Add")
  CloseFile(f)
  Debug "Success!" ; Actually failure.
EndIf

Code: Select all

f=OpenFile(#PB_Any,"c:\test.txt",#PB_File_Append)
If f
  WriteStringN(f,"Add")
  CloseFile(f)
  Debug "Success!" ; Actually failure.
EndIf

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 4:03 am
by Dude
On my C: drive, this example is even worse. What is going on?

Code: Select all

file$="c:\test.txt"

orig.q=FileSize(file$)

f=OpenFile(#PB_Any,file$,#PB_File_Append)

If f
  WriteStringN(f,"add 12 chars")
  CloseFile(f)
EndIf

Debug orig ; 29
Debug FileSize(file$) ; 43

; 29 and 43? No such c:\test.txt file even exists!

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 4:49 am
by TI-994A
Dude wrote:Both of these incorrectly report non-zero (success), but no such file is actually created or opened, because I don't have admin rights on my Window 7 PC. So, these commands should both really return 0 for failure instead, due to no file writing...
Since you're using the #PB_Any directive, you're not getting the result codes, but the auto-generated file number instead.

This should work:

Code: Select all

f = CreateFile(0, "c:\test2.txt")
If f
  If WriteStringN(0, "Add")
    Debug "Success!"
  EndIf
  CloseFile(0)
EndIf
As for the FileSize() function, it's returning file not found (-1) correctly for me, with PureBasic v5.41 LTSb on Windows 10. Perhaps a Windows 7 issue.

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 4:55 am
by Dude
TI-994A wrote:Since you're using the #PB_Any directive, you're not getting the result codes, but the auto-generated file number instead.
Okay, but the manual says "...returns the auto-generated file number instead on success". However, there isn't any success.
TI-994A wrote:This should work
Tried your example without #PB_Any, but despite the debug output saying "success", no file is created. :(

Here's my C: drive as proof, with all files and folders shown: https://i.imgur.com/XOF9WxG.png

It's probably some junction or hard-link rubbish in play? But I haven't set anything up like that.

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 6:02 am
by TI-994A
Dude wrote:Okay, but the manual says "...returns the auto-generated file number instead on success". However, there isn't any success.
It appears that you're right; zero should be returned on failure regardless of whether #PB_Any is used.

But funny thing is, it works correctly for me; the file functions return zero with or without #PB_Any if access was denied. It might be some permissions anomaly with your OS. Perhaps you could test it on another machine to confirm.

* tested with PureBasic 5.40 LTS on Windows 8.1, and PureBasic 5.41 LTSb on Windows 10, (all x64)

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 9:14 am
by Fred
Are you sure it's not the Windows filesystem virtualisation feature ?

http://www.thewindowsclub.com/file-regi ... -windows-7

I don't think there is such a bug in PB.

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 10:11 am
by Dude
Fred wrote:Are you sure it's not the Windows filesystem virtualisation feature ?
I doubt it, because if I use my examples but then try to load such a file into Notepad, it says the file doesn't exist. If it were virtualised, it should be seen and opened by Notepad.

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 1:00 pm
by blueb
I'm not so sure Dude...

I can open the file (and read the contents) with Notepad, located in: C:\Users\Bob\AppData\Local\VirtualStore

My file finder program locates: c:\test.txt but I can't open it with Notepad.

And I can't locate: c:\test.txt with Windows Explorer.

Win 10 x86... I have full rights and able to open hidden files, etc.

Re: CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 2:15 pm
by Dude
blueb wrote:C:\Users\<UserName>\AppData\Local\VirtualStore
Aha! :D I opened that folder on my PC and that's where the file actually is! Now we're getting somewhere.

When I run my examples with admin rights, though, they get stored to the root of C: as expected, and not to the VirtualStore folder.

So... we now have the situation that the file being created is NOT where the user will expect. If the user specifies C: and opens it with Windows Explorer, he will complain that my app has a bug when he doesn't see the file (just as I reported this as a bug).

Looks like I'll have to put a note about this in my docs, to explain the situation to users who don't run with admin rights.

Thanks for solving this for me, BlueB! 8)

Re: [Done] CreateFile/OpenFile report success on failure

Posted: Sat Nov 28, 2015 2:40 pm
by User_Russian
The code works correctly with enabled option Request User mode for Windows Vista and above (no virtualization).

Image

Re: [Done] CreateFile/OpenFile report success on failure

Posted: Sun Nov 29, 2015 1:26 am
by Dude
User_Russian wrote:The code works correctly with enabled option Request User mode for Windows Vista and above (no virtualization).
Not here. When I try that, no file is created in either C: or the VirtualStore folder at all. And PureBasic correctly returns 0 for failure. ;)

Re: [Done] CreateFile/OpenFile report success on failure

Posted: Sun Nov 29, 2015 1:46 am
by nco2k
and that is the correct behavior. if you want access to c:\ you have to request admin rights. you should really use one of those two modes and avoid virtualisation.

c ya,
nco2k

Re: [Done] CreateFile/OpenFile report success on failure

Posted: Sun Nov 29, 2015 1:53 am
by Dude
I know, nco2k. :) My users may be on limited accounts though, so I need to mention this behavior in my docs. My apps will never force admin rights because it increases support requests from users and it's too hard explaining what admin/limited rights are. It's a lot easier just to say that Windows blocks writing to C: and tell them where Windows has stored it instead. ;)