Page 1 of 1

How to check user permissions

Posted: Thu May 17, 2018 10:52 pm
by hmgchang
Dear Frens,
Marhaban ya Ramadhan

How to code for checking user permission for a folder in order to create/write a file/s....
Is there anyway to change/get the write permission on a folder.
It's on win10 os
TIA

Best rgds
Chang

Re: How to check user permussions

Posted: Fri May 18, 2018 7:54 pm
by KDJ
Chang

Without coding in C, you can check if you have permission to write to the directory by trying to create a temporary file in this directory.
Example:

Code: Select all

#include "fileio.ch"
#include "hmg.ch"

FUNCTION Main()
  LOCAL cDir := "C:\Program Files"

  IF HB_DirExists(cDir)
    MsgBox(cDir + CRLF + CRLF + If(IsDirWritable(cDir), "You can write in this directory.", "You have not permissions to write in this directory!"))
  ELSE
    MsgBox(cDir + CRLF + CRLF + "Directory not found!")
  ENDIF

RETURN NIL

FUNCTION IsDirWritable(cDir)
  LOCAL cFileName
  LOCAL nFileHandle

  nFileHandle := HB_fTempCreate(cDir, NIL, FC_NORMAL, @cFileName)

  IF nFileHandle != F_ERROR
    fClose(nFileHandle)
    fErase(cFileName)
    RETURN .T.
  ENDIF

RETURN .F.

Re: How to check user permissions

Posted: Sat May 19, 2018 9:33 pm
by hmgchang
Yes and thanks KDJ,
what I don't understand is, I may copy files to my c:\, but
within hmgExe, I can not save a text file by hb_Memowrit to my c:\

any suggestion ?
TIA

best rgds,
Chang

Re: How to check user permissions

Posted: Sat May 19, 2018 10:05 pm
by apais
No program is alowed to write to the root folder since win 7

Re: How to check user permissions

Posted: Sun May 20, 2018 4:37 am
by bpd2000
1. If you run KDJ's program as Administrator in windows then Program is allowed to write to the root folder
Untitled.png
Untitled.png (6.81 KiB) Viewed 4540 times
Or
You can get applications to write to Program Files by changing the folder's security settings.

1. Go to properties and select the Security tab.
2. Click Advanced.
Image
3. Change ownership to your own account instead of TrustedInstaller, and let it propagate. This allows you to add yourself to the list of principals.
4. Click 'Add' and type your username, press 'Check names' and it auto-corrects.
Now you can add 'Full control' for your username, and all applications you run should be able to write to this folder and its sub folders.
Or
Always Run a Program as Administrator in Windows:
Create shortcut of your program, Select shortcut, right click with mouse Go to the properties, and there you will find a button labeled “Advanced.” Click on it. Check the “Run as administrator” box, and then press OK. From now on, this shortcut icon will run the program with administrator's privileges all the time

Re: How to check user permissions

Posted: Sun May 20, 2018 9:54 pm
by hmgchang
Dear Apais,
No program is alowed to write to the root folder since win 7
but I can copy/paste files to the c:\ with win explorer

Dear Bpd2000,
Thanks for the tips
1. Can we set the Administrators rights to the hmg Exe ?
2. Wil the rights remain if we rebuild the Exe ?

TIA

best rgds,
Chang

Re: How to check user permissions

Posted: Sun May 20, 2018 10:10 pm
by dragancesu

Re: How to check user permissions

Posted: Mon May 21, 2018 8:00 am
by bpd2000
hmgchang wrote: Sun May 20, 2018 9:54 pm Dear Apais,
No program is alowed to write to the root folder since win 7
but I can copy/paste files to the c:\ with win explorer

Dear Bpd2000,
Thanks for the tips
1. Can we set the Administrators rights to the hmg Exe ?
2. Wil the rights remain if we rebuild the Exe ?

TIA

best rgds,
Chang
I have not tested / knowledge but it can be done through necessary changes / addition in Manifest
Claudio / Gfilatov / Rathinagiri may help you

Code: Select all

<requestedExecutionLevel
level="asInvoker|highestAvailable|requireAdministrator"
uiAccess="true|false"/>
https://msdn.microsoft.com/en-us/library/bb756929.aspx

Another way is Pack your app into WinRar SFX with silent mode + admin request mode.
I have tested and is working smart

Re: How to check user permissions

Posted: Mon May 21, 2018 9:09 am
by bpd2000
Bpd2000
1. Can we set the Administrators rights to the hmg Exe ?
2. Wil the rights remain if we rebuild the Exe ?
Finally I have managed to generate Manifest file to run app with Admin rights
You have to add reference of manifest file in RC file
Finally working example of app run as Administrators rights
My OS is Win-10, request other users to check it is working Ok or not