Results 1 to 8 of 8

Thread: COM Example: Internet Explorer Automation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    COM Example: Internet Explorer Automation

    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
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Internet Explorer Security Alert
    By matthew in forum Shout Box Area
    Replies: 3
    Last Post: 18-12-2008, 15:31

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •