Jump to content

Using ObjCreateInterface() and ObjectFromTag() Functions


Recommended Posts

17 minutes ago, LarsJ said:

"This feature is experimental". No it's not. A function that has been used for more than 9 years to implement advanced code like the following is no longer experimental but has long proven its worth:

+1

Signature beginning:
Please remember: "AutoIt".....  Wondering who uses AutoIt and what it can be used for ?
* GHAPI UDF - modest beginning - communication with GitHub REST API Forum Rules *
Include Dependency Tree (Tool for analyzing script relations)
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) *

PDF Related:How to get reference to PDF object embeded in IE *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2021-03-17

Link to post
Share on other sites

Responding events
In the first post, the ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler callback interface was implemented. According to the Microsoft documentation, this callback interface is implemented to be able get an object pointer to the the ICoreWebView2Environment through a DllCall() to either the CreateCoreWebView2Environment() or CreateCoreWebView2EnvironmentWithOptions() functions implemented in WebView2Loader.dll. ICoreWebView2Environment represents the WebView2 environment on the PC.

You pass the $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler pointer to one of the dll-functions. As a consequence of the function call, the dll-function generates a CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler event. You respond to this event by adding code in the callback function CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() (WebView2-1-4.au3)

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui

Global $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
       $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler
Global Const $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

; ICoreWebView2Environment interface
Global $oCoreWebView2Environment
Global Const $sIID_ICoreWebView2Environment = "{B96D755E-0319-4E92-A296-23436F46A1FC}"
Global Const $dtag_ICoreWebView2Environment = _
  "CreateCoreWebView2Controller hresult(hwnd;ptr);" & _
  "CreateWebResourceResponse hresult();" & _
  "get_BrowserVersionString hresult();" & _
  "add_NewBrowserVersionAvailable hresult();" & _
  "remove_NewBrowserVersionAvailable hresult();"

; Project includes
;#include "..\Includes\WV2Interfaces.au3"
#include "..\Includes\ObjectFromTag.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUI
  $hGui = GUICreate( "WebView2 Sample", 1200, 900, -1, -1, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, False, True ) ; $bPrint = True

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", "", _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUI
  GUISetState( @SW_SHOW )

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  DllClose( $hWebView2Loader )
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall()
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Environment object
  $oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
  ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

  ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
  ;$oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

SciTE console output:

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc
@error = 0

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()
IsObj( $oCoreWebView2Environment ) = 1

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()

CreateCoreWebView2EnvironmentWithOptions OK

According to the documentation for ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler interface the Invoke() method returns a pointer to the ICoreWebView2Environment interface as the last parameter. From this pointer the CoreWebView2Environment object can be created:

; Create CoreWebView2Environment object
$oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
;$oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

Data for the ICoreWebView2Environment interface is included at the top of WebView2-1-4.au3.

The most interesting method of the CoreWebView2Environment object is CreateCoreWebView2Controller(). The method takes our AutoIt GUI and a pointer to an ICoreWebView2CreateCoreWebView2ControllerCompletedHandler callback interface as input parameters and creates a WebView2 window embedded in the AutoIt GUI.
 

Create callback interface
Before we can execute the CreateCoreWebView2Controller() method, we need to create the ICoreWebView2CreateCoreWebView2ControllerCompletedHandler callback interface and its functions so that we can pass the object pointer to the method.

We create the callback interface and its functions by performing the 5 steps described in first post.
 

Step 1
The $dtag for the ICoreWebView2CreateCoreWebView2ControllerCompletedHandler callback interface can be created based on the information in WebView2.h (search the interface) and the Microsoft documentation (google the interface):

Global Const $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

 

Continue with steps 2 - 4 as described in first post.
 

Step 5
To test whether the interface functions are called as expected, we need to execute the CreateCoreWebView2Controller() method in the code above. The CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() function is called as a consequence of executing this method.

Check if the interface functions are called as expected (WebView2-1-5.au3)

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui

Global $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
       $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler
Global Const $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

Global $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
       $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler
Global Const $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

; ICoreWebView2Environment interface
Global $oCoreWebView2Environment
Global Const $sIID_ICoreWebView2Environment = "{B96D755E-0319-4E92-A296-23436F46A1FC}"
Global Const $dtag_ICoreWebView2Environment = _
  "CreateCoreWebView2Controller hresult(hwnd;ptr);" & _
  "CreateWebResourceResponse hresult();" & _
  "get_BrowserVersionString hresult();" & _
  "add_NewBrowserVersionAvailable hresult();" & _
  "remove_NewBrowserVersionAvailable hresult();"

; Project includes
;#include "..\Includes\WV2Interfaces.au3"
#include "..\Includes\ObjectFromTag.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUI
  $hGui = GUICreate( "WebView2 Sample", 1200, 900, -1, -1, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, False, True ) ; $bPrint = True

  $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler, False, True ) ; $bPrint = True

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", "", _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUI
  GUISetState( @SW_SHOW )

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  DllClose( $hWebView2Loader )
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall()
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Environment object
  $oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
  ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

  ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
  $oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc

SciTE console output:

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc
@error = 0

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()
IsObj( $oCoreWebView2Environment ) = 1

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()

CreateCoreWebView2EnvironmentWithOptions OK

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()

Output looks as expected. So far, there is no web page to view because we haven't opened one.
 

Responding events 2
As a consequence of executing $oCoreWebView2Environment.CreateCoreWebView2Controller() above, a CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler event is generated. You respond to this event by adding code in the callback function CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke(). Here we'll add code to open a web page (WebView2-1-6.au3)

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui

Global $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
       $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler
Global Const $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

Global $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
       $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler
Global Const $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler = _
  "Invoke hresult(hresult;ptr*);"

