tags:

views:

96

answers:

2

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

+1  A: 

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.

MarkJ
I'll have to break test it but a quick test works fine. Thanks:)
Aaron Bush
+1  A: 

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.

jeffamaphone
Good tip. I'd upvote you but I need 2 more rep rofl:)
Aaron Bush
Is it cheating for me to upvote your question because of that? :)
jeffamaphone
Cheating is too strong a word... Perhaps "Conflict of Interest";)
Aaron Bush