开发者

ResolvePath for VB6 - resolve environment variables

开发者 https://www.devze.com 2023-02-04 10:15 出处:网络
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

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?

开发者_StackOverflow中文版

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


Using the seldom used Environ() Function: http://vbcity.com/forums/t/45987.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消