sriram91

AutoIt Scripts not working as expected when called from PHP

4 posts in this topic

I have an autoit script that clears the IE cache, then navigate to a webpage and takes a screenshot of it. I have compiled the script as an exe and it works fine when i execute it manually by double clicking it but when i execute the script from a PHP file the .exe file does execute and the screenshot is taken but IE cache is not cleared (I definitely need the IE cache to be cleared as the web page i am taking screenshot has some cache issues).

Does anyone knows the reason why IE cache is not cleared only when called through PHP. Is it some sort of access control issue. Below is my autoit script (It uses AutoItObject.au3 for taking screenshot). And i am calling the compiled exe file from PHP as shell_exec('screenshot_app.exe');

 

#Include <IE.au3>
#Include <WinAPI.au3>
#Include <ScreenCapture.au3>
#Include <WindowsConstants.au3>
#Include "AutoItObject.au3"

_AutoItObject_StartUp()
OnAutoItExitRegister('OnExit')


Func clearCache()

dim $ClearID=2

dim $run=Run("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess " & $ClearID)

while 1

if ProcessExists ($run) then

WinSetState ("[CLASS:#32770]","",@SW_disable)

else

exitloop

endif

wend

Sleep(2000)

EndFunc


clearCache()

WebCaptureTest("https://www.google.com", "Test1.png")

clearCache()

WebCaptureTest("https://www.gmail.com")

clearCache()

WebCaptureTest("https://www.ymail.com", "Test3.png")


Func OnExit()
    _AutoItObject_Shutdown()
EndFunc

Func WebCaptureTest($Url, $ImageName)

    Local $hBmp = WebCapture($Url, 1280)
    _ScreenCapture_SaveImage($ImageName, $hBmp, True)
EndFunc

Func ElementCaptureTest($Url, $ImageName)
    Local $WebWidth = 1280
    Local $oIE = ObjCreate("Shell.Explorer.2")
    GUICreate("", $WebWidth, 768, -1, -1)
    GUICtrlCreateObj($oIE, 0, 0 , $WebWidth, 768)
    _IENavigate($oIE, $Url)

    Local $oElement = $oIE.document.documentElement
    If $oElement.clientWidth = 0 Then $oElement = $oIE.document.body
    Local $hBmp = ElementCapture($oElement)
    _ScreenCapture_SaveImage($ImageName, $hBmp, True)
EndFunc

Func WebCapture($Url, $WebWidth = 1024)
    Local Const $DVASPECT_CONTENT = 1
    Local $tIID_IViewObject2 = _AutoItObject_CLSIDFromString("{00000127-0000-0000-C000-000000000046}")
    Local $dtagIViewObject2 = $dtagIUnknown & "Draw hresult(dword;long;ptr;ptr;dword;dword;ptr;ptr;int;dword);"
    Local $oIE = ObjCreate("Shell.Explorer.2")

    GUICreate("", $WebWidth, 768, -1, -1)
    GUICtrlCreateObj($oIE, 0, 0 , $WebWidth, 768)

    _IENavigate($oIE, $url)
    _IELoadWait($oIE)

    Sleep(7000)
    Local $oDocument = $oIE.document
    Local $oBody = $oIE.document.body
    Local $oHtml = $oIE.document.documentElement

    $oBody.scroll = "no"
    $oBody.style.borderStyle = "none"
    $oHtml.style.overflow = 'hidden'
    $oBody.style.overflow = 'hidden'

    Local $oIViewObject2 = _AutoItObject_WarpInterface($oDocument, $tIID_IViewObject2, $dtagIViewObject2)
    If @Error Then Return SetError(1, 0, 0)

    Local $BodyWidth = $oBody.scrollWidth
    Local $BodyHeight = $oBody.scrollHeight
    Local $RootWidth = $oHtml.scrollWidth
    Local $RootHeight = $oHtml.scrollHeight

    Local $Width = $BodyWidth
    Local $Height = $RootHeight
    If $BodyHeight > $Height Then $Height = $BodyHeight

    $oIE.width = $Width
    $oIE.height = $Height

    Local $hDC = _WinAPI_GetDC(0)
    Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
    _WinAPI_SelectObject($hMemDc, $hBmp)

    Local $sRECT = DllStructCreate($tagRECT)
    DllStructSetData($sRECT, "Top", 0)
    DllStructSetData($sRECT, "Left", 0)
    DllStructSetData($sRECT, "Right", $Width)
    DllStructSetData($sRECT, "Bottom", $Height)

    $oIViewObject2.Draw($DVASPECT_CONTENT, -1, 0, 0, Number($hDC), Number($hMemDC), Number(DllStructGetPtr($sRECT)), 0, 0, 0)

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_ReleaseDC(0, $hDC)
;~  _IEQuit($oIE)
    Return $hBmp