; ICoreWebView2Environment interface
Global $oCoreWebView2Environment
Global Const $sIID_ICoreWebView2Environment = "{B96D755E-0319-4E92-A296-23436F46A1FC}"
Global Const $dtag_ICoreWebView2Environment = _
  "CreateCoreWebView2Controller hresult(hwnd;ptr);" & _
  "CreateWebResourceResponse hresult();" & _
  "get_BrowserVersionString hresult();" & _
  "add_NewBrowserVersionAvailable hresult();" & _
  "remove_NewBrowserVersionAvailable hresult();"

Global $oCoreWebView2Controller
Global Const $sIID_ICoreWebView2Controller = "{4D00C0D1-9434-4EB6-8078-8697A560334F}"
Global Const $dtag_ICoreWebView2Controller = _
  "get_IsVisible hresult();" & _
  "put_IsVisible hresult();" & _
  "get_Bounds hresult();" & _
  ( @AutoItX64 ? "put_Bounds hresult(struct*);" :  "put_Bounds hresult(struct);" ) & _
  "get_ZoomFactor hresult();" & _
  "put_ZoomFactor hresult();" & _
  "add_ZoomFactorChanged hresult();" & _
  "remove_ZoomFactorChanged hresult();" & _
  "SetBoundsAndZoomFactor hresult();" & _
  "MoveFocus hresult();" & _
  "add_MoveFocusRequested hresult();" & _
  "remove_MoveFocusRequested hresult();" & _
  "add_GotFocus hresult();" & _
  "remove_GotFocus hresult();" & _
  "add_LostFocus hresult();" & _
  "remove_LostFocus hresult();" & _
  "add_AcceleratorKeyPressed hresult();" & _
  "remove_AcceleratorKeyPressed hresult();" & _
  "get_ParentWindow hresult();" & _
  "put_ParentWindow hresult();" & _
  "NotifyParentWindowPositionChanged hresult();" & _
  "Close hresult();" & _
  "get_CoreWebView2 hresult(ptr*);"

Global $oCoreWebView2, $pCoreWebView2
Global Const $sIID_ICoreWebView2 = "{76ECEACB-0462-4D94-AC83-423A6793775E}"
Global Const $dtag_ICoreWebView2 = _
  "get_Settings hresult(ptr*);" & _
  "get_Source hresult();" & _
  "Navigate hresult(wstr);" & _
  "NavigateToString hresult(wstr);" & _
  "add_NavigationStarting hresult(ptr;struct*);" & _
  "remove_NavigationStarting hresult();" & _
  "add_ContentLoading hresult();" & _
  "remove_ContentLoading hresult();" & _
  "add_SourceChanged hresult();" & _
  "remove_SourceChanged hresult();" & _
  "add_HistoryChanged hresult();" & _
  "remove_HistoryChanged hresult();" & _
  "add_NavigationCompleted hresult();" & _
  "remove_NavigationCompleted hresult();" & _
  "add_FrameNavigationStarting hresult();" & _
  "remove_FrameNavigationStarting hresult();" & _
  "add_FrameNavigationCompleted hresult();" & _
  "remove_FrameNavigationCompleted hresult();" & _
  "add_ScriptDialogOpening hresult();" & _
  "remove_ScriptDialogOpening hresult();" & _
  "add_PermissionRequested hresult();" & _
  "remove_PermissionRequested hresult();" & _
  "add_ProcessFailed hresult();" & _
  "remove_ProcessFailed hresult();" & _
  "AddScriptToExecuteOnDocumentCreated hresult(wstr;ptr);" & _
  "RemoveScriptToExecuteOnDocumentCreated hresult();" & _
  "ExecuteScript hresult(wstr;ptr);" & _
  "CapturePreview hresult();" & _
  "Reload hresult();" & _
  "PostWebMessageAsJson hresult();" & _
  "PostWebMessageAsString hresult();" & _
  "add_WebMessageReceived hresult(ptr;struct*);" & _
  "remove_WebMessageReceived hresult();" & _
  "CallDevToolsProtocolMethod hresult();" & _
  "get_BrowserProcessId hresult();" & _
  "get_CanGoBack hresult();" & _
  "get_CanGoForward hresult();" & _
  "GoBack hresult();" & _
  "GoForward hresult();" & _
  "GetDevToolsProtocolEventReceiver hresult();" & _
  "Stop hresult();" & _
  "add_NewWindowRequested hresult();" & _
  "remove_NewWindowRequested hresult();" & _
  "add_DocumentTitleChanged hresult();" & _
  "remove_DocumentTitleChanged hresult();" & _
  "get_DocumentTitle hresult();" & _
  "AddHostObjectToScript hresult();" & _
  "RemoveHostObjectFromScript hresult();" & _
  "OpenDevToolsWindow hresult();" & _
  "add_ContainsFullScreenElementChanged hresult();" & _
  "remove_ContainsFullScreenElementChanged hresult();" & _
  "get_ContainsFullScreenElement hresult();" & _
  "add_WebResourceRequested hresult();" & _
  "remove_WebResourceRequested hresult();" & _
  "AddWebResourceRequestedFilter hresult();" & _
  "RemoveWebResourceRequestedFilter hresult();" & _
  "add_WindowCloseRequested hresult();" & _
  "remove_WindowCloseRequested hresult();"

; Project includes
;#include "..\Includes\WV2Interfaces.au3"
#include "..\Includes\ObjectFromTag.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUI
  $hGui = GUICreate( "WebView2 Sample", 1200, 900, -1, -1, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, False, True ) ; $bPrint = True

  $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler, False, True ) ; $bPrint = True

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", "", _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUI
  GUISetState( @SW_SHOW )

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  DllClose( $hWebView2Loader )
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall()
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Environment object
  $oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
  ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

  ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
  $oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc

; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke" & @CRLF )

  ; Create CoreWebView2Controller object
  $oCoreWebView2Controller = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
  ConsoleWrite( "IsObj( $oCoreWebView2Controller ) = " & IsObj( $oCoreWebView2Controller ) & @CRLF )
  $oCoreWebView2Controller.AddRef() ; Prevent the object from being deleted when the function ends

  ; Set bounds for the CoreWebView2 object
  Local $tRect = _WinAPI_GetClientRect( $hGui )
  $oCoreWebView2Controller.put_Bounds( $tRect )

  ; Create CoreWebView2 object
  $oCoreWebView2Controller.get_CoreWebView2( $pCoreWebView2 )
  $oCoreWebView2 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
  ConsoleWrite( "IsObj( $oCoreWebView2 ) = " & IsObj( $oCoreWebView2 ) & @CRLF & @CRLF )

  ; Navigate to web page
  $oCoreWebView2.Navigate( "https://www.bing.com/" )

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

SciTE console output:

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface( $pSelf, $pRIID, $pObj ) ; Ret: long  Par: ptr;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_QueryInterface()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $pRIID, $pObj
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release( $pSelf ) ; Ret: dword
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()" & @CRLF & @CRLF )
  Return 1 ; For AddRef/Release
  #forceref $pSelf
EndFunc
@error = 0

Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF & @CRLF )
  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long, $ptr
EndFunc
@error = 0

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()
IsObj( $oCoreWebView2Environment ) = 1

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Release()

CreateCoreWebView2EnvironmentWithOptions OK

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_AddRef()

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke
IsObj( $oCoreWebView2Controller ) = 1
IsObj( $oCoreWebView2 ) = 1

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()

CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Release()

According to the documentation for ICoreWebView2CreateCoreWebView2ControllerCompletedHandler interface the Invoke() method returns a pointer to the ICoreWebView2Controller interface as the last parameter. From this pointer the CoreWebView2Controller object can be created:

; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke" & @CRLF )

  ; Create CoreWebView2Controller object
  $oCoreWebView2Controller = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
  ConsoleWrite( "IsObj( $oCoreWebView2Controller ) = " & IsObj( $oCoreWebView2Controller ) & @CRLF )
  $oCoreWebView2Controller.AddRef() ; Prevent the object from being deleted when the function ends

  ; Set bounds for the CoreWebView2 object
  Local $tRect = _WinAPI_GetClientRect( $hGui )
  $oCoreWebView2Controller.put_Bounds( $tRect )

  ; Create CoreWebView2 object
  $oCoreWebView2Controller.get_CoreWebView2( $pCoreWebView2 )
  $oCoreWebView2 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
  ConsoleWrite( "IsObj( $oCoreWebView2 ) = " & IsObj( $oCoreWebView2 ) & @CRLF & @CRLF )

  ; Navigate to web page
  $oCoreWebView2.Navigate( "https://www.bing.com/" )

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

With the CoreWebView2Controller object, we can set the position and size of the embedded WebView window to match the client rectangle of the AutoIt GUI, and we can create the CoreWebView2 object. With this object we can open a web page.


The 7z-file at bottom of first post has been updated.

Edited by LarsJ
Link to post
Share on other sites

UDF version
The definitions at top of WebView2-1-6.au3 are starting to fill up a lot. It's time to support the examples with a UDF: WV2Interfaces.au3.

WV2Interfaces.au3 contains all the interface definitions as well as the callback functions generated by ObjectFromTag(). These callback functions always seem to consist of 4 functions:

$sFunctionPrefix_QueryInterface()
$sFunctionPrefix_AddRef()
$sFunctionPrefix_Release()
$sFunctionPrefix_Invoke()

For the first 3 functions, the code appears to be constant. These functions can be moved directly into the UDF.

For the last function, Invoke(), the code varies depending on the functionality we want in the WebView window. Therefore, Invoke() is placed in the UDF as a function skeleton commented out. When we need to use the Invoke() function in our own code, we can start by copying the function skeleton, and add code as needed.

WebView2-1-7.au3 is similar to WebView2-1-a.au3 (WebView2-1-a.au3 is a copy of the WebView2-1.au3 example here) but is slightly updated:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui

