8

Is there an easy way to translate a path with system path variables to an absolute path?

So %ProgramFiles%\Internet Explorer\hmmapi.dll becomes C:\Program Files\Internet Explorer\hmmapi.dll

I like to know if there is an API call that can do this, or do I have to do this the hard way and detect %..% sequences and replace them with the corresponding environment variable?

The_Fox
  • 6,992
  • 2
  • 43
  • 69

1 Answers1

17

You can use the WinAPI function ExpandEnvironmentStrings:

function ExpandEnvStr(const szInput: string): string;
  const
    MAXSIZE = 32768;
  begin
    SetLength(Result,MAXSIZE);
    SetLength(Result,ExpandEnvironmentStrings(pchar(szInput),
      @Result[1],length(Result)) - 1);
  end;
Harriv
  • 6,029
  • 6
  • 44
  • 76
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Yes, that was the function I was looking for. I just found it myself too, after finally using the right keywords at google. – The_Fox May 14 '10 at 09:58
  • 4
    ExpandEnvironmentStrings returns the length including the null character, so you have to substract 1 from the result to return the string without the null terminator. – The_Fox May 14 '10 at 10:01
  • Added -1 to the end to strip trailing #0 – Harriv May 03 '17 at 10:17