EndFunc

Func ElementCapture($oElement, $Border = 2)
    If Not IsObj($oElement) Then Return SetError(1, 0, 0)
    If $oElement.clientWidth = 0 Or $oElement.clientHeight = 0 Then Return SetError(1, 0, 0)

    Local $tIID_IHTMLElementRender = _AutoItObject_CLSIDFromString("{3050F669-98B5-11CF-BB82-00AA00BDCE0B}")
    Local $dtagIHTMLElementRender = $dtagIUnknown & "DrawToDC hresult(hwnd);"

    Local $PageHeight = $oElement.scrollHeight - $Border
    Local $PageWidth = $oElement.scrollWidth - $Border

    Local $oIHTMLElementRender = _AutoItObject_WarpInterface($oElement, $tIID_IHTMLElementRender, $dtagIHTMLElementRender)
    Local $hDC = _WinAPI_GetDC(0)
    Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateSolidBitmap(0, 0xffffff, $oElement.clientWidth, $oElement.clientHeight)
    Local $hMemDcFull = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmpFull = _WinAPI_CreateSolidBitmap(0, 0xffffff, $PageWidth, $PageHeight)

    _WinAPI_SelectObject($hMemDc, $hBmp)
    _WinAPI_SelectObject($hMemDcFull, $hBmpFull)

    Local $DrawWidth = $oElement.clientWidth - $Border
    Local $DrawHeight = $oElement.clientHeight - $Border
    Local $CurrentX = 0
    Local $CurrentY = 0

    While $CurrentX < $PageWidth
        While $CurrentY < $PageHeight
            $oElement.scrollLeft = $CurrentX
            $oElement.scrollTop = $CurrentY
            $oIHTMLElementRender.DrawToDC(Number($hMemDC))
            _WinAPI_BitBlt($hMemDcFull, $oElement.scrollLeft, $oElement.scrollTop, $DrawWidth, $DrawHeight, $hMemDC, $Border, $Border, $SRCCOPY)
            $CurrentY += $DrawHeight
        WEnd
        $CurrentY = 0
        $CurrentX += $DrawWidth
    WEnd

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_DeleteDC($hMemDcFull)
    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($hBmp)

    Return $hBmpFull
EndFunc

Func _AutoItObject_WarpInterface($Obj, $IID, $Tag, $fNoUnknown = False)
    If Not IsDllStruct($IID) Then $IID = _AutoItObject_CLSIDFromString($IID)

    If Not IsObj($Obj) Then Return SetError(1, 0, 0)

    Local $pObj = _AutoItObject_IDispatchToPtr($Obj)
    If Not _AutoItObject_IUnknownAddRef($Obj) Then Return SetError(1, 0, 0)
    Local $ObjWarpped = _AutoItObject_WrapperCreate($pObj, $dtagIUnknown, $fNoUnknown)
    Local $aCall = $ObjWarpped.QueryInterface(Number(DllStructGetPtr($IID)), 0)
    If Not IsArray($aCall) And $aCall[0] <> 0 Then Return SetError(1, 0, 0)

    Local $pInterface = $aCall[2]
    Return _AutoItObject_WrapperCreate($pInterface, $Tag)
EndFunc

 

Share this post


Link to post
Share on other sites



1 hour ago, sriram91 said:

Does anyone knows the reason why IE cache is not cleared only when called through PHP.

How is the  PHP file shelled? What are the credentials this PHP is running with?  

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I just solved problem by allowing Apache to Interact with Desktop in the Services configuration (Start->Services->wampapache->properties->logon->Allow service to Interact with desktop). Thanks.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I am facing a similar kind of problem now but with a different script.  The script logins to a web page and downloads a CSV file via IE. It works fine when i run the exe manually but when i run it through php nothing happens and i have no idea if it is executing or not.  Allowing service to Interact with desktop doesn't help too. Is there anyway to find out why the exe doesn't execute? Also suggest me any other ideas to call the autoIT scripts programmatically.

Edited by sriram91

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

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

Create an account

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


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now