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:
"%windir%\System32\"
"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.
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)
Worst case you can use the native implementation: ExpandEnvironmentStrings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With