Jump to content

Recommended Posts

Posted

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

Posted

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

 

Posted
  On 4/26/2018 at 5:03 PM, 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

 

Expand  

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

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
Posted
  On 4/27/2018 at 4:40 AM, 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

 

Expand  

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!

  • 4 months later...
Posted

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...