VB6 help

thatbox

Senior member
Dec 5, 2002
253
0
76
I want to make a nice little proggy that will check all my favorite sites for updates by reading the html and comparing it to the last time it checked. However, I am not very experienced in even disk IO operations, so I need some help. What is the code that I would use to direct the program to a website? I'll mainly be using this for Penny-Arcade and MegaTokyo. If it would be easier to do in C++ or TurboPascal, I can do those too, I just think it would be nice to have a GUI and maybe keep it in the system tray, neither of which I am capable of doing in the other two languages.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
You could use the Winsock ActiveX Control in VB6 to manually negotiate an HTTP GET request to the respective website. You can also use a COM component like ASPTear to make the request; much easier, in my opinion. Microsoft's XML SDK also includes a COM component that was intended to be used to retrieve XML data from HTTP GET requests, but you would use it for this purpose as well.

If you want to do it in C++ you could use the same COM component, but COM programming in C+ is daunting if you're not familiar. The easier option for both C and C++ would be to use the WinInet API that's part of the Win32 API. Look at the following functions: InternetOpen(), InternetConnect(), InternetOpenUrl(), HttpOpenRequest(), etc..
 

thatbox

Senior member
Dec 5, 2002
253
0
76
Neither of those websites explains (well enough for me to understand) how exactly it works or what exactly it does, at least for someone with my knowledge. From the MS page:

Private Sub Command1_Click()
Dim HttpReq As New MSXML2.XMLHTTP

HttpReq.open "GET", "http://XMLSampleServer/CatalogServer.asp", False
HttpReq.send
MsgBox HttpReq.responseText
End Sub

Does that display the html for that page in a message box? I'm just not following what the code is doing. Where and how exactly does it store the html?
How would I use ASPTear to do what I'm trying to do? I'm sorry if I'm acting like a n00b, but a n00b is what I'll stay if i dont learn!

Edit: Ah, this thing is very close to what I am looking for. Thanks! but still, any help would be greatly appreciated!
 

thatbox

Senior member
Dec 5, 2002
253
0
76
I dled and examined the program that the last link discusses, and it will be very helpful. There is one thing though: I have no ASPtear.tlb... I've got ASPtear.dll, but no type library (I think), so I can't use any ASPtear features.
 

AndyHui

Administrator Emeritus<br>Elite Member<br>AT FAQ M
Oct 9, 1999
13,141
17
81
Have you ever seen the Coolmon program? There are a number of plugins to the Coolmon program that do almost exactly what you want, such as going to a weather page and grabbing the latest information.

Check it out here. I suggest you have a look at the Information from the Internet section to see if there is something as close as possible to what you want. Most of them are written in VBScript.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: thatbox
I dled and examined the program that the last link discusses, and it will be very helpful. There is one thing though: I have no ASPtear.tlb... I've got ASPtear.dll, but no type library (I think), so I can't use any ASPtear features.

The typelib is part of ASPTear.dll, you don't need a separate typelib to use the exported COM coclasses. Just reference ASPTear.dll.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: thatbox

Private Sub Command1_Click()
Dim HttpReq As New MSXML2.XMLHTTP

HttpReq.open "GET", "http://XMLSampleServer/CatalogServer.asp", False
HttpReq.send
MsgBox HttpReq.responseText
End Sub

The code above does this.

1. Creates a new XMLHTTP object called HttpReq.
2. Sets up the object to use an HTTP GET command, sets the url to "http://XMLSampleServer/CatalogServer.asp" and sets the asynchronous property to False. Setting asynchronous to false causes the XMLHTTP object to wait until a response has been received from the URL in question before returning control to the rest of code.
3. The send command executes the HTTP parameters set with the open command.
4. Once the command has been set, a few properties are available:

