Jump to content
Sign in to follow this  
nightwatcher

Guiding me with object event

Recommended Posts

nightwatcher

I'm trying to use the COM interface to create an object .

Here is the reference url:https://msdn.microsoft.com/zh-tw/library/windows/desktop/aa384106.aspx

I failed to use the event ,actually I have no idea about how to.

 

I take the example in objectevent. apparently I do not get the key point.

here is my  code.

 

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Global $oHttp = ObjCreate('WinHttp.WinHttpRequest.5.1')

Global $ohttpevent= ObjEvent($oHttp, 'IWinEvents_','IWinHttpRequestEvents')

$oHttp.SetTimeouts(60000, 60000, 60000, 5000);

$oHttp.open('GET', $reqauth, False)

$oHttp.send()

$oHttp.waitforresponse()

$sBody = BinaryToString($oHttp.responsebody(), 4)
 ConsoleWrite('$sBody1=' & $sBody & @CRLF)

 

Func IWinEvents_OnError($lerrnum,$wserrdes)
    ConsoleWrite('Error! error code='&Hex($lerrnum)&@TAB&'description='&$wserrdes)
EndFunc

Func IWinEvents_OnResponseDataAvailable($punchar)
    ConsoleWrite('stringlen='&StringLen($punchar)&@TAB&'content='&$punchar&@CRLF)
EndFunc

Func IWinEvents_OnResponseFinished()
    ConsoleWrite('ResponseFinished!'&@CRLF)
EndFunc

Func IWinEvents_OnResponseStart($long,$wscontent)
    ConsoleWrite('status='&$long&@TAB&'ContentType='&$wscontent&@CRLF)
