Jump to content

IECreatePseudoEmbedded


slbmeh
 Share

Recommended Posts

Inspired by I took a more simplified approach that is more reliant on IE.au3.

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <IE.au3>
Func IECreatePseudoEmbedded($iLeft, $iTop, $iWidth, $iHeight, $hParent, $sURL = "about:blank", $bShowWin = False)
Local $iPID = Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe -k " & $sURL, "", @SW_HIDE)
Sleep(2000)
Local $oIE = _IEAttach($sURL, "URL", 1)
Local $hWND = _IEPropertyGet($oIE, "hwnd")
_WinAPI_SetParent($hWND, $hParent)
_WinAPI_MoveWindow($hWND, $iLeft, $iTop, $iWidth, $iHeight, True)
_WinAPI_SetWindowLong($hWND, $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)
If $bShowWin == True Then
  WinSetState($hWND, "", @SW_SHOW)
EndIf
Return $oIE
EndFunc

With this function it runs IE in kiosk mode defaulting to hidden. It will make the window a child window, resize, relocate, and redraw. It will then show the window if the ShowWin flag is set to true.

It does not handle killing the process I would like to register a cleanup function if there is a way to call a function before application close. I wan't able to find a simple way to handle this just yet, so for the time being your application should call _IEQuit on application close.

Edited by slbmeh
Link to comment
Share on other sites

I really like what you did. I really appreciated _IECreate2, but didn't like the process polling algorithm.

I have enhanced what you posted to further follow the IE.au3 style, made it more bullet-proof and included a working example. Haven't tested all of the failure scenarios, so your testing and that of others would be good. Nice.

Dale

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <IE.au3>

Dim $hGUI, $oIE, $sURL, $iLeft, $iTop, $iHeight, $iWidth

$sURL = "http://www.google.com"
$iLeft = 0
$iTop = 0
$iWidth = 1024
$iHeight = 768

$hGUI = GUICreate("Test", $iWidth, $iHeight)

$oIE = IECreatePseudoEmbedded($iLeft, $iTop, $iWidth, $iHeight, $hGUI)
_IENavigate($oIE, $sURL)
GUISetState(@SW_SHOW, $hGUI)

While 1
  $msg = GUIGetMsg()
  If $msg = $GUI_EVENT_CLOSE Then
    _IEQuit($oIE)
    Exit
  EndIf
WEnd

Exit


