Page 1 of 1
loaded app at startup cannot create file
Posted: Thu Apr 11, 2013 3:03 pm
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?
Re: loaded app at startup cannot create file
Posted: Thu Apr 11, 2013 3:38 pm
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)
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 11:17 am
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.
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 1:10 pm
by leonhardt
maybe you should use GetLastError_() to get the failure info first?
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 3:23 pm
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))
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 3:26 pm
by Demivec
What is the current directory set to?
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 3:48 pm
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?
Re: loaded app at startup cannot create file
Posted: Fri Apr 12, 2013 4:00 pm
by ts-soft
Your autostart will not set currentdirectory, better use:
The explorer set the currentdirectory, but the shell doesn't!
Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 9:54 am
by doctorized
ts-soft wrote:Your autostart will not set currentdirectory, better use:
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!!
Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 1:54 pm
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?
Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 5:13 pm
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 ?
Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 7:54 pm
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.

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!
Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 8:10 pm
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

Re: loaded app at startup cannot create file
Posted: Sat Apr 13, 2013 8:29 pm
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....