3

Using VB6. It's not hard to roll your own, but I wondered if was a prebuilt one out there?

3
  • I haven't found one yet - but there are code snippets around that do this. Commented Jan 1, 2010 at 16:16
  • 2
    You might look at the API calls UrlEscape and UrlUnescape in shlwapi.dll. Commented Jan 1, 2010 at 22:46
  • 1
    Rolling your own is a terrible idea. You will get it wrong. Use a library. Commented Jan 2, 2010 at 20:20

2 Answers 2

4

Prompted by Bob's comment: Google found this wrapper for UrlEscape in a newsgroup post from Karl Peterson.

Private Declare Function UrlEscape Lib "Shlwapi.dll" Alias "UrlEscapeA" ( _
  ByVal pszURL As String, ByVal pszEscaped As String, ByRef pcchEscaped As Long, _
  ByVal dwFlags As Long) As Long

Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000

Private Function EscapeURL(ByVal URL As String) As String
' Purpose:  A thin wrapper for the URLEscape API function. '
Dim EscTxt As String
Dim nLen As Long

' Create a maximum sized buffer. '
nLen = Len(URL) * 3
EscTxt = Space$(nLen)

If UrlEscape(URL, EscTxt, nLen, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then
  EscapeURL = Left$(EscTxt, nLen)
End If
End Function

Disclaimer: I haven't tried this code myself.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll have to break test it but a quick test works fine. Thanks:)
This library does not escape all characters and will still result in issues. For example, if you compare the output to the .NET Framework method "System.Web.HttpUtility.UrlEncode" you'll have completely different results.
2

You should use CoInternetParseUrl(), with URL_ENCODE.

The sample from MSDN, modified for your purposes. Of course, you'll have to figure out how to call CoInternetParseUrl() from VB6, but you seem well on your way to that.

#include <wininet.h>

// ...

WCHAR encoded_url[INTERNET_MAX_URL_LENGTH];
DWORD encoded_url_len = ARRAYSIZE(encoded_url);

// Assumes |url| contains the value you want to encode.

HRESULT hr = CoInternetParseUrl(url, PARSE_CANONICALIZE, URL_ENCODE, encoded_url, 
                        INTERNET_MAX_URL_LENGTH, & encoded_url_len, 0);
if (SUCCEEDED(hr)) {
  // Do stuff...
}

You may want to use PARSE_ENCODE instead of PARSE_CANONICALIZE, depending on your needs.

Also, consider using google-url. May be difficult since it's C++ and not COM based.

2 Comments

Is it cheating for me to upvote your question because of that? :)
Cheating is too strong a word... Perhaps "Conflict of Interest";)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.