1. Tracy Lamb
  2. PowerBuilder
  3. Wednesday, 15 June 2022 21:28 PM UTC

I don't use the Windows Registry to install my software... customers just copy the code I send them to the directory I suggest.  They may use a different directory, their choice.

In the code, I call GetCurrentDirectory() to find out where the application was launched from, and create a couple temporary directories from there.  One customer reported that the temporary directories are being created on their Google Drive.  This is new behavior for them... the software used to create the directories under MyApp/Temp1 and MyApp/Temp2.  

What would cause GetCurrentDirectory() to report something other than the home directory of the app?  PB2019 app.

~~~Tracy

 

 
Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 13:12 PM UTC
  2. PowerBuilder
  3. # Permalink

Get the current directory in the application open event and store it in a global variable.

The current directory can be changed by GetFileOpenName and GetFileSaveName.

Comment
There are no comments made yet.
Tracy Lamb Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 14:00 PM UTC
  2. PowerBuilder
  3. # 1

Thank you all for your brilliant advice!  Yes, a new feature I added includes the GetFileOpenName... so obviously that was resetting the current directory.  I decided to use Miguel's solution.  Thank you for including the code for n_applicationPath Miguel! 

I also want to shout out to Benjamin for his insight... it's definitely something to consider if a customer ever decides to host the application from a network share. Right now, I have several other applications and documents in the app directory.  One is the app.ini file, which can be updated when the app is running... so a network share will present a few different problems that might be solved by using the AppData folder.

Again, many thank to all!

~~~Tracy

 

Comment
  1. Miguel Leeuwe
  2. Sunday, 19 June 2022 12:41 PM UTC
Hi,

