loaded app at startup cannot create file

Everything else that doesn't fall into one of the other PB categories.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

loaded app at startup cannot create file

Post by doctorized »

I make a simple app that is loaded at windows startup via registry value and works saving some data in a log file.
When I double-click the app it runs fine. When the app is loaded from the windows at startup fails to create a file
and write in it. I understand that my app has not the right to do it but what can I do?
Little John
Addict
Addict
Posts: 4869
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: loaded app at startup cannot create file

Post by Little John »

Choose a location for the file, where your program has the right to write. Note that the proper location might depend on the user account, on which the program is started. For getting a proper path, you can use e.g. the environment variable %appdata%, or a Win API function such as SHGetSpecialFolderLocation_().
The easiest way is to choose a path, where the program always will be allowed to write, e.g. the temp directory:

Code: Select all

; Example (PB 5.11)

filePath$ = GetTemporaryDirectory() + "log.txt"  ; chose a path here, where your program has the right to write
ofn = OpenFile(#PB_Any, filePath$, #PB_File_Append)
WriteStringN(ofn, "Test")
CloseFile(ofn)
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

Little John wrote:Note that the proper location might depend on the user account, on which the program is started.
The user is administrator, the user that was created at windows installation. I tried many different folders with no success, like the folder where the app is in and a folder in %appdata%/local. No luck.
User avatar
leonhardt
Enthusiast
Enthusiast
Posts: 220
Joined: Wed Dec 23, 2009 3:26 pm

Re: loaded app at startup cannot create file

Post by leonhardt »

maybe you should use GetLastError_() to get the failure info first?
poor English...

PureBasic & Delphi & VBA
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

leonhardt wrote:maybe you should use GetLastError_() to get the failure info first?
Error code: 183 = "Cannot create a file when that file already exists."
And guess what.... the file does not exist!!

Code: Select all

i.l = CreateFile(#PB_Any, GetCurrentDirectory() + "log.txt")
WriteString(i,"test text")
CloseFile(i)
Buffer.s = Space(4096)
dw.l = GetLastError_()
Chars.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, Buffer, Len(Buffer), 0)
MessageRequester(Str(dw), Left(Buffer, Chars-2))
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: loaded app at startup cannot create file

Post by Demivec »

What is the current directory set to?
Little John
Addict
Addict
Posts: 4869
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: loaded app at startup cannot create file

Post by Little John »

Demivec wrote:What is the current directory set to?
In addition to this question:

doctorized, what happens when in your above code you replace GetCurrentDirectory() with GetTemporaryDirectory()?
And BTW, which Windows version do you use?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: loaded app at startup cannot create file

Post by ts-soft »

Your autostart will not set currentdirectory, better use:

Code: Select all

GetPathPart(ProgramFilename())
The explorer set the currentdirectory, but the shell doesn't!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

ts-soft wrote:Your autostart will not set currentdirectory, better use:

Code: Select all

GetPathPart(ProgramFilename())
The explorer set the currentdirectory, but the shell doesn't!
That's the solution to my problem! My directory is C:\Users\Doctorized\AppData\Local\MyApp.
I changed

Code: Select all

MessageRequester(Str(dw), Left(Buffer, Chars-2))
to

Code: Select all

MessageRequester(GetCurrentDirectory(), Str(dw) + "  :  " + Left(Buffer, Chars-2))
and the directory was c:\windows\system32. Changing GetCurrentDirectory() to GetTemporaryDirectory()
error code was 0 (successful). By changing it to GetPathPart(ProgramFilename()) the path was correct and the file
was created. Thanx a lot for the help!!
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

According to PB help for CreateFile(): If the file already exists, it will be overwritten by the new empty file.
but if the file exists, error 183 pops up. Any suggestions?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: loaded app at startup cannot create file

Post by luis »

doctorized wrote: By changing it to GetPathPart(ProgramFilename()) the path was correct and the file was created.
Before you said:
tried many different folders with no success, like the folder where the app is in
How do you explain that ?
"Have you tried turning it off and on again ?"
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

luis wrote:
doctorized wrote: By changing it to GetPathPart(ProgramFilename()) the path was correct and the file was created.
Before you said:
tried many different folders with no success, like the folder where the app is in
How do you explain that ?
Very easy!! I was having the app in a different folder every time and GetCurrentDirectory() was used. :wink:
I was thinking that GetCurrentDirectory() was giving the correct path in every case. I must confess that in some
cases, I didn't use GetCurrentDirectory() but a constant string and then I had the problem too. Weird, I know, but true!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: loaded app at startup cannot create file

Post by ts-soft »

You should only use GetCurrentDirectory(), if you have set it by yourself.
The user of your app can start it in many ways, and only some ways set
the currentdirectory to your applicationfolder :!:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: loaded app at startup cannot create file

Post by doctorized »

ts-soft wrote:You should only use GetCurrentDirectory(), if you have set it by yourself.
The user of your app can start it in many ways, and only some ways set
the currentdirectory to your applicationfolder :!:
You are right my friend but I didn't know that. Now I know....
Post Reply