0

I've been trying to write to a file in the system dir on my computers' but I can't get it to work;what would I do to get the necessary permissions?I need to update the SS without having to manually change the permissions on each and every computer...

    var
    z: THandle;
...
    begin
    ....
    z:=OpenFile('myfile.scr',a,OF_WRITE);
    FileWrite(z,buf[16],3124);
    FileClose(z);
    end;
user0
  • 129
  • 3
  • 9
  • This isn't permitted by the operating system without being logged in as an Administrator. It's not a Delphi issue. Your app would have to be run by an administrator. – Ken White Apr 05 '13 at 18:30
  • I ran it as admin and it still didn't write to the system dir(windows 8).And I already know it's not a Delphi issue lol(sorry if the source I posted confuses people). – user0 Apr 05 '13 at 18:31
  • It looks like suspicious code to me, but the problem is operating system privileges, and you can't set them without Administrator rights. There are dozens of posts here about operating system privileges; search here on `[windows] file privilege` for some of the previous questions. – Ken White Apr 05 '13 at 18:35
  • on Vista and above you also need to obtain `UAC Elevation` – Arioch 'The Apr 08 '13 at 07:01

2 Answers2

2

Most likely this is down the the file system redirector. Assuming you really are running elevated then you will have sufficient rights. But a 32 bit process sees the 32 bit system folder SysWOW64. That's probably where your file is landing.

If you really do need to write to the 64 bit system directory either run a 64 bit process, or write to the sysnative alias.

I'm not quite sure why you are using the legacy OpenFile. It would be normal to use CreateFile. Or even TFileStream.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I just injected the code into explorer and had it do the work for me(better method though my av detected it as a virus imao). – user0 Apr 05 '13 at 21:34
1

You could add a manifest to your application as described here:

How to add manifest <requestedPrivileges> info into delphi project

This way, on application startup, Windows UAC will prompt for administrator privileges. The post is about Delphi XE but I've tested the manifest successfully on Delphi 2005.

During development, be sure to start the Delphi IDE with administrator privileges too. Otherwise, it might crash when you try to run/debug the program out of the IDE.

Community
  • 1
  • 1
Marcellus
  • 1,277
  • 8
  • 7