1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
unit userdir;
//returns directory where user has read/write permissions...
{$IFDEF FPC} {$mode delphi}{$H+} {$ENDIF}
interface
//returns number of cores: a computer with two dual cores will report 4
function IniName: string;
function DefaultsDir (lSubFolder: string): string;
function UserDataFolder: string; //uses shlobj
implementation
{$Include ..\common\isgui.inc}
{$IFDEF UNIX}
uses Process, SysUtils,classes,IniFiles,
{$IFDEF GUI}dialogs;{$ELSE} dialogsx;{$ENDIF}
function UserDataFolder: string;
begin
result :=expandfilename('~/');
end;
function FileNameNoExt (lFilewExt:String): string;
//remove final extension
var
lLen,lInc: integer;
lName: String;
begin
lName := '';
lLen := length(lFilewExt);
lInc := lLen+1;
if lLen > 0 then begin
repeat
dec(lInc);
until (lFileWExt[lInc] = '.') or (lInc = 1);
end;
if lInc > 1 then
for lLen := 1 to (lInc - 1) do
lName := lName + lFileWExt[lLen]
else
lName := lFilewExt; //no extension
Result := lName;
end;
function DefaultsDir (lSubFolder: string): string;
//for Linux: DefaultsDir is ~/appname/SubFolder/, e.g. /home/username/mricron/subfolder/
//Note: Final character is pathdelim
const
pathdelim = '/';
var
lBaseDir: string;
begin
lBaseDir := GetEnvironmentVariable ('HOME')+pathdelim+'.'+ FileNameNoExt(ExtractFilename(paramstr(0) ) );
if not DirectoryExists(lBaseDir) then begin
{$I-}
MkDir(lBaseDir);
if IOResult <> 0 then begin
//Msg('Unable to create new folder '+lBaseDir);
end;
{$I+}
end;
lBaseDir := lBaseDir+pathdelim;
if lSubFolder <> '' then begin
lBaseDir := lBaseDir + lSubFolder;
if not DirectoryExists(lBaseDir) then begin
{$I-}
MkDir(lBaseDir);
if IOResult <> 0 then begin
//you may want to show an error, e.g. showmessage('Unable to create new folder '+lBaseDir);
exit;
end;
{$I+}
end;
result := lBaseDir + pathdelim;
end else
result := lBaseDir;
end;
function IniName: string;
begin
result := DefaultsDir('')+FileNameNoExt(extractfilename(paramstr(0)))+'.ini';
end;
{$ELSE} //If UNIX ELSE NOT Unix
uses
SysUtils, Windows,shlobj;
//for administrators, we can write to folder with executable, otherwise we will save data to the user's AppDataFolder
function AppDataFolder: string; //uses shlobj
{$IFDEF FPC} const CSIDL_APPDATA = 26; {$ENDIF}
var
Path : pchar;
idList : PItemIDList;
begin
GetMem(Path, MAX_PATH);
SHGetSpecialFolderLocation(0, CSIDL_APPDATA , idList);
SHGetPathFromIDList(idList, Path);
Result := string(Path);
FreeMem(Path);
end;
function UserDataFolder: string; //uses shlobj
var
PIDL : PItemIDList;
Folder : array[0..MAX_PATH] of Char;
const CSIDL_PERSONAL = $0005;
begin
SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, PIDL);
SHGetPathFromIDList(PIDL, Folder);
result :=Folder;
end;
(*function UserDataFolder: string; //uses shlobj
var
Path : pchar;
idList : PItemIDList;
begin
GetMem(Path, MAX_PATH);
SHGetSpecialFolderLocation(0, csidl_Personal , idList);
SHGetPathFromIDList(idList, Path);
Result := string(Path);
FreeMem(Path);
end; *)
function IsAdmin: Boolean;
const
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =
(Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
x: Integer;
bSuccess: BOOL;
LastError: integer;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then
begin
Result := True;
exit;
end;
Result := False;
bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
hAccessToken);
if not bSuccess then
begin
if GetLastError = ERROR_NO_TOKEN then
bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
hAccessToken);
end;
if bSuccess then
begin
GetMem(ptgGroups, 1024);
{$IFDEF FPC}
bSuccess := GetTokenInformation(hAccessToken, TokenGroups,
ptgGroups, 1024, @dwInfoBufferSize);
{$ELSE}
bSuccess := GetTokenInformation(hAccessToken, TokenGroups,
ptgGroups, 1024, dwInfoBufferSize);
{$ENDIF}
LastError := GetLastError;
if not bSuccess then begin
//you may want to show an error message..
//showmessage(format('GetLastError %d',[LastError]));
end;
CloseHandle(hAccessToken);
if bSuccess then
begin
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, psidAdministrators);
{$R-}
for x := 0 to ptgGroups.GroupCount - 1 do
if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then
begin
Result := True;
break;
end;
{$R+}
FreeSid(psidAdministrators);
end;
FreeMem(ptgGroups);
end;
end;
function IniName: string;
//only administrators can write to c:\program files -use AppDataFolder for non-Administrators
begin
if isAdmin then
result := changefileext(paramstr(0),'.ini')
else
result := AppDataFolder+'\'+changefileext(extractfilename(paramstr(0)),'.ini');
end;
function DefaultsDir (lSubFolder: string): string;
const
pathdelim = '\';
//for Administrators: DefaultsDir is in the location of the executable, e.g. c:\program files\mricron\subfolder\
//for non-Administrators, the AppDataFolder is returned
//Note: Final character is pathdelim
begin
result := extractfilepath(IniName);
if length(result) < 1 then exit;
if result[length(result)] <> pathdelim then
result := result + pathdelim;
if lSubFolder = '' then
exit;
result := result + lSubFolder;
if result[length(result)] <> pathdelim then
result := result + pathdelim;
end;
{$ENDIF}
end.
|