WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Web UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UrlEscape UDF

 Keywords: UrlEscape UrlEscapeA SHLWAPI.DLL DllCall COM DynamicWrapper DynamicWrapperX

;==========================================================================================================================================
; This profiling check includes three code implementations of the same coding algorithm of the "udfUrlEscapeA" function.
;
; To anticipate the result: The Dynamic Wrapper modules are the faster ones.
;
; The speed difference between the three coding variants is that way  ...
; "BinaryBuffer : DynamicWrapperX : DynamicWrapper" behaves somewhat like "5 : 2.5 : 2.5".
;
; This profiling check turns out that the speed blocker seems no to be the WinBatch binary buffers,
; but the WinBatch DllCall function.
;
; Detlev Dalitz.20100128.
;==========================================================================================================================================



;------------------------------------------------------------------------------------------------------------------------------------------
; WinBatch scripting language using WinBatch Binary Buffer and WinBatch function DllCall ().
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfUrlEscapeAV1 (strURL, intFlags)
hdlBB_pcchEscaped = BinaryAlloc (4)                      ; Allocate in/out size-buffer with size of DWORD.
BinaryPoke4 (hdlBB_pcchEscaped, 0, 1)                    ; Poke smallest accepted size=1.
hdlBB_pszEscaped = BinaryAlloc (1)                       ; Allocate test-buffer with smallest accepted size=1.
intResult = DllCall ("SHLWAPI.DLL", long : "UrlEscapeA", lpstr : strURL, lpbinary : hdlBB_pszEscaped, lpbinary : hdlBB_pcchEscaped, long : intFlags) ; Error=-2147024809.
intBufSize = BinaryPeek4 (hdlBB_pcchEscaped, 0)          ; Get required number of characters for the conversion from size-buffer.
hdlBB_pszEscaped = BinaryFree (hdlBB_pszEscaped)         ; Free test out-buffer.
hdlBB_pszEscaped = BinaryAlloc (intBufSize)              ; Allocate out-buffer with required size.
BinaryEodSet (hdlBB_pszEscaped, intBufSize)              ; Set out-buffer EOD to maximal size.
intResult = DllCall ("SHLWAPI.DLL", long : "UrlEscapeA", lpstr : strURL, lpbinary : hdlBB_pszEscaped, lpbinary : hdlBB_pcchEscaped, long : intFlags) ; OK=0.
strURL = BinaryPeekStr (hdlBB_pszEscaped, 0, intBufSize) ; Get converted string.
hdlBB_pszEscaped = BinaryFree (hdlBB_pszEscaped)         ; Free out-buffer.
hdlBB_pcchEscaped = BinaryFree (hdlBB_pcchEscaped)       ; Free size-buffer.
Return strURL
;..........................................................................................................................................
; This UDF "udfUrlEscapeA" converts characters in a URL that might be altered during transport
; across the Internet ("unsafe" characters) into their corresponding escape sequences.
;..........................................................................................................................................
; Syntax:
;   HRESULT UrlEscape(
;       LPCTSTR pszURL,
;       LPTSTR pszEscaped,
;       LPDWORD pcchEscaped,
;       DWORD dwFlags
;   );
;..........................................................................................................................................
; dwFlags:
;   URL_DONT_ESCAPE_EXTRA_INFO  = 33554432   ; 0x02000000 ; Used only in conjunction with URL_ESCAPE_SPACES_ONLY to prevent the conversion of characters in the query (the portion of the URL following the first # or ? character encountered in the string). This flag should not be used alone, nor combined with URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_PERCENT          = 4096       ; 0x00001000 ; Convert any % character found in the segment section of the URL (that section falling between the server specification and the first # or ? character). By default, the % character is not converted to its escape sequence. Other unsafe characters in the segment are also converted normally. Combining this flag with URL_ESCAPE_SEGMENT_ONLY includes those % characters in the query portion of the URL. However, as the URL_ESCAPE_SEGMENT_ONLY flag causes the entire string to be considered the segment, any # or ? characters are also converted. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY.
;   URL_ESCAPE_SEGMENT_ONLY     = 8192       ; 0x00002000 ; Indicates that pszURL contains only that section of the URL following the server component but preceeding the query. All unsafe characters in the string are converted. If a full URL is provided when this flag is set, all unsafe characters in the entire string are converted, including # and ? characters. Combine this flag with URL_ESCAPE_PERCENT to include that character in the conversion. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY or URL_DONT_ESCAPE_EXTRA_INFO.
;   URL_ESCAPE_SPACES_ONLY      = 67108864   ; 0x04000000 ; Convert only space characters to their escape sequences, including those space characters in the query portion of the URL. Other unsafe characters are not converted to their escape sequences. This flag assumes that pszURL does not contain a full URL. It expects only the portions following the server specification. Combine this flag with URL_DONT_ESCAPE_EXTRA_INFO to prevent the conversion of space characters in the query portion of the URL. This flag cannot be combined with URL_ESCAPE_PERCENT or URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_UNSAFE           = 536870912  ; 0x20000000
;
; See also: UrlEscape Function, http://msdn.microsoft.com/en-us/library/bb773774(VS.85).aspx
;..........................................................................................................................................
; (c)Detlev Dalitz.20100127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
; WinBatch scripting language using COM object "DynamicWrapperX v1.0".
; COM object by Author: Yuri Popov 2008 (Ton Plooy 1998, Jeff Stong, Guenter Born).
; Licence: Freeware. Package: "dynwrapx1_00_eng.zip".
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfUrlEscapeAV2 (strURL, intFlags)
objDX = ObjectCreate ("DynamicWrapperX")
objDX.Register("SHLWAPI.DLL", "UrlEscapeA", "f=s", "i=sllu", "r=l")
cchEscaped = "????"                                      ; Allocate in/out size-buffer with size of DWORD i. e. 4.
pcchEscaped = objDX.StrPtr(cchEscaped, "s")              ; Get a pointer to cchEscaped.
objDX.NumPut(1, pcchEscaped, 0, "u")                     ; Poke smallest accepted size=1.
szEscaped = "?"                                          ; Allocate test-buffer with smallest accepted size=1.
pszEscaped = objDX.StrPtr(szEscaped, "s")                ; Get a pointer to szEscaped.
intResult = objDX.UrlEscapeA(strURL, pszEscaped, pcchEscaped, intFlags) ; Error=-2147024809.
intBufSize = objDX.NumGet(pcchEscaped, 0, "u")           ; Get required number of characters for the conversion from size-buffer.
szEscaped = StrFill ("?", intBufSize)                    ; Allocate out-buffer with required size.
pszEscaped = objDX.StrPtr(szEscaped, "s")                ; Get pointer to out-buffer.
intResult = objDX.UrlEscapeA(strURL, pszEscaped, pcchEscaped, intFlags) ; Error=0
szEscaped = objDX.StrGet(pszEscaped, "s")                ; Get converted string.
Drop (objDX)                                             ; Release object.
Return szEscaped
;..........................................................................................................................................
; This UDF "udfUrlEscapeA" converts characters in a URL that might be altered during transport
; across the Internet ("unsafe" characters) into their corresponding escape sequences.
;..........................................................................................................................................
; Syntax:
;   HRESULT UrlEscape(
;       LPCTSTR pszURL,
;       LPTSTR pszEscaped,
;       LPDWORD pcchEscaped,
;       DWORD dwFlags
;   );
;..........................................................................................................................................
; dwFlags:
;   URL_DONT_ESCAPE_EXTRA_INFO  = 33554432   ; 0x02000000 ; Used only in conjunction with URL_ESCAPE_SPACES_ONLY to prevent the conversion of characters in the query (the portion of the URL following the first # or ? character encountered in the string). This flag should not be used alone, nor combined with URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_PERCENT          = 4096       ; 0x00001000 ; Convert any % character found in the segment section of the URL (that section falling between the server specification and the first # or ? character). By default, the % character is not converted to its escape sequence. Other unsafe characters in the segment are also converted normally. Combining this flag with URL_ESCAPE_SEGMENT_ONLY includes those % characters in the query portion of the URL. However, as the URL_ESCAPE_SEGMENT_ONLY flag causes the entire string to be considered the segment, any # or ? characters are also converted. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY.
;   URL_ESCAPE_SEGMENT_ONLY     = 8192       ; 0x00002000 ; Indicates that pszURL contains only that section of the URL following the server component but preceeding the query. All unsafe characters in the string are converted. If a full URL is provided when this flag is set, all unsafe characters in the entire string are converted, including # and ? characters. Combine this flag with URL_ESCAPE_PERCENT to include that character in the conversion. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY or URL_DONT_ESCAPE_EXTRA_INFO.
;   URL_ESCAPE_SPACES_ONLY      = 67108864   ; 0x04000000 ; Convert only space characters to their escape sequences, including those space characters in the query portion of the URL. Other unsafe characters are not converted to their escape sequences. This flag assumes that pszURL does not contain a full URL. It expects only the portions following the server specification. Combine this flag with URL_DONT_ESCAPE_EXTRA_INFO to prevent the conversion of space characters in the query portion of the URL. This flag cannot be combined with URL_ESCAPE_PERCENT or URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_UNSAFE           = 536870912  ; 0x20000000
;
; See also: UrlEscape Function, http://msdn.microsoft.com/en-us/library/bb773774(VS.85).aspx
;..........................................................................................................................................
; (c)Detlev Dalitz.20100127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
; WinBatch scripting language using WinBatch Binary Buffer and COM object "DynamicWrapper".
; COM object by Author: Guenter Born (Ton Plooy 1998, Jeff Stong).
; Licence: Freeware. Packages: "dynawrap95.zip", "dynawrapNt.zip".
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfUrlEscapeAV3 (strURL, intFlags)
objDX = ObjectCreate ("DynamicWrapper")
objDX.Register("SHLWAPI.DLL", "UrlEscapeA", "f=s", "i=sllu", "r=l")
cchEscaped = BinaryAlloc (4)                             ; Allocate in/out size-buffer with size of DWORD=4.
BinaryPoke4 (cchEscaped, 0, 1)                           ; Poke smallest accepted size=1.
szEscaped = BinaryAlloc (1)                              ; Allocate test-buffer with smallest accepted size=1.
pcchEscaped = BinaryBufInfo (cchEscaped, 1)              ; Get pointer to in/out size-buffer.
pszEscaped = BinaryBufInfo (szEscaped, 1)                ; Get pointer to out-buffer.
intResult = objDX.UrlEscapeA(strURL, pszEscaped, pcchEscaped, intFlags) ; Error=-2147467261.
intBufSize = BinaryPeek4 (cchEscaped, 0)                 ; Get required number of characters for the conversion from size-buffer.
szEscaped = BinaryFree (szEscaped)                       ; Free test out-buffer.
szEscaped = BinaryAlloc (intBufSize)                     ; Allocate out-buffer with required size.
pszEscaped = BinaryBufInfo (szEscaped, 1)                ; Get pointer to out-buffer.
intResult = objDX.UrlEscapeA(strURL, pszEscaped, pcchEscaped, intFlags) ; Error=0
BinaryEodSet (szEscaped, intBufSize)                     ; Set out-buffer EOD to maximal size.
strURL = BinaryPeekStr (szEscaped, 0, intBufSize)        ; Get converted string.
szEscaped = BinaryFree (szEscaped)                       ; Free out-buffer.
cchEscaped = BinaryFree (cchEscaped)                     ; Free size-buffer.
Drop (objDX)                                             ; Release object.
Return strURL
;..........................................................................................................................................
; This UDF "udfUrlEscapeA" converts characters in a URL that might be altered during transport
; across the Internet ("unsafe" characters) into their corresponding escape sequences.
;..........................................................................................................................................
; Syntax:
;   HRESULT UrlEscape(
;       LPCTSTR pszURL,
;       LPTSTR pszEscaped,
;       LPDWORD pcchEscaped,
;       DWORD dwFlags
;   );
;..........................................................................................................................................
; dwFlags:
;   URL_DONT_ESCAPE_EXTRA_INFO  = 33554432   ; 0x02000000 ; Used only in conjunction with URL_ESCAPE_SPACES_ONLY to prevent the conversion of characters in the query (the portion of the URL following the first # or ? character encountered in the string). This flag should not be used alone, nor combined with URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_PERCENT          = 4096       ; 0x00001000 ; Convert any % character found in the segment section of the URL (that section falling between the server specification and the first # or ? character). By default, the % character is not converted to its escape sequence. Other unsafe characters in the segment are also converted normally. Combining this flag with URL_ESCAPE_SEGMENT_ONLY includes those % characters in the query portion of the URL. However, as the URL_ESCAPE_SEGMENT_ONLY flag causes the entire string to be considered the segment, any # or ? characters are also converted. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY.
;   URL_ESCAPE_SEGMENT_ONLY     = 8192       ; 0x00002000 ; Indicates that pszURL contains only that section of the URL following the server component but preceeding the query. All unsafe characters in the string are converted. If a full URL is provided when this flag is set, all unsafe characters in the entire string are converted, including # and ? characters. Combine this flag with URL_ESCAPE_PERCENT to include that character in the conversion. This flag cannot be combined with URL_ESCAPE_SPACES_ONLY or URL_DONT_ESCAPE_EXTRA_INFO.
;   URL_ESCAPE_SPACES_ONLY      = 67108864   ; 0x04000000 ; Convert only space characters to their escape sequences, including those space characters in the query portion of the URL. Other unsafe characters are not converted to their escape sequences. This flag assumes that pszURL does not contain a full URL. It expects only the portions following the server specification. Combine this flag with URL_DONT_ESCAPE_EXTRA_INFO to prevent the conversion of space characters in the query portion of the URL. This flag cannot be combined with URL_ESCAPE_PERCENT or URL_ESCAPE_SEGMENT_ONLY.
;   URL_ESCAPE_UNSAFE           = 536870912  ; 0x20000000
;
; See also: UrlEscape Function, http://msdn.microsoft.com/en-us/library/bb773774(VS.85).aspx
;..........................................................................................................................................
; (c)Detlev Dalitz.20100127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;==========================================================================================================================================
; This profiling loop includes three variants of coding algorithms of the "udfUrlEscapeA" function.
;==========================================================================================================================================

; Which tests to check.
strListTests = "1,2,3"

; How many loops to run.
intLoopMax = 1000

; Init for all.
ClipPut ("")
intFlags = 4096 ; URL_ESCAPE_PERCENT
;intFlags = 8192 ; URL_ESCAPE_SEGMENT_ONLY
strUrl = "http://microsoft.com/test/t%%e<s t.asp?url=/{ex%% ample</abc.asp?frame=true#fr%%agment"

; Loop.
BoxOpen ("Test", "Be patient ...")
intTestMin = 1
intTestMax = ItemCount (strListTests, ",")
For intItem = intTestMin To intTestMax
   intTest = ItemExtract (intItem, strListTests, ",")
   intTicks%intTest% = 0
   For intLoop = 1 To intLoopMax
      strMsgText = "Test: " : intTestMax : "/" : intTest : @LF : "Loop: " : intLoopMax : "/" : intLoop : @LF : "Ticks%intTest%: " : intTicks%intTest%
      BoxText (strMsgText)
      Exclusive (@ON)
      intTicksStart = GetTickCount ()
      GoSub Test%intTest%
      intTicksStop = GetTickCount ()
      Exclusive (@OFF)
      intTicks%intTest% = intTicks%intTest% + intTicksStop - intTicksStart
   Next
   BoxText (strMsgText)
Next
BoxShut ()

; Result.
Decimals (0)
intTicksSum = 1 ; To prevent dividing by zero.
For intTest = intTestMin To intTestMax
   intTicksSum = intTicksSum + intTicks%intTest%
Next
For intTest = intTestMin To intTestMax
   intPct%intTest% = 100.0 * intTicks%intTest% / intTicksSum
Next

; Format output;
strTest = "Test"
strTicks = "Ticks"
strPct = "Pct"
strSep = "  "
strPre = "; "
intLenTest = Max (StrLen (strTest), StrLen (intTestMax))
intLenTicks = StrLen (strTicks)
intLenPct = StrLen (strPct)
For intItem = intTestMin To intTestMax
   intTest = ItemExtract (intItem, strListTests, ",")
   intLenTicks = Max (intLenTicks, StrLen (intTicks%intTest%))
   intLenPct = Max (intLenPct, StrLen (intPct%intTest%))
Next
strTest = StrFixLeft (strTest, " ", intLenTest)
strTicks = StrFixLeft (strTicks, " ", intLenTicks)
strPct = StrFixLeft (strPct, " ", intLenPct)
strResult = ""
strResult = ItemInsert (StrCat (strPre, "Iterations: ", intLoopMax), -1, strResult, @LF)
strResult = ItemInsert (StrCat (strPre, strTest, strSep, strTicks, strSep, strPct), -1, strResult, @LF)
For intTest = intTestMin To intTestMax
   strTest = StrFixLeft (intTest, " ", intLenTest)
   strTicks = StrFixLeft (intTicks%intTest%, " ", intLenTicks)
   strPct = StrFixLeft (intPct%intTest%, " ", intLenPct)
   strResult = ItemInsert (StrCat (strPre, strTest, strSep, strTicks, strSep, strPct), -1, strResult, @LF)
Next
strResult = StrCat (strResult, @LF)
Message ("Test Result", strResult)
ClipPut (strResult)
Exit

;------------------------------------------------------------------------------------------------------------------------------------------
; GoSub's.
;------------------------------------------------------------------------------------------------------------------------------------------
:Test1
strUrlEscaped = udfUrlEscapeAV1 (strURL, intFlags)
Return
;------------------------------------------------------------------------------------------------------------------------------------------
:Test2
strUrlEscaped = udfUrlEscapeAV2 (strURL, intFlags)
Return
;------------------------------------------------------------------------------------------------------------------------------------------
:Test3
strUrlEscaped = udfUrlEscapeAV3 (strURL, intFlags)
Return
;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================



;------------------------------------------------------------------------------------------------------------------------------------------
; The test results are different in detail but meaningful in general.
;------------------------------------------------------------------------------------------------------------------------------------------
; strUrl = "http://microsoft.com/test/t%%e<s t.asp?url=/{ex%% ample</abc.asp?frame=true#fr%%agment"
;------------------------------------------------------------------------------------------------------------------------------------------
; URL_ESCAPE_PERCENT
;
; Iterations: 10000
; Test  Ticks  Pct
;    1  29607   43  udfUrlEscapeAV1
;    2  19374   28  udfUrlEscapeAV2
;    3  19509   28  udfUrlEscapeAV3
;
; Iterations: 4000
; Test  Ticks  Pct
;    1  11816   57  udfUrlEscapeAV1
;    2   5155   25  udfUrlEscapeAV2
;    3   3789   18  udfUrlEscapeAV3
;
; Iterations: 4000
; Test  Ticks  Pct
;    1  11900   54  udfUrlEscapeAV1
;    2   4491   20  udfUrlEscapeAV2
;    3   5757   26  udfUrlEscapeAV3
;
;------------------------------------------------------------------------------------------------------------------------------------------
; URL_ESCAPE_SEGMENT_ONLY
;
; Iterations: 1000
; Test  Ticks  Pct
;    1   3240   49  udfUrlEscapeAV1
;    2   1657   25  udfUrlEscapeAV2
;    3   1715   26  udfUrlEscapeAV3
;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================



Article ID:   W18414
Filename:   UrlEscape UDF.txt
File Created: 2010:02:02:11:21:24
Last Updated: 2010:02:02:11:21:24