InnoSetup, expand environment variable (taken from registry value using {reg:…} )

前端 未结 2 753
说谎
说谎 2021-01-21 03:39

I try to set the default install path from the registry:

DefaultDirName={reg:HKCU\\Software\\Microsoft\\VisualStudio\\14.0,VisualStudioLocation|{userdocs}\\Visua         


        
2条回答
  •  走了就别回头了
    2021-01-21 04:11

    Here is my improved version of ElektroStudios' solution:

    It takes care of correct string-termination and does not rely on the 0-termination added by the Win32-function (guess it's not good to use that in Pascal code).

    [Code]
    #ifdef UNICODE
    #define AW "W"
    #else
    #define AW "A"
    #endif
    
    function ExpandEnvironmentStrings(lpSrc: String; lpDst: String; nSize: DWORD): DWORD;
    external 'ExpandEnvironmentStrings{#AW}@kernel32.dll stdcall';
    
    function ExpandEnvVars(const Input: String): String;
    var
      Buf: String;
      BufSize: DWORD;
    begin
      BufSize := ExpandEnvironmentStrings(Input, #0, 0);
      if BufSize > 0 then
      begin
        SetLength(Buf, BufSize);  // The internal representation is probably +1 (0-termination)
        if ExpandEnvironmentStrings(Input, Buf, BufSize) = 0 then
          RaiseException(Format('Expanding env. strings failed. %s', [SysErrorMessage(DLLGetLastError)]));
    #if AW == "A"
        Result := Copy(Buf, 1, BufSize - 2);
    #else
        Result := Copy(Buf, 1, BufSize - 1);
    #endif
      end
      else
        RaiseException(Format('Expanding env. strings failed. %s', [SysErrorMessage(DLLGetLastError)]));
    end;
    

提交回复
热议问题