Djang: I haven't looked at JCL fileutils yet? But have done a good deal of web searching for 'working' answers to this (or put another way, wasted a whole day)
First of All I am using Delphi XE (1) under Windows 7, User logged in as a normal account.
Getting the paths to the system folders is simple enough
You need to pass in a constant that is declared in the shfolder unit
e.g. CSIDL_COMMON_APPDATA for the Users Program Data folder
So thanks to Zarko Gajic of about.com Delphi for this
Code:
function GetSpecialFolderPath(folder : integer) : string;
const SHGFP_TYPE_CURRENT = 0;
var path: array [0..MAX_PATH] of char;
begin
if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0])) then
Result := path
else
Result := '';
end;
Then you need to get permission to write in here (assuming the user wont have admin rights)
The most common thing I found was to raise the privileges of your application by using a custom manifest.
Created used this manifest source, there are several example of this on the web
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="3.1.0.0"
processorArchitecture="*"
name="RTO_Monitor"
type="win32"/>
<description>elevate execution level</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="highestAvailable"
uiAccess="true"/>
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!--The ID below indicates application support for Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!--The ID below indicates application support for Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
</application>
</compatibility>
</assembly>
Most speak about compiling this externally using BRCC32, but I found that (in XE anyway) adding the manifest source and the .rc file (below) to the project is the only way to get this to compile at all.
I think XE compiles it for you, but I am not 100% about this.
Code:
1 24 "RTO_Monitor.manifest"
adding this to the main code
Code:
implementation
{$R *.DFM}
{$R 'RTO_Monitor.res'}
It seems to be important not to have any references to XPMan in the code I have none.
Having done this recompiling the project throws up these warnings (for me)
[DCC Warning] W1056 Warning: Duplicate resource: Type 14 (ICON GROUP), ID MAINICON; File C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.RES resource kept; file C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.res resource discarded.
[DCC Warning] W1056 Warning: Duplicate resource: Type 14 (ICON GROUP), ID MAINICON; File C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.RES resource kept; file C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.res resource discarded.
[DCC Warning] W1056 Warning: Duplicate resource: Type 16 (VERSIONINFO), ID 1; File C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.RES resource kept; file C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.res resource discarded.
[DCC Warning] W1056 Warning: Duplicate resource: Type 16 (VERSIONINFO), ID 1; File C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.RES resource kept; file C:\Users\Steve\Newsoft\321\RTO_Monitor\RTO_Monitor.res resource discarded.
When I have seen this sort of thing before I have found that, something will be wrong, perhaps here the .res is not actually going to work, as is born out by the results..
But I am not sure about this at all, no idea about why the warnings occur or how to fix them.
But the project does run.
However....
When attempting to write an ini file to the location it falls over. (access denied)
Code:
FName := ChangeFileExt(extractfilename(Application.ExeName), '.INI' );
FIniFile := TIniFile.Create(GetSpecialFolderPath(CSIDL_COMMON_APPDATA) + '\Application Data\Newsoft\RTO_Monitor\' + FName);
with FIniFile do
try
WriteString(SECTION, 'LoadName', LoadFilename);
//etc..
So it hast worked, now I must decide whether or not to waste another day, or just continue installing apps to a 'new' folder and carry on as before.
Unless anyone can see what is wrong with this?
Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain