Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResolvePath for VB6 - resolve environment variables

I am looking for a function in VB6 (or some WinAPI) that might be able to satisfy this requirement: Take an input path string that includes environment variables, and output that path with environment variables resolved.

For example:

  • Input: "%windir%\System32\"
  • Output: "C:\Windows\System32\"

I could of course write my own parser, but I am wondering if this functionality already exists?

This would be similar to the Spring Framework's "ResolvePath" method.

like image 532
csauve Avatar asked Jan 11 '11 20:01

csauve


2 Answers

Kernel32.dll exports a function called ExpandEnvironmentStrings:

My VB6 is rusty but you can call this by doing:

Declare Function ExpandEnvironmentStrings _
   Lib "kernel32" Alias "ExpandEnvironmentStringsA" _
   (ByVal lpSrc As String, ByVal lpDst As String, _
   ByVal nSize As Long) As Long

Then in a function or sub:

Dim result as Long
Dim strInput As String, strOutput As String
'' Two calls required, one to get expansion buffer length first then do expansion
result = ExpandEnvironmentStrings(strInput, strOutput, result)
strOutput = Space$(result)
result = ExpandEnvironmentStrings(strInput, strOutput, result)
like image 129
Kev Avatar answered Nov 15 '22 09:11

Kev


Worst case you can use the native implementation: ExpandEnvironmentStrings

like image 36
Krypes Avatar answered Nov 15 '22 10:11

Krypes