[Done] CreateFile/OpenFile report success on failure

Windows specific forum
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

[Done] CreateFile/OpenFile report success on failure

Post 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
Last edited by Dude on Sat Nov 28, 2015 2:17 pm, edited 1 time in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateFile/OpenFile report success on failure

Post 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!
User avatar
TI-994A
Addict
Addict
Posts: 2754
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: CreateFile/OpenFile report success on failure

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateFile/OpenFile report success on failure

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2754
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: CreateFile/OpenFile report success on failure

Post 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)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Fred
Administrator
Administrator
Posts: 18361
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: CreateFile/OpenFile report success on failure

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateFile/OpenFile report success on failure

Post 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.
User avatar
blueb
Addict
Addict
Posts: 1121
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: CreateFile/OpenFile report success on failure

Post 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.
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateFile/OpenFile report success on failure

Post 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)
User_Russian
Addict
Addict
Posts: 1597
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post by User_Russian »

The code works correctly with enabled option Request User mode for Windows Vista and above (no virtualization).

Image
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

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

Post 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. ;)
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

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

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

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

Post 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. ;)
Post Reply