Using VB6. It's not hard to roll your own, but I wondered if was a prebuilt one out there?
-
I haven't found one yet - but there are code snippets around that do this.quamrana– quamrana2010-01-01 16:16:23 +00:00Commented Jan 1, 2010 at 16:16
-
2You might look at the API calls UrlEscape and UrlUnescape in shlwapi.dll.Bob77– Bob772010-01-01 22:46:44 +00:00Commented Jan 1, 2010 at 22:46
-
1Rolling your own is a terrible idea. You will get it wrong. Use a library.i_am_jorf– i_am_jorf2010-01-02 20:20:02 +00:00Commented Jan 2, 2010 at 20:20
Add a comment
|
2 Answers
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.
2 Comments
Aaron Bush
I'll have to break test it but a quick test works fine. Thanks:)
Warren Rox
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.
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
i_am_jorf
Is it cheating for me to upvote your question because of that? :)
Aaron Bush
Cheating is too strong a word... Perhaps "Conflict of Interest";)