Recent

Author Topic: [SOLVED] Directory operations  (Read 13456 times)

WickedDum

  • Full Member
  • ***
  • Posts: 211
[SOLVED] Directory operations
« on: October 20, 2016, 02:34:25 am »
Afternoon!

This might be too simple for a Beginners forum... :-[

I started trying to create a file.  It was unsuccessful so I backed up.  (Deep breath...exhale...)  First things first.  Just create the directory to put it in, I thought.  Hmph...  No luck there, either... :(

Here's what I started with:

Code: Pascal  [Select][+][-]
  1. Procedure CreateFile;
  2.  
  3. const
  4.   FileName = 'NewFile.txt';
  5. var
  6.   NewDir : string;
  7.  
  8. begin
  9.   NewDir := 'C:\Program Files (x86)\First\Second\';
  10.   if GetCurrentDir <> NewDir then
  11.     begin
  12.       ShowMessage('GetCurrentDir <> NewDir');
  13.       if not SetCurrentDir(NewDir) then
  14.         begin
  15.           ShowMessage('Directory "'+ NewDir + '" does not exist.');
  16. //          ChDir('C:\Program Files (x86)\');
  17.           {$I-}
  18.           mkdir(NewDir);
  19.           {$I+}
  20.           if IOResult<>0 then
  21.             ShowMessage('Cannot create directory : '+NewDir+'.')
  22.           else
  23.             ShowMessage('Directory created.');
  24.         end;
  25.     ...
  26.  

I couldn't even change directories...so I commented out the 'ChDir'.

I could not get it to work with or without the 'separators', trying first directory, trying both directories, changing strings, etc.

In my continued readings, I ran across http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/createdir.html.  That example:

Code: Pascal  [Select][+][-]
  1.       If Not DirectoryExists(NewDir) then
  2.         If Not CreateDir(NewDir) then
  3.           ShowMessage('Failed to create directory!')
  4.         else
  5.           ShowMessage('Created "NewDir" directory')

I couldn't get that to work either... :'(

I attached the whole unit for those interested.

Care to offer a suggestion?

Thanks!

« Last Edit: October 21, 2016, 11:30:50 pm by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Directory operations
« Reply #1 on: October 20, 2016, 03:05:26 am »
To write in ProgramFiles you need Administrative rights.

Go to project options > Execution Level > requireAdministrator.

To execute it inside lazarus you need to run Lazarus as administrator first. Right click on the lazarus icon > Run as administrator.

To create the directories from code:
Code: Pascal  [Select][+][-]
  1.   CreateDir('C:\Program Files (x86)\First\');
  2.   CreateDir('C:\Program Files (x86)\First\Second\');  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Directory operations
« Reply #2 on: October 20, 2016, 05:14:04 am »
To create the directories from code:
Code: Pascal  [Select][+][-]
  1.   CreateDir('C:\Program Files (x86)\First\');
  2.   CreateDir('C:\Program Files (x86)\First\Second\');  

Note that "Program Files" and "Program Files (x86)" are not always located off of the "C:\" drive.  It depends on where the user has actually installed Windows.  You should use SHGetFolderPath() or SHGetKnownFolderPath() to locate the correct path dynamically at runtime.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Directory operations
« Reply #3 on: October 20, 2016, 05:47:33 am »
Thank you!!

@lainz - I find it very interesting that Lazarus must be executed with administrative rights.  I don't remember TP3 or 4 ever needing the rights. :D    Is there a way that I can configure/build Lazarus to always execute with administrator rights?  It would be a pain to try to remember to execute Laz every time by clicking on 'Run as adminstrator'. %)

I could not find the "project options > Execution Level > requireAdministrator" option.  I checked ALL of the 'options' that I could find. Could it be under a different name?
 
I figured I might have to create the parent directory (First) before the child (Second).  Thanks for confirming that.

@Remy Lebeau - That's good to know!!  It saves me a lot of time not having to go in search of the hidden OS files. :)     Is there a Delphi or FPC conversion of those SH functions?

Thanks, again, gentlemen!!

Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Directory operations
« Reply #4 on: October 20, 2016, 06:06:11 am »
I find it very interesting that Lazarus must be executed with administrative rights.

Only if the app being debugged needs admin rights to access protected resources.

Is there a way that I can configure/build Lazarus to always execute with administrator rights?

Create a shortcut to Lazarus, then go into the shortcut's properties and set it to Run as Administrator.

Or, rewrite your app to play nice with OS security to begin with, then you won't need to run Lazarus as an admin. Such as saving your data files in folders within user profiles instead of system folders. Only admins should be touching system folders.

I figured I might have to create the parent directory (First) before the child (Second).

Use a recursive function for that, like ForceDirectories(), SHCreateDirectory(), etc.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Directory operations
« Reply #5 on: October 20, 2016, 03:37:02 pm »
Thanks for your help!  Of course, it creates more questions... :)

@Remy Lebeau -
... play nice with OS security to begin with...
???  If you noticed, I was trying to create subdirectories in the 'Program Files (x86)' directory to hold the main program.  What would be the desired method to create those directories?  (Which have nothing to do with the data...)

I'm assuming it takes admin rights to modify the registry, the Start Menu, etc.  Could you please tell me how those are accomplished?

What do I have to do to my program to have it get admin rights to be able to be installed and even run on the customers' machines?

Thank you for your time and assistance!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Directory operations
« Reply #6 on: October 20, 2016, 04:29:00 pm »
What do I have to do to my program to have it get admin rights to be able to be installed and even run on the customers' machines?

Installing and running a program are 2 different things. Only an installer app needs admin rights.
Your program must do with normal user rights (unless it does some low level system specific task).
A simple program can be run also without specific installation, just copy the executable somewhere.

You may also want to consider coding for cross platform. FPC and Lazarus are cross platform systems and MS Windows is not the center of the OS world any more.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Directory operations
« Reply #7 on: October 20, 2016, 05:10:33 pm »
If you need to create first an installer for your program you can use Inno Setup for Windows:
http://wiki.freepascal.org/Inno_Setup

If you need to write a file don't do it in Program Files, use the configuration folder instead:
http://wiki.lazarus.freepascal.org/Multiplatform_Programming_Guide#Configuration_files

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Directory operations
« Reply #8 on: October 20, 2016, 08:08:41 pm »
If you noticed, I was trying to create subdirectories in the 'Program Files (x86)' directory to hold the main program.  What would be the desired method to create those directories?  (Which have nothing to do with the data...)

Create a separate installer that runs with admin rights.

I'm assuming it takes admin rights to modify the registry, the Start Menu, etc.

Only portions of the Registry, yes.  In particular, most areas of HKEY_LOCAL_MACHINE, but not HKEY_CURRENT_USER.

Start Menu, no.  You do not require admin rights to create shortcuts for the user that is running your app, or common shortcuts for all users.  You would need admin rights only if one user needs to create a shortcut for another specific user (which is not common to do).

What do I have to do to my program to have it get admin rights to be able to be installed and even run on the customers' machines?

Use a separate installer if it must be installed in "Program Files".  Your app itself should not run as admin unless it NEEDS admin rights to perform its regular duties.  And even then, it is better to not run the entire app as admin, but instead separate out any admin tasks so they can be run by themselves with elevated rights when needed.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Directory operations
« Reply #9 on: October 20, 2016, 08:38:22 pm »
Thank you!!  That is GREAT information!!!   :)

@JuhaManninen - I misspoke.  I only need admin rights to install the application.  The data, of course, would be in the "AppData" directory.  I intend to eventually build cross-platform applications so I might as well program for it now. ;)  I have seen compiler directives at the onset of a program similar to: 
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$mode objfpc}
  3.   {$H+}
  4. {$ENDIF}

@lainz - Those are great links!  Inno Setup is an extensive script.  Isn't there an application out there that would be simpler/faster, therefore more efficient?

In my readings I came across KNOWNFOLDERID (https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx).  It contains some good information.  It is the successor to CSIDL.

Use a separate installer if it must be installed in "Program Files".
I can do that.  How do I provide admin rights on the customers' machine for the installation program to work?

I saw in the KNOWNFOLDERID page that I don't need admin rights for the Start Menu as well as other directories.

Thank you all again for your continued FANTASTIC assistance!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Directory operations
« Reply #10 on: October 20, 2016, 08:41:06 pm »
Well InnoSetup is really efficient and the best tool I know to create setup files that really works.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Directory operations
« Reply #11 on: October 20, 2016, 08:43:39 pm »
Well InnoSetup is really efficient and the best tool I know to create setup files that really works.

Agree.

If you use this tool to create the app in the first place, you automatically get an installer script that can be compiled with InnoSetup. You can add to the script, or just study what's in there.

https://macpgmr.github.io/MacXPlatform/LazXProj.html

You also get installer scripts for Mac and Linux.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Directory operations
« Reply #12 on: October 20, 2016, 09:57:00 pm »
I have seen compiler directives at the onset of a program similar to: 
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$mode objfpc}
  3.   {$H+}
  4. {$ENDIF}
That is not needed for cross-platform. Even for Delphi compatible code it does not make much sense. Then you should use:
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$mode delphi}
  3. {$ENDIF}

Just don't use Windows unit in your uses sections nor hard-coded Windows specific paths like "C:\program files\" and your code will be cross-platform.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Directory operations
« Reply #13 on: October 21, 2016, 12:27:42 am »
OK.  I downloaded INNO SETUP.  Something else to help my head hurt...   %)

