Tuesday, July 28, 2009

How to work browser helper objects in visual basic. not c shap or c++?

there is one page i found that didn't work. it had a download link to a type lib that wasn't on the site anymore. please do not post this link or any links to bho tutorials in c sharp or c++. i only work with visual basic currently. I cannot find this answer anywhere. if you could write the code in here for the dllimports and marshalas statements i might be able to figure it out.. thanks..

How to work browser helper objects in visual basic. not c shap or c++?
Forget about that primitive marshalling and dllimports...


It is no wonder that some refer to BHO as standing for Browser Highjacking Object. We are flying high with .NET. So just sit back, and enjoy the ride!





Note: My apologies—in advance—for how the Answer's Editor truncates expressions that have dot syntax or are enclosed in parentheses. But, I am substantially certain that you are savvy enough to know how to fill in the gaps.





Description of the Technique





The WebBrowser control is an easy control to work with in Visual Studio .NET or earlier versions of Microsoft Visual Studio. However, you may find it more difficult to handle the events of a WebBrowser control in Visual Studio .NET.





The following event interface exposes most of the document events that you handle in your application: Mshtml.HTMLDocumentEvents2_Event





To handle the event, you must create your own Sub procedure or function that you can call when the event occurs. You must match the signature of the event that fires. For example, the following Sub procedure handles the MouseOver event of the document: Private Sub document_onmouseover(ByVal e As mshtml.IHTMLEventObj)





After the event handler is in place, you must hook the event. You can hook an event any time after the DocumentComplete event on the WebBrowser control fires. Here is the syntax of the hook: AddHandler CType(document, _


mshtml.HTMLDocumentEvents2_Event).onmo... _


AddressOf Me.document_onmouseover





This code calls the AddHandler statement and uses the CType function to pass the event. The CType function casts the document object to the appropriate type (mshtml.HTMLDocumentEvents2_Event), and then the OnMouseOver event is passed from that. The Me.document_onmouseoverSub procedure is passed to the second parameter, the AddressOf statement, which provides the address of your handler.








Create the Project and Add Code





In the following sample, the WebBrowser control browses to http://www.microsoft.com. After the page loads, the sample hooks the OnMouseOver and the OnClick events. The sample then adds text to a list box whenever the events fire. (How cool is that?)





1. Start Visual Studio .NET.


2. Create a new Windows Application project in Visual Basic .NET


3. Add a reference to Microsoft.mshtml in the project.


4. In the toolbox, click General.


5. Right-click the open panel, and then click Customize Toolbox.


6. Select the Microsoft Web Browser check box, and then click OK.


7. In the toolbox, double-click Explorer.


8. In the toolbox, click Windows Forms, and then double-click the ListBox control.


9. Arrange the controls so that they are easy to view on the form.


10. Add the following code to the top of AssemblyInfo.vb, replace the existing Import sentences Imports System


Imports System.Drawing


Imports System.Collections


Imports System.ComponentModel


Imports System.Windows.Forms


Imports System.Data


Imports System.Reflection


Imports System.Runtime.InteropServices


Imports System.Threading


Imports System.Diagnostics








11. Add the following code between Windows Form Designer generated code and End Class in the code view of Form1.vb Public Shared dwCookie2 As Integer


Public Sub add_list(ByVal a As Object)


ListBox1.Items.Insert(0, a)


End Sub





Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


