InnoSetup,扩展环境变量(使用{reg:...}从注册表值中获取)

时间:2015-08-15 11:42:06

标签: installer registry environment-variables inno-setup pascalscript

我尝试从注册表设置默认安装路径:

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

我希望获得的目录路径是注册表值数据,它是类型REG_EXPAND_SZ的值,然后我需要扩展其变量,我的情况下的reg值指向相同的路径作为我设置的默认值,一旦{em> InnoSetup 在运行时扩展{userdocs}常量,应该是这样的:

  

C:\ Users \ Administrator \ Documents \ Visual Studio 2015

但不是我把它作为目录路径:

  

C:\ Users \ Administrator \ Desktop \%USERPROFILE%\ Documents \ Visual Studio   2015

enter image description here

我从" C:\ Users \ Administrator \ Desktop "执行安装程序路径,所以这里似乎发生了两件事,第一件事是注册表值的路径只是附加,第二件是当然%USERPROFILE%变量没有扩展。

我怎样才能做到这一点?。

2 个答案:

答案 0 :(得分:4)

以下是我对ElektroStudios解决方案的改进版本:

它负责正确的字符串终止,并且不依赖于Win32函数添加的0终止(猜测在Pascal代码中使用它是不好的。)

[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;

答案 1 :(得分:1)

我找不到在Inno Setup源中使用ExpandEnvironmentStrings函数指出一个事实(如果我错了就纠正我),Inno Setup无法扩展这样的路径(没有函数,也没有常量)为此,或者有一个我不知道的不同的API函数。当然,Inno Setup支持这样的文件名,因为它们被传递给可以在内部扩展它们的系统函数。似乎没有任何函数或常量可以在脚本中执行。我的建议是这样的 hack

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={code:GetDefaultDirName}

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

const
  RegKeyVS2015 = 'Software\Microsoft\VisualStudio\14.0';

function ExpandEnvironmentStrings(lpSrc: string; lpDst: string; nSize: DWORD): DWORD;
  external 'ExpandEnvironmentStrings{#AW}@kernel32.dll stdcall';

function ExpandEnvVars(const Input: string): string;
var
  BufSize: DWORD;
begin
  BufSize := ExpandEnvironmentStrings(Input, #0, 0);
  if BufSize > 0 then
  begin
    SetLength(Result, BufSize);
    if ExpandEnvironmentStrings(Input, Result, Length(Result)) = 0 then
      RaiseException(Format('Expanding env. strings failed. %s', [
        SysErrorMessage(DLLGetLastError)]));
  end
  else
    RaiseException(Format('Expanding env. strings failed. %s', [
      SysErrorMessage(DLLGetLastError)]));
end;

function GetDefaultDirName(Param: string): string;
begin
  if RegQueryStringValue(HKCU, RegKeyVS2015, 'VisualStudioLocation', Result) then
    Result := ExpandEnvVars(Result)
  else
    Result := ExpandConstant('{userdocs}\Visual Studio 2015');
end;