EndFunc

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite("err.number is: " & @TAB &  Hex($oError.number, 8)& @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

Share this post


Link to post
Share on other sites
TheXman

What is your goal?  Are you trying to figure out how to send an http request using COM or are you trying to learn how to implement COM event handlers?  If it is how to send an http request, then there are numerous posts on that subject.  Just do a search on "winhttp.winhttprequest.5.1".  If you are trying to learn how to implement COM event handlers, start with the example in the help file for the ObjEvent function.

Here's a very minimal example of sending an http request using COM (with minimal error handling):

http_get_request_using_com()

Func http_get_request_using_com()
    Local $oHttp, $oComError

    $oComError = ObjEvent ("AutoIt.Error", "com_error_handler")
    $oHttp     = ObjCreate("winhttp.winhttprequest.5.1")
    $oHttp.Open("GET", "https://www.google.com", False)
    $oHttp.Send()
    If @error Then Exit @error

    ConsoleWrite("HTTP Status  : " & $oHttp.Status & @CRLF)
    ConsoleWrite("HTTP Response: " & StringLeft($oHttp.ResponseText, 50) & @CRLF) ; First 50 bytes of response
EndFunc

Func com_error_handler($oError)
    ConsoleWrite("Error Desc: " & $oError.Description & @CRLF)
    Return
EndFunc

 


Whoever said that there's no such thing as a dumb question, probably asked a lot of DUMB questions.  Dumb questions are ones that you can easily find the answer to on your own.

Share this post


Link to post
Share on other sites
nightwatcher
8 hours ago, TheXman said:

What is your goal?  Are you trying to figure out how to send an http request using COM or are you trying to learn how to implement COM event handlers?  If it is how to send an http request, then there are numerous posts on that subject.  Just do a search on "winhttp.winhttprequest.5.1".  If you are trying to learn how to implement COM event handlers, start with the example in the help file for the ObjEvent function.

Here's a very minimal example of sending an http request using COM (with minimal error handling):

http_get_request_using_com()

Func http_get_request_using_com()
    Local $oHttp, $oComError

    $oComError = ObjEvent ("AutoIt.Error", "com_error_handler")
    $oHttp     = ObjCreate("winhttp.winhttprequest.5.1")
    $oHttp.Open("GET", "https://www.google.com", False)
    $oHttp.Send()
    If @error Then Exit @error

    ConsoleWrite("HTTP Status  : " & $oHttp.Status & @CRLF)
    ConsoleWrite("HTTP Response: " & StringLeft($oHttp.ResponseText, 50) & @CRLF) ; First 50 bytes of response
EndFunc

Func com_error_handler($oError)
    ConsoleWrite("Error Desc: " & $oError.Description & @CRLF)
    Return
EndFunc

 

I am trying  to  implement COM event handlers……

I want to kown why I failed to use the event  of winhttprequest

I have tried  to imitate the  example of  objevent as much as possible

Share this post


Link to post
Share on other sites
TheXman
Posted (edited)

It failed because according to the help file for COM Events, AutoIt can only receive 'dispatch' type events.  The WinHttp.WinHttpRequest.5.1 events are not 'dispatch' type events. They don't have an IDispatch interface they have an interface of IUknown.

image.png.e1c7c50ca82161ae11d4253a74bc9ac3.png

 

Edited by TheXman
Removed duplicate image

Whoever said that there's no such thing as a dumb question, probably asked a lot of DUMB questions.  Dumb questions are ones that you can easily find the answer to on your own.

Share this post


Link to post
Share on other sites
nightwatcher
5 hours ago, TheXman said:

It failed because according to the help file for COM Events, AutoIt can only receive 'dispatch' type events.  The WinHttp.WinHttpRequest.5.1 events are not 'dispatch' type events. They don't have an IDispatch interface they have an interface of IUknown.

image.png.e1c7c50ca82161ae11d4253a74bc9ac3.png

 

uh, thks so much . this has confused me a long time …… I do not kown the  limitation of the COM interface in Autoit.

thks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • cetipabo
      By cetipabo
      Hello,
      i'm trying to use this UDF _IEquerySelectorAll() to check if i clicked a button in a web page.
      here is the index.html:
      and this is my autoit script including the _IEquerySelectorAll() function (modified by mLipok) :

      well, so i'm supposed to see a msgbox when i click the button, but nothing happens...
      Can someone help me please ? something should be wrong here:
       
      $oDoc = _IEDocGetObj($oIE) $oButton = _IEquerySelectorAll($oDoc,'button', 0) ObjEvent($oButton, "_Evt_") Func _Evt_onClick() msgbox(0,"","button clicked") EndFunc  
    • Chimp
      By Chimp
      Hello and happy new Year to everybody!
      I'm trying to catch some events occuring within a browser control by simply using the ObjEvent() function.
      I think I'm not using that function in a proper way since some events are captured, while other are not.
      more precisely, events like those listed in this page are working:
      https://msdn.microsoft.com/en-us/library/aa769764(v=vs.85).aspx
      while other events listed in other "interfaces", as in the following links, does not works.
      https://msdn.microsoft.com/en-us/library/hh801967(v=vs.85).aspx
      For exemple, Here I would like to catch events like "DragOver" and/or "Drop" fired while dragging the image on the web control, but I'm not been able to obtain a result.
      Those events are listed in the HTMLDocumentEvents4 interface or also in the HTMLImgEvents interface, but I failed to use them.
      Any hint that can help to see what I'm doing wrong, or even better that can show how to achieve the result by using the ObjEvent() function (if it's possible?) is welcome.
      Thanks a lot.
      Here is the simple script that I'm using for my tests:
      #include <GUIConstantsEx.au3> #include <string.au3> ; read html page from bottom of this script ; and write it to a file on disk for later ; usage by the $oIE.navigate CreateHtmlPage() Example() Exit Func Example() Local $hGUIMain = GUICreate("Event Test", 540, 400) ; We generate the Browser Control... Global $oIE = ObjCreate("Shell.Explorer.2") ; and we embed it into the AutoIt GUI $hIE = GUICtrlCreateObj($oIE, 5, 5, 530, 390) GUISetState() ;Show GUI ; load in the browser our previously created web page $oIE.navigate('file:///' & @ScriptDir & '\Page.html') Do ; wait for document Sleep(250) $oDocument = $oIE.document Until IsObj($oDocument) $oDocument.execCommand("Refresh") ; --- Setup catch of events --- Local $oEventObjects[2] $oImage = $oDocument.getElementById('drag1') ; Object reference to The image on the page ; https://msdn.microsoft.com/en-us/library/aa769764(v=vs.85).aspx ; HTMLDocumentEvents2 interface (catch OnClick, OnMouseOver, .... etc ; ObjEvent() "Handles incoming events from the given Object." ; ObjEvent($oParam1, $sParam2 [,$sParam3]) ; Parameters ; $oParam1 A variable containing an Object from which you want to receive events ; ; $sParam2 The prefix of the functions you define to handle receiving events. ; The prefix is appended by the Objects method name. ; ; $sParam3 "interface name" [optional] name of an Event interface to use. ; Note: It must be a supported as outgoing for the Object AND it must be of type DISPATCH. ; Using third parameter as "HTMLDocumentEvents2" as event interface, or even leaving it blank, ; is the only way by which I've been able to catch some events from the browser control ; OK, events listed here are catched --> https://msdn.microsoft.com/en-us/library/aa769764(v=vs.85).aspx $oEventObjects[0] = ObjEvent($oDocument, "IEEvent2_") ;, "HTMLDocumentEvents2") ; when I try to catch events fired by the Image dragged on the web page ; following attempts do not work (no event is captured and passed to AutoIt) ; $oEventObjects[1] = ObjEvent($oDocument, "IEEvent2_", "HTMLDocumentEvents2") ; $oEventObjects[1] = ObjEvent($oDocument, "IEEvent2_", "HTMLDocumentEvents4") ; $oEventObjects[1] = ObjEvent($oImage, "IEEvent2_", "HTMLDocumentEvents2") ; $oEventObjects[1] = ObjEvent($oImage, "IEEvent2_") $oEventObjects[1] = ObjEvent($oImage, "IEEvent2_", "HTMLImgEvents2") ; ----------------------------- ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; the end For $i = 0 To UBound($oEventObjects) - 1 $oEventObjects[$i].Stop ; Tell IE we don't want to receive events. $oEventObjects[$i] = 0 Next $oEventObject = 0 ; Kill the Event Object $oIE = 0 ; Remove IE from memory (not really necessary). GUIDelete($hGUIMain) ; Remove GUI EndFunc ;==>Example ; below function should be fired by events ; occurred in the browser's objects ; --- events management zone --- Volatile Func IEEvent2_onClick($oEvent) ConsolePrint("mouse click:") EndFunc ;==>IEEvent2_onClick Volatile Func IEEvent2_onDblClick($oEvent) ConsolePrint("mouse DoubleClick:") EndFunc ;==>IEEvent2_onDblClick ; Drag related events Volatile Func IEEvent2_onDragstart($oEvent) ConsolePrint("Drag action started") EndFunc ;==>IEEvent2_onDragstart ; --- following events are not catched --- ??? Volatile Func IEEvent2_onDragOver($oEvent) ConsolePrint("DragOver event") EndFunc ;==>IEEvent2_onDragOver Volatile Func IEEvent2_onDrop($oEvent) ConsolePrint("Drop action performed ") EndFunc ;==>IEEvent2_onDrop ; ------------------------------ Func ConsolePrint($sMsg) ConsoleWrite($sMsg & @CRLF) EndFunc ;==>ConsolePrint Func CreateHtmlPage() Local $sStart = @LF & "#cs;HTML" Local $sEnd = "#ce;HTML" & @CR Local $aArray = _StringBetween(FileRead(@ScriptFullPath), $sStart, $sEnd) Local $sPage = @ScriptDir & '\Page.html' Local $hFile = FileOpen($sPage, 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents) FileWrite($hFile, $aArray[0]) FileFlush($hFile) FileClose($hFile) EndFunc ;==>CreateHtmlPage ; example got from here: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop2 #cs;HTML <!DOCTYPE HTML> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <style> #div1, #div2 { float: left; width: 214px; height: 214px; margin: 5px; padding: 10px; border: 1px solid black; } </style> <script> function allowDrop(ev) {ev.preventDefault();} function drag(ev) {ev.dataTransfer.setData("text", ev.target.id);} function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.target.appendChild(document.getElementById(data));} </script> </head> <body> <h2>Drag and Drop</h2> <p>Drag the image back and forth between the two div elements.</p> <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"> <img src="https://www.autoitscript.com/forum/uploads/monthly_2016_01/Chimp.jpg.688f81fa865450e2913b5dc2cb56215f.thumb.jpg.96a6bfa47c0fb8476f39aaf55ad68ed0.jpg" draggable="true" ondragstart="drag(event)" id="drag1" width="214" height="214"> </div> <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div> </body> </html> #ce;HTML ;  
    • crzymnmchl
      By crzymnmchl
      I have e.g.
      Local $oIEEvents = ObjEvent(_IEGetObjById($oIE, "mylink"), "_MyLink_", "HTMLAnchorEvents2") working perfectly, but
      Local $oIEEvents = ObjEvent(_IEGetObjById($oIE, "future_btn"), "_Futurebtn_", "HTMLButtonElementEvents ") does not as ExtJS has not dynamically created the html yet. Is there anything in the IE API that would allow a callback to $oIE once the html is available?
×