AxWebBrowser1.Navigate2("http://www.micr...


Dim x As IEEvents = New IEEvents(AxWebBrowser1)


x.fm = Me


End Sub





Private Sub AxWebBrowser1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxWebBrowser1.Enter


End Sub





Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCom... Handles AxWebBrowser1.DocumentComplete


Dim doc As mshtml.HTMLDocument


doc = AxWebBrowser1.Document





AddHandler CType(doc, _


mshtml.HTMLDocumentEvents2_Event).onclic... AddressOf Document_onclick


AddHandler CType(doc, _


mshtml.HTMLDocumentEvents2_Event).onmous... AddressOf Document_onmouseover


End Sub





Private Sub AxWebBrowser1_BeforeNavigate2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_BeforeNavig... Handles AxWebBrowser1.BeforeNavigate2


Dim doc As mshtml.HTMLDocument


doc = AxWebBrowser1.Document





RemoveHandler CType(doc, _


mshtml.HTMLDocumentEvents2_Event).onclic... _


AddressOf Document_onclick


RemoveHandler CType(doc, _


mshtml.HTMLDocumentEvents2_Event).onmous... _


AddressOf Document_onmouseover


End Sub





Private Sub Document_onmouseover(ByVal e As mshtml.IHTMLEventObj)


ListBox1.Items.Insert(0, "onMouseOver: " %26amp; _


e.srcElement.tagName.ToString())


End Sub





Private Function Document_onclick(ByVal e As mshtml.IHTMLEventObj) _


As Boolean


ListBox1.Items.Insert(0, "onClick: " %26amp; _


e.srcElement.tagName.ToString())


Return True


End Function








12. Add the following code after End ClassPublic Class IEHTMLDocumentEvents


Implements mshtml.HTMLDocumentEvents2


Public Sub onactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onactivate


End Sub





Public Sub onafterupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onafterupdate


End Sub





Public Function onbeforeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforeactiv...


Return True


End Function





Public Function onbeforedeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforedeact...


Return True


End Function





Public Sub onbeforeeditfocus(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onbeforeeditf...


End Sub





Public Function onbeforeupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforeupdat...


Return True


End Function





Public Sub oncellchange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.oncellchange


End Sub





Public Function onclick(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onclick


Return True


End Function





Public Function oncontextmenu(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.oncontextmenu


Return True


End Function





Public Function oncontrolselect(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.oncontrolsele...


Return True


End Function





Public Sub ondataavailable(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondataavailab...


End Sub





Public Sub ondatasetchanged(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondatasetchan...


End Sub





Public Sub ondatasetcomplete(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondatasetcomp...


End Sub





Public Function ondblclick(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.ondblclick


Return True


End Function





Public Sub ondeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondeactivate


End Sub





Public Function ondragstart(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.ondragstart


Return True


End Function





Public Function onerrorupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onerrorupdate


Return True


End Function





Public Sub onfocusin(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onfocusin


End Sub





Public Sub onfocusout(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onfocusout


End Sub





Public Function onhelp(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onhelp


Return True


End Function





Public Sub onkeydown(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onkeydown


End Sub





Public Function onkeypress(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onkeypress


Return True


End Function





Public Sub onkeyup(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onkeyup


End Sub





Public Sub onmousedown(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmousedown


End Sub





Public Sub onmousemove(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmousemove


End Sub





Public Sub onmouseout(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseout


End Sub





Public Sub onmouseover(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseover


End Sub





Public Sub onmouseup(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseup


End Sub





Public Function onmousewheel(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onmousewheel


Return True


End Function





Public Sub onpropertychange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onpropertycha...


End Sub





Public Sub onreadystatechange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onreadystatec...


End Sub





Public Sub onrowenter(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowenter


End Sub





Public Function onrowexit(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onrowexit


Return True


End Function





Public Sub onrowsdelete(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowsdelete


End Sub





Public Sub onrowsinserted(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowsinserte...


End Sub





Public Sub onselectionchange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onselectionch...


End Sub





Public Function onselectstart(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onselectstart


Return True


End Function





Public Function onstop(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onstop


Return True


End Function


End Class





Public Class IEEvents


Implements SHDocVw.DWebBrowserEvents2


Public fm As Form1


Private icp As System.Runtime.InteropServices.UCOMIConn...


Private cookie As Integer = -1


Private m_ie As AxSHDocVw.AxWebBrowser





Public Sub New(ByRef ie As AxSHDocVw.AxWebBrowser)


' Call QueryInterface for IConnectionPointContainer


m_ie = ie


Dim icpc As System.Runtime.InteropServices.UCOMIConn... = CType(ie.GetOcx(), System.Runtime.InteropServices.UCOMIConn...


' Find the connection point for the


' DWebBrowserEvents2 source interface


Dim g As Guid = GetType(SHDocVw.DWebBrowserEvents2).GUID


icpc.FindConnectionPoint(g, icp)





'Pass a pointer to the host to the connection point


icp.Advise(Me, cookie)





' Show the browser


ie.Visible = True


Dim oURL As Object = "http://www.microsoft.com"


Dim oEmpty As Object = ""


ie.Navigate2(oURL, oEmpty, oEmpty, oEmpty, oEmpty)


End Sub





Public Sub BeforeNavigate2(ByVal pDisp As Object, ByRef URL As Object, ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.BeforeNavigat...


End Sub





Public Sub ClientToHostWindow(ByRef CX As Integer, ByRef CY As Integer) Implements SHDocVw.DWebBrowserEvents2.ClientToHostW...


End Sub





Public Sub CommandStateChange(ByVal Command As Integer, ByVal Enable As Boolean) Implements SHDocVw.DWebBrowserEvents2.CommandStateC...


End Sub





Public Sub DocumentComplete(ByVal pDisp As Object, ByRef URL As Object) Implements SHDocVw.DWebBrowserEvents2.DocumentCompl...


Dim doc As mshtml.HTMLDocument = CType(CType(pDisp, SHDocVw.IWebBrowser2).Document, mshtml.HTMLDocument)


Dim pConPtCon As System.Runtime.InteropServices.UCOMIConn... = CType(doc, System.Runtime.InteropServices.UCOMIConn...


Dim guid As Guid = System.Type.GetType("mshtml.HTMLDocument...


Dim pConPt As System.Runtime.InteropServices.UCOMIConn...


pConPtCon.FindConnectionPoint(guid, pConPt)


Dim d As IEHTMLDocumentEvents = New IEHTMLDocumentEvents


pConPt.Advise(d, Form1.dwCookie2)





Dim iEvent As mshtml.HTMLDocumentEvents2_Event


iEvent = CType(doc, mshtml.HTMLDocumentEvents2_Event)


AddHandler iEvent.onclick, AddressOf ClickEventHandler


AddHandler iEvent.onmouseover, AddressOf MouseOverEventHandler


End Sub





Private Function ClickEventHandler(ByVal e As mshtml.IHTMLEventObj) As Boolean


fm.add_list(e.type + ":" + e.srcElement.tagName)


Return True


End Function





Private Sub MouseOverEventHandler(ByVal e As mshtml.IHTMLEventObj)


fm.add_list(e.type + ":" + e.srcElement.tagName)


End Sub





Public Sub DownloadBegin() Implements SHDocVw.DWebBrowserEvents2.DownloadBegin


End Sub





Public Sub DownloadComplete() Implements SHDocVw.DWebBrowserEvents2.DownloadCompl...


End Sub





Public Sub FileDownload(ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.FileDownload


End Sub





Public Sub NavigateComplete2(ByVal pDisp As Object, ByRef URL As Object) Implements SHDocVw.DWebBrowserEvents2.NavigateCompl...


End Sub





Public Sub NavigateError(ByVal pDisp As Object, ByRef URL As Object, ByRef Frame As Object, ByRef StatusCode As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.NavigateError


End Sub





Public Sub NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.NewWindow2


End Sub





Public Sub OnFullScreen(ByVal FullScreen As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnFullScreen


End Sub





Public Sub OnMenuBar(ByVal MenuBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnMenuBar


End Sub





Public Sub OnQuit() Implements SHDocVw.DWebBrowserEvents2.OnQuit


End Sub





Public Sub OnStatusBar(ByVal StatusBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnStatusBar


End Sub





Public Sub OnTheaterMode(ByVal TheaterMode As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnTheaterMode


End Sub





Public Sub OnToolBar(ByVal ToolBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnToolBar


End Sub





Public Sub OnVisible(ByVal Visible As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnVisible


End Sub





Public Sub PrintTemplateInstantiation(ByVal pDisp As Object) Implements SHDocVw.DWebBrowserEvents2.PrintTemplate...


End Sub





Public Sub PrintTemplateTeardown(ByVal pDisp As Object) Implements SHDocVw.DWebBrowserEvents2.PrintTemplate...


End Sub





Public Sub PrivacyImpactedStateChange(ByVal bImpacted As Boolean) Implements SHDocVw.DWebBrowserEvents2.PrivacyImpact...


End Sub





Public Sub ProgressChange(ByVal Progress As Integer, ByVal ProgressMax As Integer) Implements SHDocVw.DWebBrowserEvents2.ProgressChang...


End Sub





Public Sub PropertyChange(ByVal szProperty As String) Implements SHDocVw.DWebBrowserEvents2.PropertyChang...


End Sub





Public Sub SetSecureLockIcon(ByVal SecureLockIcon As Integer) Implements SHDocVw.DWebBrowserEvents2.SetSecureLock...


End Sub





Public Sub StatusTextChange(ByVal Text As String) Implements SHDocVw.DWebBrowserEvents2.StatusTextCha...


End Sub





Public Sub TitleChange(ByVal Text As String) Implements SHDocVw.DWebBrowserEvents2.TitleChange


End Sub





Public Sub UpdatePageStatus(ByVal pDisp As Object, ByRef nPage As Object, ByRef fDone As Object) Implements SHDocVw.DWebBrowserEvents2.UpdatePageSta...


End Sub





Public Sub WindowClosing(ByVal IsChildWindow As Boolean, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.WindowClosing


End Sub





Public Sub WindowSetHeight(ByVal Height As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetHeig...


End Sub





Public Sub WindowSetLeft(ByVal Left As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetLeft


End Sub





Public Sub WindowSetResizable(ByVal Resizable As Boolean) Implements SHDocVw.DWebBrowserEvents2.WindowSetResi...


End Sub





Public Sub WindowSetTop(ByVal Top As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetTop


End Sub





Public Sub WindowSetWidth(ByVal Width As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetWidt...


End Sub


End Class





---





Additional Notes








• This process is the same if you automate Internet Explorer. Replace AxWebBrowser1 with your local variable name for Internet Explorer.


• This sample does not consider framesets. When you navigate to a frameset, you may not see any events in your application. If necessary in your application, you must add code to handle the frameset possibility.





_____________________
Reply:what version of VB? VB.NET (any framework), or previous? Without knowing that, I can't help you. Check out http://www.pscode.com for great source code samples.

deliver flowers

No comments:

Post a Comment