当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:

Worst case you can use the native implementation: ExpandEnvironmentStrings

网友答案:

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)
网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: