Page 1 of 1

accessing the program files directory

Posted: Fri Oct 05, 2012 12:26 pm
by sirrab
Hi All,

This has most likley been asked before and I have spent a hour trying to search for an answer. So Help please :)

How do I give my program write access to the program files dir so I can create a directory and add files to this directory.

I have tryed using request administrator mode with out any luck. Im running windows 7 but need it to work on vista too please.

Thank You
Craig

Re: accessing the program files directory

Posted: Fri Oct 05, 2012 1:19 pm
by IdeasVacuum
Could you not simply use an installer app? There is a free one delivered with Windows.

Re: accessing the program files directory

Posted: Fri Oct 05, 2012 10:17 pm
by sirrab
IdeasVacuum wrote:Could you not simply use an installer app? There is a free one delivered with Windows.
I could but then I wouldn't learn how to do something new.
Craig

Re: accessing the program files directory

Posted: Sat Oct 06, 2012 8:09 am
by jesperbrannmark
What installer app is included in windows?

Re: accessing the program files directory

Posted: Sat Oct 06, 2012 9:18 am
by sirrab
jesperbrannmark wrote:What installer app is included in windows?
Good Point :)

Re: accessing the program files directory

Posted: Sat Oct 06, 2012 12:34 pm
by Crusiatus Black
I don't know about the default installer in Windows, but you could use the
fancy BytesSence Install Maker. Free and very nice, requires no scripting.

Written in PureBasic, I believe.

http://www.bytessence.com/bim.html


As for your original question, administrator rights should be enough to be
able to write to the Program Files directory.

This works for me:

Code: Select all

szProgramFiles.s = GetEnvironmentVariable("programfiles")
If(Right(szProgramFiles, 1) <> "\")
  szProgramFiles + "\"
EndIf 

szDirectory.s = szProgramFiles + "Testing\"

If(CreateDirectory(szDirectory))
  hFile = CreateFile(#PB_Any, szDirectory + "test.txt")
  If(hFile)
    Debug WriteString(hFile, "Hello World!!")
    CloseFile(hFile)
  EndIf 
EndIf 

Re: accessing the program files directory

Posted: Sun Oct 07, 2012 6:04 am
by sirrab
Thanks Crusiatus Black,

Is what I needed. I could use that Installer,but always nice to be able to do something your self. Something I might need to know in the furture :)

Thanks
Craig

Re: accessing the program files directory

Posted: Sun Oct 07, 2012 11:12 am
by Inf0Byt3
Here's the code that I use in the install maker to re-launch the installer EXE and gain administrator rights:

http://www.purebasic.fr/english/viewtop ... shExecInfo

To actually gain the rights to write to Program Files, you'll need to explicitly right click your executable and select "Run as Administrator" from the context menu. That should work in theory.

Re: accessing the program files directory

Posted: Sun Oct 07, 2012 12:05 pm
by Danilo
Inf0Byt3 wrote:Here's the code that I use in the install maker to re-launch the installer EXE and gain administrator rights:

http://www.purebasic.fr/english/viewtop ... shExecInfo

To actually gain the rights to write to Program Files, you'll need to explicitly right click your executable and select "Run as Administrator" from the context menu. That should work in theory.
It is the same way recommended by Microsoft. Small working example for self-elevation:

Code: Select all

If ProgramParameter(0)<>"-admin"
    ;
    ; main program
    ;
    MessageRequester("INFO","USER MODE")

    FileName.s = ProgramFilename()
    
    Info.SHELLEXECUTEINFO
    Info\cbSize = SizeOf(SHELLEXECUTEINFO)
    Info\lpVerb = @"runas"
    Info\lpFile = @FileName
    Info\lpParameters = @"-admin"
    Info\nShow = #SW_SHOW
    ShellExecuteEx_(@Info)
Else
    ; dialog for admin mode
    MessageRequester("INFO","ADMIN MODE")
EndIf