COMUtils.au3 2.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include-once
  2. Func CoCreateInstance( $rclsid, $pUnkOuter, $ClsContext, $riid, ByRef $ppv )
  3. Local $aRet = DllCall( "ole32.dll", "long", "CoCreateInstance", "struct*", $rclsid, "ptr", $pUnkOuter, "dword", $ClsContext, "struct*", $riid, "ptr*", 0 )
  4. If @error Then Return SetError(1,0,1)
  5. $ppv = $aRet[5]
  6. Return $aRet[0]
  7. EndFunc
  8. ; Add these lines to a script to activate the error handler:
  9. ;Local $oComErrFunc = ObjEvent( "AutoIt.Error", "ComErrFunc" )
  10. ;#forceref $oComErrFunc
  11. Func ComErrFunc( $oError )
  12. ConsoleWrite( @ScriptName & "(" & $oError.scriptline & "): ==> COM Error intercepted!" & @CRLF & _
  13. @TAB & "Err.number is: " & @TAB & @TAB & "0x" & Hex( $oError.number ) & @CRLF & _
  14. @TAB & "Err.windescription:" & @TAB & $oError.windescription & _
  15. @TAB & "Err.description is: " & @TAB & $oError.description & @CRLF & _
  16. @TAB & "Err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
  17. @TAB & "Err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
  18. @TAB & "Err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
  19. @TAB & "Err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
  20. @TAB & "Err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
  21. @TAB & "Err.retcode is: " & @TAB & "0x" & Hex( $oError.retcode ) & @CRLF )
  22. EndFunc
  23. ; Copied from "Hooking into the IDispatch interface" by monoceres
  24. ; https://www.autoitscript.com/forum/index.php?showtopic=107678
  25. Func ReplaceVTableFuncPtr( $pVTable, $iOffset, $pNewFunc )
  26. Local $pPointer = DllStructGetData( DllStructCreate( "ptr", $pVTable ), 1 ) + $iOffset, $PAGE_EXECUTE_READWRITE = 0x40
  27. Local $pOldFunc = DllStructGetData( DllStructCreate( "ptr", $pPointer ), 1 ) ; Get the original function pointer in VTable
  28. Local $aRet = DllCall( "Kernel32.dll", "int", "VirtualProtect", "ptr", $pPointer, "long", @AutoItX64 ? 8 : 4, "dword", $PAGE_EXECUTE_READWRITE, "dword*", 0 ) ; Unprotect memory
  29. DllStructSetData( DllStructCreate( "ptr", $pPointer ), 1, $pNewFunc ) ; Replace function pointer in VTable with $pNewFunc function pointer
  30. DllCall( "Kernel32.dll", "int", "VirtualProtect", "ptr", $pPointer, "long", @AutoItX64 ? 8 : 4, "dword", $aRet[4], "dword*", 0 ) ; Protect memory
  31. Return $pOldFunc ; Return original function pointer
  32. EndFunc