3

I have an Inno Setup installer for an app that offers the user a choice between "Install for all users" and "Install just for me". If it is installed for all users, the INI file is placed in commonappdata and the settings are shared (this is a customer requirement). If it is installed for the current user, it is placed in Local settings appdata.

Once the app has been installed, what's the cleanest way for the app to "know" at run-time what the installation option used was and thus where it should read and save the INI file from? Also, I want the 'default' folder (the one the user is placed in the first time they try to open a data file) to be MyDocs for the single user install, and SharedDocs for the all users install, and I'm assuming the solution will work for that also.

rossmcm
  • 5,493
  • 10
  • 55
  • 118

1 Answers1

1

Let setup write a registry value under HKCU if "install just for me" is chosen. Query the key at program startup and determine the default folder accordingly.

If you're using a task to let the user choose the installation type, you can use a registry entry like this:

[Registry]
Root: HKCU; SubKey: SOFTWARE\MyCompany\MyProg; ValueType: dword; ValueName: InstallUserOnly; ValueData: 1; Tasks: install_just_for_me; Flags: UninsDeleteValue; 

Then at application startup you can do something like:

function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean): string;
begin
  SetLength(Result, MAX_PATH + 1);
  SHGetSpecialFolderPath(0, PChar(Result), FOLDER, CanCreate);
  Result := PChar(Result);
end;

...
var
  Reg: TRegistry;
  DefFolder: string;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\SOFTWARE\MyCompany\MyProg', False) then begin
      if Reg.ValueExists('InstallUserOnly') then
        DefFolder := GetSpecialFolderPath(CSIDL_PERSONAL, True)
      else
        DefFolder := GetSpecialFolderPath(CSIDL_COMMON_DOCUMENTS, True);
    end;
  finally
    Reg.Free;
  end;

You can of course also read the registry value if you like.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • Note that if you use the 'task' approach, you can read if it is selected at install time at `HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{AppID}_is1` from the value `Inno Setup: Selected Tasks`. But as there's a possibility for one user to install for himself and another for all users, this might not be reliable. – Sertac Akyuz Apr 20 '11 at 22:44
  • Thanks, @Sertac. Good answer. You started me thinking. The app installs a default INI file if there is not one found. Presumably I could instead add an entry to the INI file after I have installed it to achieve the same thing? – rossmcm Apr 20 '11 at 23:49
  • @rossmcm - You're welcome!  I don't see why not. You can very well keep track of installations with sth like '[InstallType]' 'username=userinstall' 'otheruser=commoninstall'... – Sertac Akyuz Apr 21 '11 at 00:15
  • @Sertac. Just another thing. I'm not using tasks, I'm using the check: parameter to conditionally execute Inno statments. Presumably I could code: `[Registry] Root: HKCU; SubKey: SOFTWARE\MyCompany\MyProg; ValueType: dword; ValueName: InstallUserOnly; ValueData: 1; check: InstallForJustMe ; Flags: UninsDeleteValue;` instead (InstallForJustMe is a boolean function defined in the code section). – rossmcm Apr 21 '11 at 01:28
  • @Sertac. Just yet another thing. The ideal would be for the installer to leave the type of installation (which can be one of 3 - "just me", "everyone, but separate settings for each user", or "everyone, all users share the same settings") in a location that was accessible to all users of the app. Can I install something into HKLM with `[Registry] Root: HKLM; SubKey: SOFTWARE\MyCompany\MyProg; ValueType: string; ValueName: InstallationType; ValueData: "Just me"; check: InstallForJustMe ; Flags: UninsDeleteValue;`? Then all runs of the app can see it, no matter what install option was used. – rossmcm Apr 24 '11 at 01:54
  • @rossmcm - If you're asking my opinion on it, this ideal scenario is over-complication.. I've never seen an installer that does anything different than installing the start-menu icons to all-users folder or to the specific user's folder depending on the chosen install type. Why should everyone use the same settings? what's the point in it? What if one user installs the application choosing this and another user is not content with this decision? Or what if a user chooses "just me" and then another logs on to the computer and browses to program files and double clicks the executable. .. – Sertac Akyuz Apr 25 '11 at 00:03
  • .. Will you launch a message saying "sorry you cannot use this application". If you're asking technically, yes it's possible, by default 'users' have read access to 'HKLM\Software'. And you can even set the installer to give your registry key read access to "everyone". – Sertac Akyuz Apr 25 '11 at 00:04
  • @Sertac - it's complicated (naturally). The point is that the application is used in a lab environment, installed on one PC which is used by many users. The PC has specialist hardware attached. The INI file contains hardware settings (e.g. COM port in use, etc) which the customer needs to be able to set up once, and know that all users will have the same settings. In this environment I need the "all users, shared settings" type of installation. This setup option is additional to the usual 2 of "just me" and "all users, separate settings", which I also provide... – rossmcm Apr 25 '11 at 19:34
  • 1
    ... I figure that I will get the installation type choice from the user at setup time, and write it to HKLM. When app starts, I read HKLM and decide where to read/store the INI file on the basis of which installation type it is. – rossmcm Apr 25 '11 at 19:37