; Project includes
#include "..\Includes\WV2Interfaces.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUI
  $hGui = GUICreate( "WebView2 Sample", 1200, 900, -1, -1, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerCreate( True )
  CoreWebView2CreateCoreWebView2ControllerCompletedHandlerCreate( True )

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", @ScriptDir, _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUI
  GUISetState( @SW_SHOW )

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE
        Local $tRect = _WinAPI_GetClientRect( $hGui )
        $oCoreWebView2Controller.put_Bounds( $tRect )

      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerDelete()
  CoreWebView2CreateCoreWebView2ControllerCompletedHandlerDelete()
  DllClose( $hWebView2Loader )
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall() above
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Environment object
  $oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
  ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

  ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
  $oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Controller object
  $oCoreWebView2Controller = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
  ConsoleWrite( "IsObj( $oCoreWebView2Controller ) = " & IsObj( $oCoreWebView2Controller ) & @CRLF )
  $oCoreWebView2Controller.AddRef() ; Prevent the object from being deleted when the function ends

  ; Set bounds for the CoreWebView2 object
  Local $tRect = _WinAPI_GetClientRect( $hGui )
  $oCoreWebView2Controller.put_Bounds( $tRect )

  ; Create CoreWebView2 object
  $oCoreWebView2Controller.get_CoreWebView2( $pCoreWebView2 )
  $oCoreWebView2 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
  ConsoleWrite( "IsObj( $oCoreWebView2 ) = " & IsObj( $oCoreWebView2 ) & @CRLF & @CRLF )

  ; Navigate to web page
  $oCoreWebView2.Navigate( "https://www.bing.com/" )

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

 

Two WebView windows
WebView2-1-8.au3 implements two WebView windows:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui_1, $hGui_2
Global $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1, $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1
Global $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1, $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1
Global $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2, $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2
Global $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2, $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2
Global $oCoreWebView2Environment_1, $oCoreWebView2Controller_1, $oCoreWebView2_1
Global $oCoreWebView2Environment_2, $oCoreWebView2Controller_2, $oCoreWebView2_2

; Project includes
#include "..\Includes\WV2Interfaces.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUIs
  $hGui_1 = GUICreate( "WebView2 Sample 1", 1200, 900,  50,  50, $WS_OVERLAPPEDWINDOW )
  $hGui_2 = GUICreate( "WebView2 Sample 2", 1200, 900, 100, 100, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1 = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1, False )
  $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1 = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1, False )

  $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2 = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2, False )
  $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2 = _
    ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                   $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                   $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2, False )

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", @ScriptDir, _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1 )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", @ScriptDir, _
                   "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2 )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUIs
  GUISetState( @SW_SHOW, $hGui_1 )
  GUISetState( @SW_SHOW, $hGui_2 )

  Local $aMsg, $tRect

  ; Loop
  While 1
    $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
      Case $hGui_1
        Switch $aMsg[0]
          Case $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE
            $tRect = _WinAPI_GetClientRect( $hGui_1 )
            $oCoreWebView2Controller_1.put_Bounds( $tRect )

          Case $GUI_EVENT_CLOSE
            GUIDelete( $hGui_1 )
            If Not $hGui_2 Then ExitLoop
            $hGui_1 = 0
        EndSwitch

      Case $hGui_2
        Switch $aMsg[0]
          Case $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE
            $tRect = _WinAPI_GetClientRect( $hGui_2 )
            $oCoreWebView2Controller_2.put_Bounds( $tRect )

          Case $GUI_EVENT_CLOSE
            GUIDelete( $hGui_2 )
            If Not $hGui_1 Then ExitLoop
            $hGui_2 = 0
        EndSwitch
    EndSwitch
  WEnd

  ; Cleanup
  DllClose( $hWebView2Loader )
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall() above
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  Switch $pSelf
    Case $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1 ; $hGui_1
      ; Create CoreWebView2Environment object
      $oCoreWebView2Environment_1 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
      ConsoleWrite( "IsObj( $oCoreWebView2Environment_1 ) = " & IsObj( $oCoreWebView2Environment_1 ) & @CRLF & @CRLF )

      ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
      $oCoreWebView2Environment_1.CreateCoreWebView2Controller( $hGui_1, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1 )
      ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

    Case $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2 ; $hGui_2
      ; Create CoreWebView2Environment object
      $oCoreWebView2Environment_2 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
      ConsoleWrite( "IsObj( $oCoreWebView2Environment_2 ) = " & IsObj( $oCoreWebView2Environment_2 ) & @CRLF & @CRLF )

      ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
      $oCoreWebView2Environment_2.CreateCoreWebView2Controller( $hGui_2, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2 )
      ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed
  EndSwitch

  Return 0 ; S_OK = 0x00000000
  #forceref $long
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF )

  Local $tRect, $pCoreWebView2

  Switch $pSelf
    Case $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1 ; $hGui_1
      ; Create CoreWebView2Controller object
      $oCoreWebView2Controller_1 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
      ConsoleWrite( "IsObj( $oCoreWebView2Controller_1 ) = " & IsObj( $oCoreWebView2Controller_1 ) & @CRLF )
      $oCoreWebView2Controller_1.AddRef() ; Prevent the object from being deleted when the function ends

      ; Set bounds for the CoreWebView2 object
      $tRect = _WinAPI_GetClientRect( $hGui_1 )
      $oCoreWebView2Controller_1.put_Bounds( $tRect )

      ; Create CoreWebView2 object
      $oCoreWebView2Controller_1.get_CoreWebView2( $pCoreWebView2 )
      $oCoreWebView2_1 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
      ConsoleWrite( "IsObj( $oCoreWebView2_1 ) = " & IsObj( $oCoreWebView2_1 ) & @CRLF & @CRLF )

      ; Navigate to web page
      $oCoreWebView2_1.Navigate( "https://www.bing.com/" )

    Case $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2 ; $hGui_2
      ; Create CoreWebView2Controller object
      $oCoreWebView2Controller_2 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
      ConsoleWrite( "IsObj( $oCoreWebView2Controller_2 ) = " & IsObj( $oCoreWebView2Controller_2 ) & @CRLF )
      $oCoreWebView2Controller_2.AddRef() ; Prevent the object from being deleted when the function ends

      ; Set bounds for the CoreWebView2 object
      $tRect = _WinAPI_GetClientRect( $hGui_2 )
      $oCoreWebView2Controller_2.put_Bounds( $tRect )

      ; Create CoreWebView2 object
      $oCoreWebView2Controller_2.get_CoreWebView2( $pCoreWebView2 )
      $oCoreWebView2_2 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
      ConsoleWrite( "IsObj( $oCoreWebView2_2 ) = " & IsObj( $oCoreWebView2_2 ) & @CRLF & @CRLF )

      ; Navigate to web page
      $oCoreWebView2_2.Navigate( "file:///" & @ScriptDir & "/index.html" )
  EndSwitch

  Return 0 ; S_OK = 0x00000000
  #forceref $long
EndFunc

For the two callback interfaces, the same $sFunctionPrefix values are used, so that the same set of four callback functions can be used in both windows:

; Create WebView2 GUIs
$hGui_1 = GUICreate( "WebView2 Sample 1", 1200, 900,  50,  50, $WS_OVERLAPPEDWINDOW )
$hGui_2 = GUICreate( "WebView2 Sample 2", 1200, 900, 100, 100, $WS_OVERLAPPEDWINDOW )

; Initialize COM
_WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

; Create callback interfaces and functions
$pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1 = _
  ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                 $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                 $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1, False )
$pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1 = _
  ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                 $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                 $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1, False )

$pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2 = _
  ObjectFromTag( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_", _
                 $dtag_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, _
                 $tCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2, False )
$pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2 = _
  ObjectFromTag( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_", _
                 $dtag_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, _
                 $tCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2, False )

In the Invoke() functions you can distinguish between the two WebView windows through the object pointers (corresponding to the value of $pSelf) in this way:

; Copied from WV2Interfaces.au3
; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall() above
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  Switch $pSelf
    Case $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_1 ; $hGui_1
      ; Create CoreWebView2Environment object
      $oCoreWebView2Environment_1 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
      ConsoleWrite( "IsObj( $oCoreWebView2Environment_1 ) = " & IsObj( $oCoreWebView2Environment_1 ) & @CRLF & @CRLF )

      ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
      $oCoreWebView2Environment_1.CreateCoreWebView2Controller( $hGui_1, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_1 )
      ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

    Case $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_2 ; $hGui_2
      ; Create CoreWebView2Environment object
      $oCoreWebView2Environment_2 = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
      ConsoleWrite( "IsObj( $oCoreWebView2Environment_2 ) = " & IsObj( $oCoreWebView2Environment_2 ) & @CRLF & @CRLF )

      ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
      $oCoreWebView2Environment_2.CreateCoreWebView2Controller( $hGui_2, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler_2 )
      ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed
  EndSwitch

  Return 0 ; S_OK = 0x00000000
  #forceref $long
EndFunc

 

DllCall()
In the two examples in this post, @ScriptDir is added as a parameter in the DllCall() function to avoid this error: CreateCoreWebView2EnvironmentWithOptions ERR. You can add the @ScriptDir parameter in the DllCall() function in the previous examples if you wish.


The 7z-file at bottom of first post has been updated.

Link to post
Share on other sites

Creating a WebView window through pseudo events
The CreateCoreWebView2EnvironmentWithOptions() function (DllCall) takes a pointer to an ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler callback interface as an input parameter. See e.g. WebView2-1-7.au3. As a consequence of the DllCall, the callback function CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() is executed.

In the documentation for the Invoke() method you can read: Called to provide the implementer with the completion status and result of the corresponding asynchronous method call. 

The Invoke() method returns an ICoreWebView2Environment interface.


The $oCoreWebView2Environment.CreateCoreWebView2Controller() method takes a pointer to an ICoreWebView2CreateCoreWebView2ControllerCompletedHandler callback interface as an input parameter. As a consequence of the method call, the callback function CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() is executed.

In the documentation for the Invoke() method you can read: Called to provide the implementer with the completion status and result of the corresponding asynchronous method call. 

The Invoke() method returns an ICoreWebView2Controller interface.


The creation of the ICoreWebView2Environment and ICoreWebView2Controller interfaces required to create a WebView window appears to be implemented as a kind of pseudo-events to handle code running asynchronously.

Creating a WebView window is about creating two callback interfaces and executing code in a DllCall function and a method function to call the corresponding Invoke() methods.
 

Real events in a WebView window
An example of real events (due to user actions that may occur multiple times) in a WebView window is NavigationStarting events as coded in WebView2-2-0.au3 (similar to WebView2-2-a.au3 but slightly updated (WebView2-2-a.au3 is a copy of the WebView2-2.au3 example here))

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt( "MustDeclareVars", 1 )

#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui

; Project includes
#include "..\Includes\WV2Interfaces.au3"

WebView2()

