This is little example showing how to Google things using COM interface.
I meant this example as something other people experimenting with COM under ThinBASIC can learn from, as it shows:
- How to create objects
- How to access properties
- How to invoke methods with parameters
- To not forget release objects
Example is based on José Roca PowerBASIC example, the final code took the COM methods from it, while the whole COM manipulation is ThinBASIC specific.
[code=thinBasic]
' -- Example how to programatically Google things, based on example by José Roca
' -- Petr Schreiber, 2009
' -- V2.0
uses "COM", "Console"
function tbMain()
%READYSTATE_COMPLETE = 4
LOCAL pIWebBrowser2 AS DWORD ' // Reference to the IWebBrowser2 interface
LOCAL pIHTMLDocument3 AS DWORD ' // Reference to the IHTMLDocument3 interface
LOCAL pIHTMLElement AS DWORD ' // Reference to the IHTMLElement interface
' dim result as long
dim vParam(5) as variant
dim vValue as variant
dim success as long
' Create a new instance of Internet Explorer
pIWebBrowser2 = COM_CreateObject("InternetExplorer.Application")
IF isFalse(pIWebBrowser2) THEN EXIT FUNCTION
' Make it visible
vParam(1) = "1"
success = COM_Succeeded(COM_SetProperty(pIWebBrowser2, "Visible", 1, vParam))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Navigate to Google
vParam(1) = "http://www.google.com/ncr"
success = COM_Succeeded(COM_CallMethod(pIWebBrowser2, "Navigate", 1, vParam, 0))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Wait until the page is ready
success = COM_Succeeded(COM_GetProperty(pIWebBrowser2, "ReadyState", vValue))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
WHILE (variant#(vValue) <> %READYSTATE_COMPLETE)
SLEEP 3
COM_GetProperty(pIWebBrowser2, "ReadyState", vValue)
WEND
success = COM_Succeeded(COM_GetProperty(pIWebBrowser2, "Document", vValue))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Get a reference to the IHTMLDocument3 interface
pIHTMLDocument3 = variant#(vValue)
IF isFalse(pIHTMLDocument3) THEN CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Get a reference to the input box
vParam(1) = "q"
success = COM_Succeeded(COM_CallMethod(pIHTMLDocument3, "getElementById", 1, vParam, vValue))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
pIHTMLElement = variant#(vValue)
if isFalse(pIHTMLElement) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Set the value
vParam(1) = "value"
vParam(2) = "ThinBasic"
vParam(3) = "0"
success = COM_Succeeded(COM_CallMethod(pIHTMLElement, "setAttribute", 3, vParam, 0))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Get reference to Search button
vParam(1) = "btnG"
success = COM_Succeeded(COM_CallMethod(pIHTMLDocument3, "getElementById", 1, vParam, vValue))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
COM_Release(pIHTMLElement) ' We had already reference for textbox here, need to release from memory
pIHTMLElement = variant#(vValue)
if isFalse(pIHTMLElement) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Click in
success = COM_Succeeded(COM_CallMethod(pIHTMLElement, "Click", 0, 0, 0))
if isFalse(success) then CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
' Do not forget to release objects
CleanUpAndEnd(pIWebBrowser2, pIHTMLDocument3, pIHTMLElement )
end function
sub CleanUpAndEnd( var1 as dword, optional var2 as dword, var3 as dword )
if var1 then COM_Release(var1)
if var2 then COM_Release(var2)
if var3 then COM_Release(var3)
stop
end sub
[/code]
Petr
UPDATED July 7th 2009, 18:54
Bookmarks