@Phil - Are you the Phil at the bottom of the page (https://macpgmr.github.io/MacXPlatform/LazXProj.html)?

@JuhaManninen - Sorry about the {$IFDEF} code.  I was referring to something more like
Code: Pascal  [Select][+][-]
  1.  uses
  2.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  3.  {$IFDEF WIN32}
  4.    Windows;
  5.  {$ENDIF}
  6.  {$IFDEF Unix}
  7.    ports;
  8.  {$ENDIF}
but I haven't found it.  Yet.  I saw something once where the {$IFDEF} commands were at the top of the file...

Just don't use Windows unit in your uses sections nor hard-coded Windows specific paths like "C:\program files\" and your code will be cross-platform.
I believe I read somewhere that I need to incorporate 'generic' procedure/function calls.  But, of course, I cannot find the list now.  I believe I'll need to incorporate those terms for cross compatibility.

Thanks for your time!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Directory operations
« Reply #14 on: October 21, 2016, 02:02:28 am »
Use a separate installer if it must be installed in "Program Files".
I can do that.  How do I provide admin rights on the customers' machine for the installation program to work?

Any modern installer creation tool that is worth using should already be creating installer executables that are manifested to require admin rights.  But even if they are not, Windows UAC also includes an "Installer Detection" feature that will automatically run an un-manifested app with elevated rights if it thinks the app is an installer, based on various heuristics it performs on the app when it is loaded into memory. 

When an app is run with elevated rights, whether by manifest or heuristics, Windows will prompt the user for admin credentials before allowing the app to actually run.

This information is documented on MSDN.

I saw in the KNOWNFOLDERID page that I don't need admin rights for the Start Menu as well as other directories.

Correct, as I already told you earlier.  Start Menu folders are publically accessible.  And you don't need admin rights to *query* folder paths, only when *accessing* them if they are protected folders.
« Last Edit: October 21, 2016, 02:04:32 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018