Func WebView2()
  ; Create WebView2 GUI
  $hGui = GUICreate( "WebView2 Sample", 1200, 900, -1, -1, $WS_OVERLAPPEDWINDOW )

  ; Initialize COM
  _WinAPI_CoInitialize( $COINIT_APARTMENTTHREADED )

  ; Create callback interfaces and functions
  CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerCreate( True ) ; Create WebView window
  CoreWebView2CreateCoreWebView2ControllerCompletedHandlerCreate( True )  ; Create WebView window
  CoreWebView2NavigationStartingEventHandlerCreate( True )                ; Create event handler

  ; DllCall CreateCoreWebView2EnvironmentWithOptions
  Local $hWebView2Loader = DllOpen( @AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll" )
  Local $aRet = DllCall( $hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", @ScriptDir, _
                         "ptr", NULL, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed
  If @error Or $aRet[0] Then Return ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF )
  ConsoleWrite( "CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF )

  ; Show WebView2 GUI
  GUISetState( @SW_SHOW )

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE
        Local $tRect = _WinAPI_GetClientRect( $hGui )
        $oCoreWebView2Controller.put_Bounds( $tRect )

      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerDelete()
  CoreWebView2CreateCoreWebView2ControllerCompletedHandlerDelete()
  DllClose( $hWebView2Loader )
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of the CreateCoreWebView2EnvironmentWithOptions DllCall() above
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Environment object
  $oCoreWebView2Environment = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment )
  ConsoleWrite( "IsObj( $oCoreWebView2Environment ) = " & IsObj( $oCoreWebView2Environment ) & @CRLF & @CRLF )

  ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
  $oCoreWebView2Environment.CreateCoreWebView2Controller( $hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler )
  ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke( $pSelf, $long, $ptr ) ; Ret: long  Par: long;ptr*
  ConsoleWrite( "CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2Controller object
  $oCoreWebView2Controller = ObjCreateInterface( $ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller )
  ConsoleWrite( "IsObj( $oCoreWebView2Controller ) = " & IsObj( $oCoreWebView2Controller ) & @CRLF )
  $oCoreWebView2Controller.AddRef() ; Prevent the object from being deleted when the function ends

  ; Set bounds for the CoreWebView2 object
  Local $tRect = _WinAPI_GetClientRect( $hGui )
  $oCoreWebView2Controller.put_Bounds( $tRect )

  ; Create CoreWebView2 object
  $oCoreWebView2Controller.get_CoreWebView2( $pCoreWebView2 )
  $oCoreWebView2 = ObjCreateInterface( $pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2 )
  ConsoleWrite( "IsObj( $oCoreWebView2 ) = " & IsObj( $oCoreWebView2 ) & @CRLF & @CRLF )

  ; Add NavigationStarting event handler
  Local $tEventRegistrationToken = DllStructCreate( "uint64" )
  $oCoreWebView2.add_NavigationStarting( $pCoreWebView2NavigationStartingEventHandler, $tEventRegistrationToken )
  ConsoleWrite( "DllStructGetData( $tEventRegistrationToken, 1 ) = " & DllStructGetData( $tEventRegistrationToken, 1 ) & @CRLF & @CRLF )
  ; Forces CoreWebView2NavigationStartingEventHandler_Invoke() below to be executed

  ; Navigate to web page
  $oCoreWebView2.Navigate( "https://www.bing.com/" )

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $long
EndFunc

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2.add_NavigationStarting() above
Func CoreWebView2NavigationStartingEventHandler_Invoke( $pSelf, $ptr1, $ptr2 ) ; Ret: long  Par: ptr*;ptr*
  ConsoleWrite( "CoreWebView2NavigationStartingEventHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2NavigationStartingEventArgs object
  $oCoreWebView2NavigationStartingEventArgs = ObjCreateInterface( $ptr2, $sIID_ICoreWebView2NavigationStartingEventArgs, $dtag_ICoreWebView2NavigationStartingEventArgs )
  ConsoleWrite( "IsObj( $oCoreWebView2NavigationStartingEventArgs ) = " & IsObj( $oCoreWebView2NavigationStartingEventArgs ) & @CRLF & @CRLF )
  $oCoreWebView2NavigationStartingEventArgs.AddRef() ; Prevent the object from being deleted when the function ends

  ; Get navigation information
  Local $bIsUserInitiated, $bIsRedirected
  $oCoreWebView2NavigationStartingEventArgs.get_IsUserInitiated( $bIsUserInitiated )
  $oCoreWebView2NavigationStartingEventArgs.get_IsRedirected( $bIsRedirected )

  ; Confirm navigation
  If $bIsUserInitiated And Not $bIsRedirected Then
    Local $sUri
    $oCoreWebView2NavigationStartingEventArgs.get_Uri( $sUri )
    If MsgBox( $MB_YESNO+$MB_ICONWARNING, "Navigation warning", "You are about to navigate to: " & $sUri & @CRLF & @CRLF & "Do you want to continue?" ) = $IDNO Then
      $oCoreWebView2NavigationStartingEventArgs.put_Cancel( True )
      $oCoreWebView2.NavigateToString( "<h1>Navigation Canceled</h1><p>You chose to cancel navigation to the following URL: " & $sUri & "</p>" )
    EndIf
  EndIf

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $ptr1
EndFunc

 

EventRegistrationToken
At top of the code, three callback interfaces are now created:

; Create callback interfaces and functions
CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerCreate( True ) ; Create WebView window
CoreWebView2CreateCoreWebView2ControllerCompletedHandlerCreate( True )  ; Create WebView window
CoreWebView2NavigationStartingEventHandlerCreate( True )                ; Create event handler

The NavigationStarting event handler is added in CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke()

; Add NavigationStarting event handler
Local $tEventRegistrationToken = DllStructCreate( "uint64" )
$oCoreWebView2.add_NavigationStarting( $pCoreWebView2NavigationStartingEventHandler, $tEventRegistrationToken )
ConsoleWrite( "DllStructGetData( $tEventRegistrationToken, 1 ) = " & DllStructGetData( $tEventRegistrationToken, 1 ) & @CRLF & @CRLF )
; Forces CoreWebView2NavigationStartingEventHandler_Invoke() below to be executed

Note the EventRegistrationToken used to add the event handler to the code. Such a token seems to be used in conjunction with most real event handlers.
 

EventHandler_Invoke()
An EventArgs object supports many real event handlers in obtaining information about the event:

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2.add_NavigationStarting() above
Func CoreWebView2NavigationStartingEventHandler_Invoke( $pSelf, $ptr1, $ptr2 ) ; Ret: long  Par: ptr*;ptr*
  ConsoleWrite( "CoreWebView2NavigationStartingEventHandler_Invoke()" & @CRLF )

  ; Create CoreWebView2NavigationStartingEventArgs object
  $oCoreWebView2NavigationStartingEventArgs = ObjCreateInterface( $ptr2, $sIID_ICoreWebView2NavigationStartingEventArgs, $dtag_ICoreWebView2NavigationStartingEventArgs )
  ConsoleWrite( "IsObj( $oCoreWebView2NavigationStartingEventArgs ) = " & IsObj( $oCoreWebView2NavigationStartingEventArgs ) & @CRLF & @CRLF )
  $oCoreWebView2NavigationStartingEventArgs.AddRef() ; Prevent the object from being deleted when the function ends

  ; Get navigation information
  Local $bIsUserInitiated, $bIsRedirected
  $oCoreWebView2NavigationStartingEventArgs.get_IsUserInitiated( $bIsUserInitiated )
  $oCoreWebView2NavigationStartingEventArgs.get_IsRedirected( $bIsRedirected )

  ; Confirm navigation
  If $bIsUserInitiated And Not $bIsRedirected Then
    Local $sUri
    $oCoreWebView2NavigationStartingEventArgs.get_Uri( $sUri )
    If MsgBox( $MB_YESNO+$MB_ICONWARNING, "Navigation warning", "You are about to navigate to: " & $sUri & @CRLF & @CRLF & "Do you want to continue?" ) = $IDNO Then
      $oCoreWebView2NavigationStartingEventArgs.put_Cancel( True )
      $oCoreWebView2.NavigateToString( "<h1>Navigation Canceled</h1><p>You chose to cancel navigation to the following URL: " & $sUri & "</p>" )
    EndIf
  EndIf

  Return 0 ; S_OK = 0x00000000
  #forceref $pSelf, $ptr1
EndFunc



The 7z-file at bottom of first post has been updated.

Link to post
Share on other sites

$oObject.AddRef()
In WebView2-2-0.au3 in the post above, a $oCoreWebView2NavigationStartingEventArgs.AddRef() code line is added after the object is created. This code line is required to prevent 0xC0000005 errors (STATUS_ACCESS_VIOLATION). It can be a little difficult to figure out when this line of code is needed. The easiest way is usually to run the code and see if the 0xC0000005 error occurs. In that case, try adding the code line and see if the error disappears.
 

Global variables
The Invoke() functions are defined and created based on description tags for callback interfaces and they are executed due to events. Therefore, the easiest way to pass data between these functions and the rest of the code is to use global variables. If there are many Invoke() functions, it can result in a fairly large number of global variables. There's not much to do about it. At least not in the program development phase. Once the program is complete, you may be able to group the variables into a single or a few global arrays.
 

WebView2 project
The WebView2 project has been used as a starting point for the examples above. If you are interested in a complete translation of the WebView2 API into AutoIt, keep an eye on the original project.

Link to post
Share on other sites
  • 1 month later...

Thank your for sharing this excellent UDF.
I'm sure many AutoIt-Users are looking at your work with a great expectaion.
I have run your examples and they all run as expected.
Here I have a couple of questions.

My first question is: 
  how do I get the event id when I interface with Edge, clicking a button for example?
  ex html:
     <div id="exit">exit</exit>
     <div id="button_action">action</exit>
  id:exit or id:button_action click event receved function not search...
  -----------------------
  ie using objevent .
  Local $oEventsDoc = ObjEvent($oIE.document, "ie_", "HTMLDocumentEvents2")
  
Volatile Func ie_onclick($oEvtObj)
If IsObj($oEvtObj) Then
   Local $sId    = $oEvtObj.srcElement.id & ""
     ConsoleWrite($sId & @CRLF)
    Switch $sId
      Case "exit"
        Exit
      Case "start_action"
        ;start_action func
    EndSwitch
EndIf
EndFunc
-------------------------
  Second, how can I inject a jQuery in webview2.
  In IE, for example, I could use 
     $jQuery = $oIE.document.parentWindow.jQuery.
     Can I do this in webview2 as well?
 

Edited by davidkim

I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

Link to post
Share on other sites

All AutoIt code that exists in relation to Microsoft Edge (Chromium) and WebView2 can be found in the original thread. And the existing code is so far largely limited to a translation of the Getting started with WebView2 examples into AutoIt. You can download the AutoIt translation of the examples in bottom of this post.

My examples here are all based on code in the original thread. So if you're interested in WebView2 code, study the original thread. But as already mentioned, not much code has been implemented so far. Sufficient code has simply not been implemented to make it practically usable in real-world examples. Much more of the code in WebView2.h (included in the code for the AutoIt examples) needs to be translated into AutoIt before the code is practically applicable in real examples.

Link to post
Share on other sites

thank you LarsJ

add question is

  webview2 support  [ ExecutScriptAsync ] method ?

https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.wpf.webview2.executescriptasync?view=webview2-dotnet-1.0.774.44

 

public System.Threading.Tasks.Task<string> ExecuteScriptAsync (string javaScript);

I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

Link to post
Share on other sites

@davidkim please fix your signature.

Edited by mLipok

Signature beginning:
Please remember: "AutoIt".....  Wondering who uses AutoIt and what it can be used for ?
* GHAPI UDF - modest beginning - communication with GitHub REST API Forum Rules *
Include Dependency Tree (Tool for analyzing script relations)
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) *

PDF Related:How to get reference to PDF object embeded in IE *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2021-03-17

Link to post
Share on other sites

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

thank you  Chimp

The executescript function has a JavaScript return value of 0.

<script>

function calc(x,y){
     return x + y;
 }

</script>

au3 call...

Local $re = $oCoreWebView2.ExecuteScript('calc(5,2);' , Null); $pCoreWebView2ExecuteScriptCompletedHandler)
                ;Local $ob = json_decode($re)
                ;Local $info = json_get($ob,'info')
 MsgBox(0,'', $re )
 MsgBox(0,'',String($re) )

$re is return value is 0

Edited by davidkim

I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

Link to post
Share on other sites

Hi @davidkim,

it doesn't work that way.

You have to:

1) place your script somewhere on the managed webpage. In this case we just inject the script into the blank page (see lines 115 and 131-138)