HttpReq.status (the status code returned from the server, e.g. 200, 404)
HttpReq.statusText (any message associated with the status code)
HttpReq.responseText (the actual response stored as a string -- this is what you're looking for)

XMLHTTP is extremely easy to use and works well within VB. Once you've installed the XML SDK, go to Start / Programs / MSXML 4.0 / MSXML 4.0 Parser SDK. You'll find the necessary information under XML Reference / XML Helper Objects/Interfaces / IXMLHTTPRequest.

Good luck.
 

thatbox

Senior member
Dec 5, 2002
253
0
76
Alright, got the MSXML 4.0 SDK and tried out that above code. I got this error message when I clicked Command1:
"Method 'send' of object 'ISMHTTPRequest' failed."
Why does it do that?
 

thatbox

Senior member
Dec 5, 2002
253
0
76
Now an Disk IO question. I'm saving the addresses to check in a txt file, so it can load them each time it runs. I'm also trying to add the ability to modify a site, or add a new one (there a 6). When the user modifies a site, I want to go into the txt file to a specific line (the line that contains that site's URL) and replace only that line. ForReading won't help me, ForWriting seems to replace the whole file, and ForAppending only writes to the end of the file... What can I do?

I think I have an idea: On loading, I'll put each existing site URL into a string, called Site1, Site2, etc. Then, when they modify a URL, the changes will be made to the corresponding string. Then I'll just rewrite all of them to the text file. Sound good?

Edit: That works. New problem though. For the first site and only the first, when I write the HTML to its own file (site1cache.txt), the compiler tells me "Invalid procedure call or argument." The line it does this with is

fsoStream.Write (HTML)

The code for the first site (I have six) is EXACTLY the same as the code for the other 5 (In fact I copied and pasted, changing only the filenames when needed). I messed around a bit and found out that it can write non-variable strings just fine (ie, fsoStream.Write ("Quack")). Why might it be having trouble with my HTML string variable, that contains the HTTPReq.ResponseText, only for the first one? Here's some of the code:

Private Sub CmdCheck_Click()
Dim HttpReq As New MSXML2.XMLHTTP
Dim FileSys As FileSystemObject
Dim f As File
Dim fsoStream As TextStream
Dim CACHE As String
Dim HTML As String
Set fsoStream = Nothing
Set FileSys = Nothing
Set f = Nothing
''''''''''''''''''
Set FileSys = New FileSystemObject
Set f = FileSys.GetFile("C:\windows\HTML checker\site1cache.txt")
If txtSite2 <> "" Then
HttpReq.open "GET", txtSite1, False
HttpReq.send
If (HttpReq.Status > 299 Or HttpReq.Status < 200) Then
MsgBox "Site 1 could not be reached!", vbExclamation, "Error"
Else
HTML = HttpReq.responseText
Set fsoStream = f.OpenAsTextStream(ForWriting)
fsoStream.Write (HTML)
End If

Set fsoStream = f.OpenAsTextStream(ForReading)
CACHE = fsoStream.ReadAll
If HTML <> CACHE Then
site1 = 1
Set fsoStream = f.OpenAsTextStream(ForWriting)
fsoStream.Write (HTML)
End If
End If
Set fsoStream = Nothing
Set FileSys = Nothing
Set f = Nothing
''''''''''''''''''''
Set FileSys = New FileSystemObject
Set f = FileSys.GetFile("C:\windows\HTML checker\site2cache.txt")
If txtSite2 <> "" Then
HttpReq.open "GET", txtSite2, False
HttpReq.send
If (HttpReq.Status > 299 Or HttpReq.Status < 200) Then
MsgBox "Site 2 could not be reached!", vbExclamation, "Error"
Else
HTML = HttpReq.responseText
Set fsoStream = f.OpenAsTextStream(ForWriting)
fsoStream.Write (HTML)
End If

Set fsoStream = f.OpenAsTextStream(ForReading)
CACHE = fsoStream.ReadAll
If HTML <> CACHE Then
site2 = 1
Set fsoStream = f.OpenAsTextStream(ForWriting)
fsoStream.Write (HTML)
End If
End If
Set fsoStream = Nothing
Set FileSys = Nothing
Set f = Nothing
''''''''''''''''''''''