Func IECreatePseudoEmbedded($i_Left, $i_Top, $i_Width, $i_Height, $h_Parent, $f_ShowWin = False, $i_Timeout = 30000)

   Local Const $i_Random = Random(10001, 99999, 1)
   Local $i_PID, $o_IE, $h_HWND, $h_Timer = TimerInit()

   $i_PID = Run(@ProgramFilesDir & "Internet Exploreriexplore.exe -k " & "about:blank-" & $i_Random, "", @SW_HIDE)

   $f_ErrorNotify = _IEErrorNotify()
   _IEErrorNotify(False)

   While Not IsObj($o_IE)
     $o_IE = _IEAttach("about:blank-" & $i_Random, "URL")
     If TimerDiff($h_Timer) > $i_Timeout Then
        _IEErrorNotify($f_ErrorNotify)
        Return SetError(1, 0, 0)
     EndIf
     Sleep(200)
   WEnd

   _IEErrorNotify($f_ErrorNotify)

   $h_HWND = _IEPropertyGet($o_IE, "hwnd")

   _WinAPI_SetParent($h_HWND, $h_Parent)
   _WinAPI_MoveWindow($h_HWND, $i_Left, $i_Top, $i_Width, $i_Height, True)
   _WinAPI_SetWindowLong($h_HWND, $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

   If $f_ShowWin Then
     WinSetState($h_HWND, "", @SW_SHOW)
   EndIf

   Return $o_IE

EndFunc   ;==>IECreatePseudoEmbedded

Notes: This modification move navigation to the URL out of the function. It creates a random URL for the initial creation so that there will be no trouble with duplicate windows. It adds a timeout waiting for the browser to be created.

Updated: Updated to use JohnOne's idle loop suggestion (below)

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

@DaleHohm

As your example stands, I recieve a crash upon exiting via the gui close x

When that occurs, sometimes the spawned IE processes are not closed.

It appears to be connected to the way you have the main loop

Do
    Sleep(200)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_IEQuit($oIE)
Exit

If it is changed to

While 1
    $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
  _IEQuit($oIE)
  Exit
EndIf
WEnd

The problem is gone.

Latest release version of Autoit3

Windows 7 32

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Notes: This modification move navigation to the URL out of the function. It creates a random URL for the initial creation so that there will be no trouble with duplicate windows. It adds a timeout waiting for the browser to be created.

I like that. Duplicate windows was a concern for me, it wouldn't affect me, because I know what could screw it up... But if others were to use it, I couldn't think of how to prevent that.

One thing I have encountered with this method is that if you use _IEPropertyGet for hwnd after you set parent it will give the parent handle and not that of the actual IE window. Do you know if there is a better way to get the handle?

I currently have this piece of code that I do not like, because I have multiple child windows and if one is open it doesn't always give expected results.

Func IEGetHandle($oIE)
Local $hWND = _IEPropertyGet($oIE, "hwnd")
$hWND = _WinAPI_GetWindow($hWND, $GW_CHILD)
Return $hWND
EndFunc
Link to comment
Share on other sites

I very rarely use the GUI functions or the WinApi functions, so I'm not much help there. I modified my post above to use JohnOne's idle loop suggestion.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

You could alter the function to return an array, with the hwnd as one of the array elements.

Edit:

Or maybe an additional byref parameter for the hwnd?

The array return would work, I wanted to try to remain with just the $oIE return to refrain from straying too far from the functionality of IE.au3.

I'm uncertain as to how much benefit a ByRef would be, generally the same downside as a Global with added complexity.

Edit: @JohnOne

I use GUIRegisterMsg instead of GUIGetMessage or OnEventMode, so I'm not sure why the difference there.

Edited by slbmeh
Link to comment
Share on other sites

I'm uncertain as to how much benefit a ByRef would be, generally the same downside as a Global with added complexity.

Absolutely not. You add one byref parameter to the function call ie ByRef $hWnd, and pass in a variable of your choosing, both in name and scope. The function sets that var to the created window hwnd before changing its parent. You can do what you want with it after that. This way you can keep the same object return as the standard UDF functions.

Edited by wraithdu
Link to comment
Share on other sites

Absolutely not. You add one byref parameter to the function call ie ByRef $hWnd, and pass in a variable of your choosing, both in name and scope. The function sets that var to the created window hwnd before changing its parent. You can do what you want with it after that. This way you can keep the same object return as the standard UDF functions.

My mention of added complexity was not of the function itself, but in applied use cases. Adding a ByRef parameter isn't difficult, but it would add a mandatory parameter that would not be necessary in many cases adding unnecessary complexity to the third party code using this function.

Link to comment
Share on other sites

  • 3 months later...

I really like what you did. I really appreciated _IECreate2, but didn't like the process polling algorithm.

I have enhanced what you posted to further follow the IE.au3 style, made it more bullet-proof and included a working example. Haven't tested all of the failure scenarios, so your testing and that of others would be good. Nice.

Dale

Dale,

You don't need to use IEAttach at all... create the object, assign it theatre mode, full screen, and hide the status bar if you need to. So far I have found that the embedded object doesn't automatically take the focus when you selet the GUI title bar, so I added an "if winactive then winactivate" line in my main program loop. I'm sure there is a better solution, but I'm still not exactly an expert with GUI's... more like a seasoned amatuer:)

Also of note: the vertical scroll bars do not redraw every time the window is resized, the GUI background color is shown until you mouseover the scroll bars or an element in the webpage that changes with the mouseover (button that emboldens, for example). I'm using _WinAPI_MoveWindow to move the embedded object when the parent moves, and have even tried manually redrawing the window after the move, hiding and unhiding it... many unconventional things as well as everything I could search for that was within my capabilities at this point, all to no avail. I worked around this by turning the scroll bars off and then back on any time the window is resized. It's a little messy visually, but it works.

I'm using autoit to scrape customer account numbers, as they are viewed by sales in our AS400, to pull up in Salesforce.com. Most of our sales people are on thin clients. I had to find a way of getting the object without using the collections method in the IEAttach function... for some reason this part of the function always gets stuck in an endless loop on our thin clients.Took me forever to figure out that was the issue, and then quite awhile to figure out a way around it. The above methods work perfectly on a PC, but my solution works in our very restrictive thin client environment and appears to be more efficient in my limited experience.

Your posts have been instrumental in my own coding, thank you for your posts and any criticism you may be wiling (anyone for that matter) to give on my code below.

Have a good day,

Dan

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <IE.au3>

Func IECreatePseudoEmbedded($i_Left, $i_Top, $i_Width, $i_Height, $h_Parent)

Local $o_IE, $h_HWND

$o_IE = ObjCreate("InternetExplorer.Application")
$o_IE.theatermode = True
$o_IE.fullscreen = True
$o_IE.statusbar = False

$h_HWND = _IEPropertyGet($o_IE, "hwnd")
_WinAPI_SetParent($h_HWND, $h_Parent)
_WinAPI_MoveWindow($h_HWND, $i_Left, $i_Top, $i_Width, $i_Height, False)
_WinAPI_SetWindowLong($h_HWND, $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

Return $o_IE

EndFunc ;==>IECreatePseudoEmbedded
Link to comment
Share on other sites

  • 9 months later...

I'm a great fan of these methods, but alas with IE.10 It crashes upon exit if there is another instance of IE open besides itself.

I'd be happy to hear is anyone has a solution to this.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Can you be more specific, please? What crashes, when what exits? Error messages?

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Sorry, Internet explorer crashes., with the usual box...

"Internet explorer has stopped working"

"check online"

"Restart" or sometimes "close"

"debug"

Problem signature:

Problem Event Name: APPCRASH

Application Name: iexplore.exe

Application Version: 10.0.9200.16537

Application Timestamp: 512347f7

Fault Module Name: IEFRAME.dll

Fault Module Version: 10.0.9200.16540

Fault Module Timestamp: 5125f35b

Exception Code: c0000005

Exception Offset: 000163e2

OS Version: 6.1.7601.2.1.0.256.48

Locale ID: 2057

Additional Information 1: 0a9e

Additional Information 2: 0a9e372d3b4ad19135b953a78882e789

Additional Information 3: 0a9e

Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Crashes however you end it, even with ctrl + Break

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Odd. When I run DangerousDan's version of the code, I get no crash (running the same version of 32-bit IE10 as you are).

Perhaps you can post a reproducer or try on another machine running IE10.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Yes, I did. The one from whence I am replying to you.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
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
 Share

×
×
  • Create New...