2) call that function from AutoIt using the ExecuteScript method (see lines 60-63)

3)  and get the returned result from within the callback function CoreWebView2ExecuteScriptCompletedHandler_Invoke() where it is passed in the third parameter $wstr (see line 126)

Here the larsj script referred to in my previous link slightly modified in a very basic way just to express proof of concept. Put the numbers in the first 2 input boxes, click on the ExecuteScript button and see the result returned by the script in the box on the right

p.s.

I hope larsj doesn't blame us for this hijacking of his post :unsure:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=y

Opt("MustDeclareVars", 1)

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPICom.au3>
#include <WinAPI.au3>

Global $hGui
Global $idParam1, $vParam1, $idParam2, $vParam2, $idLabelResult

; Project includes
#include "..\Includes\WV2Interfaces.au3"

WebView2()

Func WebView2()
    ; Create WebView2 GUI
    $hGui = GUICreate("WebView2 Sample", 600, 300, -1, -1, $WS_OVERLAPPEDWINDOW)

    ; Create AutoIt controls
    Local $idButton = GUICtrlCreateButton("ExecuteScript", 10, 250, 170, 40)
    $idParam1 = GUICtrlCreateInput(5, 200, 250, 50, 40)
    $idParam2 = GUICtrlCreateInput(2, 260, 250, 50, 40)
    $idLabelResult = GUICtrlCreateLabel("", 320, 250, 170, 40)

    ; Initialize COM
    _WinAPI_CoInitialize($COINIT_APARTMENTTHREADED)

    ; Create callback interface and functions
    CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerCreate(True)
    ; Forces CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke() below to be executed

    ; Create callback interface and functions
    CoreWebView2CreateCoreWebView2ControllerCompletedHandlerCreate(True)

    ; Create callback interface and functions
    CoreWebView2ExecuteScriptCompletedHandlerCreate(True)

    ; DllCall CreateCoreWebView2EnvironmentWithOptions
    Local $hWebView2Loader = DllOpen(@AutoItX64 ? "WebView2Loader-x64.dll" : "WebView2Loader-x86.dll")
    Local $aRet = DllCall($hWebView2Loader, "long", "CreateCoreWebView2EnvironmentWithOptions", "wstr", "", "wstr", "", _
            "ptr", Null, "ptr", $pCoreWebView2CreateCoreWebView2EnvironmentCompletedHandler)
    If @error Or $aRet[0] Then Return ConsoleWrite("CreateCoreWebView2EnvironmentWithOptions ERR" & @CRLF)
    ConsoleWrite("CreateCoreWebView2EnvironmentWithOptions OK" & @CRLF & @CRLF)

    ; Show WebView2 GUI
    GUISetState(@SW_SHOW)

    ; Loop
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE
                Local $tRect = _WinAPI_GetClientRect($hGui)
                $oCoreWebView2Controller.put_Bounds($tRect)

            Case $idButton
                $vParam1 = GUICtrlRead($idParam1)
                $vParam2 = GUICtrlRead($idParam2)
                $oCoreWebView2.ExecuteScript("calc(" & $vParam1 & "," & $vParam2 & ");", $pCoreWebView2ExecuteScriptCompletedHandler)
                ; Forces CoreWebView2ExecuteScriptCompletedHandler_Invoke() below to be executed

            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    ; Cleanup
    CoreWebView2CreateCoreWebView2ControllerCompletedHandlerDelete()
    CoreWebView2CreateCoreWebView2EnvironmentCompletedHandlerDelete()
    CoreWebView2ExecuteScriptCompletedHandlerDelete()
    DllClose($hWebView2Loader)
