HtmlEncode in VB


I need to generate HTML code
from a VB application.

In order for the HTML code to be parsed properly by he browser, I need to properly encoded. I know that ASP's Server object has an HTMLEncode method.

I did not manage to use the Server object from my VB application. Is there any other way of encoding HTML code.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You will need WinINet DLL which comes with IE4 or greater

Here is the API function call:
Private Declare Function InternetCanonicalizeUrl Lib "wininet.dll" Alias "InternetCanonicalizeUrlA" (ByVal lpszUrl As String, ByVal lpszBuffer As String, lpdwBufferLength As Long, ByVal dwFlags As Long) As Long

Here is a wrapper function:
Public Function URLEncode(ByVal sURL As String) As String

Dim sBuff As String
sBuff = Space$(Len(sURL) * 2)

Dim sLen As Long
sLen = Len(sBuff) - 1

Call InternetCanonicalizeUrl(sURL, sBuff, sLen, 0)
URLEncode = Left(sBuff, sLen)

End Function

- Loren
rakhrasAuthor Commented:
It's not URLEncode that I need but rather HTMLEncode. It's not the same thing.
rakhrasAuthor Commented:
Adjusted points to 200

It turns out I need the same thing.  There is no info anywhere on what is the best way to do this.  I wrote this Javascript below.  I have no idea if this is the safest way to do this, but it is working very well for displaying HTML and script source on a web page for me.  I added a quick conversion to VB at the bottom.

function clientHTMLEncode(sSrc)
var sRet = sSrc;
sRet = sRet.replace(/&/g,"&")
sRet = sRet.replace(/</g,"&lt;")
sRet = sRet.replace(/>/g,"&gt;")
sRet = sRet.replace(/ /g,"&nbsp;")
sRet = sRet.replace(/"/g,"&quot;")
return sRet;

I am not sure if you know javascript, so here is the VB Version (Unchecked)

Visual Basic:
function HTMLEncode(byval sSrc as String) as String

Dim sOut as String
sOut = sSrc
sOut = Replace(sOut,"&","&amp;")
sOut = Replace(sOut,"<","&lt;")
sOut = Replace(sOut,">","&rt;")
sOut = Replace(sOut," ","&nbsp;;")
sOut = Replace(sOut,"""","&quot;")

HTMLEncode = sOut

End Function

- Loren

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.