;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfExpandEnvironmentStrings (strExpSz) intBBSize = DllCall ("KERNEL32.DLL", long:"ExpandEnvironmentStringsA", lpstr:strExpSz, long:0, long:0) hdlBB = BinaryAlloc (intBBSize) BinaryEodSet (hdlBB, intBBSize) intResult = DllCall ("KERNEL32.DLL", long:"ExpandEnvironmentStringsA", lpstr:strExpSz, lpbinary:hdlBB, long:intBBSize) strExpSz = BinaryPeekStr (hdlBB, 0, intResult) hdlBB = BinaryFree (hdlBB) Return strExpSz ; Detlev Dalitz.20090529. #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strMsgTitle = "Demo: udfExpandEnvironmentStrings (strExpSz)" :Test1 ; Set environment variable with length of one byte more than Environment() can handle. blnResult = EnvironSet ("Z", StrFill ("Z", 1280 + 1)) strExpSz = "%%Z%%" strResult = udfExpandEnvironmentStrings (strExpSz) intLen = StrLen (strResult) strMsgText = "ExpandSz variable = " : strExpSz : @LF : "Content = " : strResult : @LF : "Length = " : intLen Message (strMsgTitle, strMsgText) :Test2 blnResult = EnvironSet ("Z", "A=B==C===D") strExpSz = "%%Z%%" strResult = udfExpandEnvironmentStrings (strExpSz) ; strResult ==> 'A=B==C===D' intLen = StrLen (strResult) strMsgText = "ExpandSz variable = " : strExpSz : @LF : "Content = " : strResult : @LF : "Length = " : intLen Message (strMsgTitle, strMsgText) :Test3 strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".' strResult = udfExpandEnvironmentStrings (strExpSz) ; strResult ==> 'My %DD% is "DD" and my %X:\DD\ApplicationData% folder is "X:\DD\ApplicationData".' strMsgText = StrCat (strExpSz, @LF, @LF, strResult) Message (strMsgTitle, strMsgText) :Test4 ; See result of Test3, did we expected this result? ; If we want to save our percent signs, then we have to do do some pre/postprocessing. strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".' strTemp1 = StrReplace (strExpSz, "%%%%", "$$") strTemp2 = udfExpandEnvironmentStrings (strTemp1) strResult = StrReplace (strTemp2, "$$", "%%") ; strResult ==> 'My %UserName% is "DD" and my %AppData% folder is "X:\DD\ApplicationData".' strMsgText = StrCat (strExpSz, @LF, @LF, strTemp1, @LF, @LF, strTemp2, @LF, @LF, strResult) Message (strMsgTitle, strMsgText) Exit
;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfExpandEnvironmentStrings (strExpSz) objWshShell = CreateObject ("WScript.Shell") strExpSz = objWshShell.ExpandEnvironmentStrings(strExpSz) objWshShell = 0 Return strExpSz ; Detlev Dalitz.20090529. #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strMsgTitle = "Demo: udfExpandEnvironmentStrings (strExpSz) using WScript.Shell object." strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".' strResult = StrReplace (udfExpandEnvironmentStrings (StrReplace (strExpSz, "%%%%", "$$")), "$$", "%%") ; strResult ==> 'My %UserName% is "DD" and my %AppData% folder is "X:\DD\ApplicationData".' strMsgText = StrCat (strExpSz, @LF, @LF, strResult) Message (strMsgTitle, strMsgText) Exit