EndFunc   ;==>WebView2

; Copied from WV2Interfaces.au3
; Executed automatically when the callback interface is created
Func CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke($pSelf, $long, $ptr)     ; Ret: long  Par: long;ptr*
    ConsoleWrite("CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke" & @CRLF)

    ; Create CoreWebView2Environment object
    $oCoreWebView2Environment = ObjCreateInterface($ptr, $sIID_ICoreWebView2Environment, $dtag_ICoreWebView2Environment)
    ConsoleWrite("IsObj( $oCoreWebView2Environment ) = " & IsObj($oCoreWebView2Environment) & @CRLF & @CRLF)

    ; Set $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler callback pointer for the WebView2 GUI
    $oCoreWebView2Environment.CreateCoreWebView2Controller($hGui, $pCoreWebView2CreateCoreWebView2ControllerCompletedHandler)
    ; Forces CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke() below to be executed

    Return 0 ; S_OK = 0x00000000
    #forceref $pSelf, $long
EndFunc   ;==>CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler_Invoke

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2Environment.CreateCoreWebView2Controller() above
Func CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke($pSelf, $long, $ptr)     ; Ret: long  Par: long;ptr*
    ConsoleWrite("CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke" & @CRLF)

    ; Create CoreWebView2Controller object
    $oCoreWebView2Controller = ObjCreateInterface($ptr, $sIID_ICoreWebView2Controller, $dtag_ICoreWebView2Controller)
    ConsoleWrite("IsObj( $oCoreWebView2Controller ) = " & IsObj($oCoreWebView2Controller) & @CRLF)
    $oCoreWebView2Controller.AddRef() ; Prevent the object from being deleted when the function ends

    ; Set bounds for the CoreWebView2 object
    Local $tRect = _WinAPI_GetClientRect($hGui)
    $oCoreWebView2Controller.put_Bounds($tRect)

    ; Create CoreWebView2 object
    $oCoreWebView2Controller.get_CoreWebView2($pCoreWebView2)
    $oCoreWebView2 = ObjCreateInterface($pCoreWebView2, $sIID_ICoreWebView2, $dtag_ICoreWebView2)
    ConsoleWrite("IsObj( $oCoreWebView2 ) = " & IsObj($oCoreWebView2) & @CRLF & @CRLF)

    ; Navigate to web page
    $oCoreWebView2.NavigateToString(_GetPage())

    Return 0 ; S_OK = 0x00000000
    #forceref $pSelf, $long
EndFunc   ;==>CoreWebView2CreateCoreWebView2ControllerCompletedHandler_Invoke

; Copied from WV2Interfaces.au3
; Executed as a consequence of $oCoreWebView2.ExecuteScript() above
Func CoreWebView2ExecuteScriptCompletedHandler_Invoke($pSelf, $long, $wstr)   ; Ret: long  Par: long;wstr
    ConsoleWrite("CoreWebView2ExecuteScriptCompletedHandler_Invoke()" & @CRLF)
    ConsoleWrite("$long = " & $long & @TAB & "$wstr = " & $wstr & @CRLF & @CRLF)
    ControlSetText('', '', $idLabelResult, $wstr)
    Return 0 ; S_OK = 0x00000000
    #forceref $pSelf
EndFunc   ;==>CoreWebView2ExecuteScriptCompletedHandler_Invoke

Func _GetPage()
    Local $sPageSource = ""
    $sPageSource &= "<SCRIPT>" & @CRLF
    $sPageSource &= "function calc(x,y){" & @CRLF
    $sPageSource &= "return x + y;" & @CRLF
    $sPageSource &= "}" & @CRLF
    $sPageSource &= "</SCRIPT>" & @CRLF
    Return $sPageSource
EndFunc   ;==>_GetPage

 

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...