So another solution would be to use GetCurrentDirectory() BEFORE doing the GetFileOpenName. You should read the app's directory as soon as possible. For example in the application's Open event. (this command DOES work in that event, unlike for some reason the other solution that I gave). That value you then store in a global variable, (or if using PFC's for example in gnv_app.is_appDir or something similar). Once you have the global variable set correctly, you don't have to worry if the 'current directory' has been changed by any GetFileOpen/SaveName or FileWrite(ex), etc. You then just use the value in the global variable.

Regards.
  1. Helpful
  1. Tracy Lamb
  2. Sunday, 19 June 2022 15:09 PM UTC
I implemented this solution after I couldn't troubleshoot the first solution. I get the current directory in the open event of the frame and save it to a global string. This solution works just as well, and doesn't crash at runtime.

Thanks,

~~~Tracy
  1. Helpful 1
  1. Miguel Leeuwe
  2. Sunday, 19 June 2022 17:31 PM UTC
Great! as long as it works!
  1. Helpful
There are no comments made yet.
Brad Mettee Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 13:42 PM UTC
  2. PowerBuilder
  3. # 2

If the shortcut on the desktop (or wherever shortcut exists) has the "Start In" folder different from the application, it may be causing this issue.

Suggestions already given should help you find out where your application was actually run from. Keep in mind, if they installed it someplace under "program files" then, you can't write to any sub folder unless the app is run as administrator.

Comment
  1. Mark Goldsmith
  2. Thursday, 16 June 2022 14:04 PM UTC
You are correct Brad and I think Miguel mentioned this as well. This is only the case if using GetCurrentDirectory(). If using the GetModuleFilenameW() approach the Start In setting has no impact. But per my follow-up comment, for me I've had occasional issues using GetModuleFilenameW() so I tend to stay away from it.
  1. Helpful 1
There are no comments made yet.
Benjamin Gaesslein Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 08:24 AM UTC
  2. PowerBuilder
  3. # 3

Hi Tracy,

although your initial question has been answered ( the "current directory" that GetCurrentDirectory gets can change for various reasons) I would suggest you "future-proof" your app by using the %APPDATA% folder. You can get the path like this:

constant string KEYWORD = "Keyword"
constant string APPDATA = "Appdata"
ContextKeyword  lcxk_base
string ls_values[]
string ls_appdata

this.GetContextService(KEYWORD, lcxk_base)
lcxk_base.GetContextKeywords(APPDATA, ls_values)
if upperbound(ls_values) >= 1 then
	ls_appdata= ls_values[1]
else
	//you might resort to some fallback solution here or
	//throw an exception because something obviously went wrong
end if

The AppData-Folder is located within the personal folder of the current Windows user. This means every user has write access to it, which *might* not be the case for the location of the application. This also makes it possible to use the app on a terminal server or network share.

Comment
  1. Chris Pollach @Appeon
  2. Thursday, 16 June 2022 11:58 AM UTC
That is an excellent suggestion Ben!
  1. Helpful
  1. mike S
  2. Saturday, 18 June 2022 15:25 PM UTC
another option is to put it under programdata - if it is pretty much the same for any user. or the temp folder if it really is tempory for things like pdf files being generated/sent



GetContextKeywords for these:



PROGRAMDATA - programdata directory

TEMP and TMP - default temporary directories for applications that are available to

PUBLIC - users public folder. - ie C:\Users\Public\

  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 07:09 AM UTC
  2. PowerBuilder
  3. # 4

Hi,

This is what we use and it works great, without using GetCurrentDirectory().

See the attached exported code. Unzip the file and import it into a PBL and use this code to run it:

n_applicationPath lnv // autoinstantiated, no CREATE needed.
string ls_appPath

ls_appPath = lnv.of_getApplicationPath()

Hope it solves your problem.

regards,

MiguelL

 

BTW: Appeon, it seems crazy that we're still not allowed to attach an *.SRU file without having to zip or rename it.

Attachments (1)
Comment
  1. Miguel Leeuwe
  2. Thursday, 16 June 2022 14:03 PM UTC
Before GetCurrentDirectory, the windows API equivalent to it has been around for ages btw.
  1. Helpful
  1. Tracy Lamb
  2. Saturday, 18 June 2022 13:21 PM UTC
Miguel,

I'm having a little trouble with the runtime code when I compiled your solution into my project. Works fine in Development mode, but after I build and deploy the application, it crashes right after I open the window with this new code in it... no error message or anything, just closes the whole app.

n_applicationPath lnv_appPath // autoinstantiated, no CREATE needed.

is_AppPath = lnv_appPath.of_getApplicationPath()

MessageBox("PDF Merge", is_AppPath)



I put the MessageBox in there to see if I could trap the error at runtime and to check the directory it was returning. But it crashes before it even gets to the MessageBox. All other windows in the app still open and function correctly.

Any ideas?

~~~Tracy

  1. Helpful
  1. Miguel Leeuwe
  2. Saturday, 18 June 2022 19:47 PM UTC
Hi Tracy,

It's very difficult to say anything without knowing what you do in all the other code of your application.

I'd suggest for you to create a small sample application. Then open some window and put the code in a button or open of that window.

What I did learn, is that the code doesn't work when you call it from the open event of the Application object.

regards.
  1. Helpful
There are no comments made yet.
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 05:54 AM UTC
  2. PowerBuilder
  3. # 5

As Mark mentioned, get the currentdirectory at the open event of the application object and store that in a global variable.  Use that variable instead of GetCurrentDirectory function elsewhere.

 

Comment
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 04:12 AM UTC
  2. PowerBuilder
  3. # 6

Hi Tracey,

Could you get the full pathname to the running application and extract the path from that?  That seems like it might be more reliable than using the GetCurrentDirectory function. I've not tried it, but here is a link to a tutorial that will return the full pathname to the executable: https://community.appeon.com/index.php/articles-blogs/tutorials-articles/2-powerbuilder/155-how-to-get-the-application-path.  Note, it uses a loop to find the last / position, but you could use the LastPos function instead.  It looks like the important bit is to use the GetModuleFileNameW function.

Comment
  1. Miguel Leeuwe
  2. Thursday, 16 June 2022 07:02 AM UTC
Yes, that's something similar to what we do but slightly different.

I've added some exported code of what works for us in a different reply.

regards
  1. Helpful
  1. Mark Goldsmith
  2. Thursday, 16 June 2022 12:47 PM UTC
Miguel and Daryl...I too have used that approach as well however I seem to have occasional application crashes or strange behaviour, command buttons not responding, when used further into the application (IE outside of the open event of the application object). I've never pursued it further to find out why, and just chocked it up to MicroSoft, but if you haven't experienced this then maybe I'll go back to troubleshooting it.
  1. Helpful 1
There are no comments made yet.
Mark Goldsmith Accepted Answer Pending Moderation
  1. Thursday, 16 June 2022 03:02 AM UTC
  2. PowerBuilder
  3. # 7

Hi Tracy,

If your application uses GetFileOpenName() or GetFileSaveName() somewhere throughout the application and users navigate away from the directory your application resides in then this will change the current directory.

Per the Help..."The dialog boxes presented by GetFileOpenName and GetFileSaveName are system dialog boxes. They provide standard system behavior, including control over the current directory. When users change the drive, directory, or folder in the dialog box, they change the current directory or folder. The newly selected directory or folder becomes the default for file operations until they exit the application, unless the optional initdir argument is passed."

A couple of ways around it (if not using the initdir argument) are:

1) In the open event of the application object, call GetCurrentDirectory() and store it in either a global variable or an instance variable of the application object.

2) When using either GetFileOpenName() or GetFileSaveName(), include Index 7 or OFN_NOCHANGEDIR in the aFlag argument to restore the current directory (although sometimes I have had mixed results with this).

HTH...regards,

Mark

Comment
  1. Miguel Leeuwe
  2. Thursday, 16 June 2022 07:03 AM UTC
Nail on the head probably!
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 15 June 2022 23:18 PM UTC
  2. PowerBuilder
  3. # 8

Do they use a shortcut in which the "Start in" property is set to somewhere on a Onedrive?

regards.

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Wednesday, 15 June 2022 22:03 PM UTC
  2. PowerBuilder
  3. # 9

Hi, Tracy - 

Did this user install your app on their Google Drive? If not, where (drive & folder(s)) did they install it?

Comment
  1. Tracy Lamb
  2. Wednesday, 15 June 2022 22:46 PM UTC
Customer says the app is on their local machine... C:\BenchTop10 directory. My app gets the CurrentDirectory() which should be C:\BenchTop10.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.