Sign in to follow this  
Followers 0
JohnBailey

Help with MS Shell Interfaces

3 posts in this topic

#1 ·  Posted (edited)

Does anyone know how to use the MS Shell Interfaces? http://msdn2.microsoft.com/en-us/library/bb774328.aspx

A Specific Example: http://msdn2.microsoft.com/en-us/library/bb774950.aspx

I tried doing

Local $dirPath = "c:\WINDOWS\system32"
If Not FileExists($dirPath) Then
      MsgBox(0, "Explorer Open - Error", "Directory does not exist: "&$dirPath)
      Exit
EndIf
$oShell = ObjCreate("shell.application") 

TrayTip('Testing','New Window will open soon',2)
Local $oShellWindows = $oShell.windows
Local $currentTotal = $oShell.windows.count;First get the current explorer window count
Local $PreviousBound = $currentTotal-1;Used later
$oShell.open($dirPath);Open new
$PreviousBound +=1
        While 1;This will give it time to open (if you don't do this it will error out)
            Local $currentTotal = $oShell.windows.count
            ConsoleWrite('$currentTotal: '&$currentTotal & ' vs '&($PreviousBound+1)&@LF)
            If  $currentTotal = ($PreviousBound+1) Then
                ExitLoop
            EndIf           
            Sleep(100)          
        WEnd
Local $Window = $oShell.windows.item($PreviousBound)
Local $WindowHwnd = HWnd($window.HWnd());NOW you have the Handler for the new Explorer Window

$window.GetShowcmd()

But I don't know what goes in the parameter and at that if this is actually supposed to be a DllCall instead of a "COM-thing" like I've done

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites



You sure have been playing a lot with explorer lately... whatcha working on?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You sure have been playing a lot with explorer lately... whatcha working on?

:P Somebody asked!

I'm working on two things:

1. General learning in regards to explorer and shell (which is most really important to me in this regards). The project though that is getting me through this learning project is below and point 2.

2. A Little program I use every day called Explorer Session Saver. It may seem ridiculous to some people, but it's so helpful to me. I want to post it in the example forum area, but it's not there yet.

Here is what I got so far

#AutoIt3Wrapper_icon=\\Schnitmangroup\SchnitmanGroup\SAI\Auto-It files\Icons\Desktop.ico
; http://www.iconarchive.com/download/ico/mayosoft/azullustre-2/Desktop.ico

;=============================================================================
; Version History:
;           1.0.0 - Initial Release
;           1.1.0 - Fix: Minimized or Maximized windows will restore properly
;                   Add: Remembers Minimized or Maximized state of a window
;
; To-Do List:
;           1. Remember History, Favorites, and Search BrowserBar.
;           2. Remember Column Settings
;           3. Remember Column Sort
;
;=============================================================================
Global $version = '1.0.0'
#Include <GuiListView.au3>
#Include <File.au3>
#Include <Array.au3>

Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode",1)
Opt("OnExitFunc", "OnAutoItExit"); Used for further features and diagnosis
Opt("GUIOnEventMode", 1)


; IShellDispatch2 (http://msdn2.microsoft.com/en-us/library/bb774150.aspx) 
; Thank you Dale Hohm
Global Const $ExplorerBarCLSID_Folders   = '{EFA24E64-B078-11d0-89E4-00C04FC9E26E}'
Global Const $ExplorerBarCLSID_History   = '{EFA24E62-B078-11d0-89E4-00C04FC9E26E}'
Global Const $ExplorerBarCLSID_Favorites = '{EFA24E61-B078-11d0-89E4-00C04FC9E26E}'
Global Const $ExplorerBarCLSID_Search    = '{30D02401-6A81-11d0-8274-00C04FD5AE38}'


;HotKeySet('{ESC}','Terminate');Used during Testing


TraySetToolTip("Explorer Session Saver v"&$version&" - Save and Load MS Explorer Windows with the positions and settings")
TraySetClick(16)
$MenuItem3 = TrayCreateItem("Exit")
TrayItemSetOnEvent($MenuItem3, "MenuItem3Click")
TrayCreateItem("")
$MenuItem2 = TrayCreateItem("Load Session")
TrayItemSetOnEvent($MenuItem2, "MenuItem2Click")
$MenuItem1 = TrayCreateItem("Save Session")
TrayItemSetOnEvent($MenuItem1, "MenuItem1Click")
TrayCreateItem("")
$MenuItem4 = TrayCreateItem("Quick Load")
TrayItemSetOnEvent($MenuItem4, "MenuItem4Click")
$MenuItem5 = TrayCreateItem("Quick Save")
TrayItemSetOnEvent($MenuItem5, "MenuItem5Click")

While 1
    Sleep(100)
WEnd
Func SaveSession($_dialog=True)
    Local $winArray[1][11]
    
    If $_dialog Then
        Local $_file = FileSaveDialog('Where to Save',@MyDocumentsDir,"Explorer Session File (*.esf)",16,'Explorer Session - '&@MON&@MDAY&@YEAR)
        If @error Then
            Return 0
        EndIf
        If StringRight($_file,4) <> '.esf' Then
            $_file &= '.esf'
        EndIf
    Else
        Local $_file = @MyDocumentsDir&'\Previous Session.esf'
    EndIf
    _FileCreate($_file)
    $oShell = ObjCreate("shell.application")   ; Get the Windows Shell Object
    $oShellWindows=$oShell.windows       ; Get the collection of open shell Windows
    Local $count = 0
    If Isobj($oShellWindows) Then
        For $Window In $oShellWindows       ; Count all existing shell windows
            $count += 1
            Local $WindowHwnd = HWnd($window.HWnd())
            Local $winLocation = StringReplace($window.locationURL (),'%20',' ')
            Local $winName = $window.LocationName

            Local $WinStateTotal = WinGetState($WindowHwnd)
            If BitAND($WinStateTotal,16) Then
                WinSetState($WindowHwnd,'',@SW_RESTORE)
            ;WinSetState($WindowHwnd,'',@SW_HIDE)
                Local $winPos = WinGetPos($WindowHwnd)
                WinSetState($WindowHwnd,'',@SW_MINIMIZE)
            ;WinSetState($WindowHwnd,'',@SW_SHOW)
                
                Local $WinState = 'MINIMIZE'
            ElseIf BitAND($WinStateTotal,32) Then
                WinSetState($WindowHwnd,'',@SW_RESTORE)
            ;WinSetState($WindowHwnd,'',@SW_HIDE)
                Local $winPos = WinGetPos($WindowHwnd)
                WinSetState($WindowHwnd,'',@SW_MAXIMIZE)
            ;WinSetState($WindowHwnd,'',@SW_SHOW)
                
                Local $WinState  = 'MAXIMIZE'
            Else
                Local $winPos = WinGetPos($WindowHwnd)
                
                Local $WinState  = 'RESTORE'
            EndIf
            
            Local $x = $winPos[0];$window.Left ()
            Local $y = $winPos[1];$window.Top ()
            Local $w = $winPos[2];$window.Width ()
            Local $h = $winPos[3];$window.Height ()
            
            Local $vis = $window.Visible()
            Local $FolderBarState
            Local $BrowserBar_HWnd=ControlGetHandle($WindowHwnd,'','[Class:SysTreeView32;Instance:1]')
            If $BrowserBar_HWnd <> 0 Then
                $FolderBarState = 1
            Else
                $FolderBarState = 0
            EndIf
            Local $FileListView_HWnd=ControlGetHandle($WindowHwnd,'','[Class:SysListView32;Instance:1]')
            If $vis = -1 Then
                $vis = 1
            Else
                $vis = 0
            EndIf
            If StringInStr($winLocation,'file://') Then;prevents from Internet Explorer windows being captured
                Redim $winArray[UBound($winArray)+1][UBound($winArray,2)]
                $winArray[UBound($winArray)-1][0] = $WindowHwnd
                $winArray[UBound($winArray)-1][1] = $winName
                $winArray[UBound($winArray)-1][2] = $winLocation
                $winArray[UBound($winArray)-1][3] = $x
                $winArray[UBound($winArray)-1][4] = $y
                $winArray[UBound($winArray)-1][5] = $w
                $winArray[UBound($winArray)-1][6] = $h
                $winArray[UBound($winArray)-1][7] = $vis; Currently not used, but could be used in the future
                $winArray[UBound($winArray)-1][8] = $WinState
                $winArray[UBound($winArray)-1][9] = $FolderBarState
                $winArray[UBound($winArray)-1][10] = _GUICtrlListView_GetView($FileListView_HWnd)
                Local $lineToWrite = ''
                For $_b = 1 To UBound($winArray,2)-1
                    If $_b <> UBound($winArray,2)-1 Then
                        $lineToWrite &= $winArray[UBound($winArray)-1][$_b]&chr(134)
                    Else;Last Column Entry so no delimiter is required
                        $lineToWrite &= $winArray[UBound($winArray)-1][$_b]
                    EndIf
                Next
                _FileWriteToLine($_file,$count-1,$lineToWrite,1)
                If @error Then
                    TrayTip('Explorer Session Saver - Error','Could not write line '&$count&' to file '&$_file&' (extended: '&@error&')',2)
                EndIf
                
            EndIf
            
        Next
    Else
        TrayTip('Explorer Session Saver - Error','Shell.Application.Windows object problem unknown',2)
    EndIf
    $winArray = 0;Reset Array
EndFunc
Func LoadSession($_Dialog=True)
    If $_dialog Then
        Local $_file = FileOpenDialog('Where to Save',@MyDocumentsDir,"Explorer Session File (*.esf)",2)
        If @error Then
            Return 0
        EndIf
        If StringRight($_file,4) <> '.esf' Then
            $_file &= '.esf'
        EndIf
    Else
        Local $_file = @MyDocumentsDir&'\Previous Session.esf'
        If Not FileExists($_file) Then
            TrayTip('Explorer Session Saver - Error','Quick Save File cannot be located',3)
            Return -1
        EndIf
    EndIf
    
    Local $oShell = ObjCreate("shell.application")   ; Get the Windows Shell Object
    Local $oShellWindows = $oShell.windows
    If NOT Isobj($oShellWindows) Then
        TrayTip('Explorer Session Saver - Error','Shell.Application.Windows object problem unknown',3)
        Return -1
    EndIf
    Local $currentTotal = $oShell.windows.count
    
    Local $PreviousBound = $currentTotal-1
    
    Local $_fileArray
    _FileReadToArray($_file,$_fileArray)
    ProgressOn('Explorer Session Saver','Loading Session')
    
    Local $totalToLoad = 0
    For $_a = 1 To UBound($_fileArray)-1
        Local $_lineSplit = StringSplit($_fileArray[$_a],Chr(134),1)
        If $_lineSplit[0] <> 10 Then
            ContinueLoop
        Else
            $totalToLoad += 1
        EndIf
        
    Next
    
    For $_a = 1 To UBound($_fileArray)-1
        Local $_lineSplit = StringSplit($_fileArray[$_a],Chr(134),1)
        Local $percent = $_a * (100/$totalToLoad)
        ProgressSet($percent,$_a&' of '&$totalToLoad&'     Please Wait')
        If $_lineSplit[0] <> 10 Then
            ContinueLoop
        EndIf
        
        Local $winName = $_lineSplit[1]
        Local $winLocation = $_lineSplit[2]
        Local $x = $_lineSplit[3]
        Local $y = $_lineSplit[4]
        Local $w = $_lineSplit[5]
        Local $h = $_lineSplit[6]
        Local $vis = $_lineSplit[7]
        Local $WinState = $_lineSplit[8]
        Local $FolderBarState = $_lineSplit[9]
        Local $LV_View = $_lineSplit[10]
        If StringInStr($winLocation,'///') Then
            Local $_locationTrimed = StringReplace($winLocation,'file:///','')
        Else
            Local $_locationTrimed = StringReplace($winLocation,'file:','')
        EndIf
        DirGetSize($_locationTrimed)
        If @error Then
            TrayTip('Explorer Session Saver','Folder No Longer Exists: '&$_locationTrimed,3)
            ContinueLoop
        EndIf
        $oShell.open($winLocation)
        $PreviousBound +=1
        While 1
            Local $currentTotal = $oShell.windows.count
            ConsoleWrite('$currentTotal: '&$currentTotal & ' vs '&($PreviousBound+1)&@LF)
            If  $currentTotal = ($PreviousBound+1) Then
                ExitLoop
            EndIf           
            Sleep(100)          
        WEnd
    ;Sleep(4000)
        Local $Window = $oShell.windows.item($PreviousBound)
        Local $WindowHwnd = HWnd($window.HWnd())
        
        If $WinState = 'MINIMIZE' Then
            WinSetState($WindowHwnd,'',@SW_MINIMIZE)
        ElseIf $WinState = 'MINIMIZE' Then
            WinSetState($WindowHwnd,'',@SW_MAXIMIZE)
        EndIf
        
        Local $FileListView_HWnd=ControlGetHandle($WindowHwnd,'','[Class:SysListView32;Instance:1]')
        
        If $FolderBarState = 1 Then
            $window.ShowBrowserBar ($ExplorerBarCLSID_Folders, True)
        EndIf
        
        _GUICtrlListView_SetView($FileListView_HWnd,$LV_View)
        
        WinMove($WindowHwnd,'',$x,$y,$w,$h)
    Next
    ProgressOff()
EndFunc
Func MenuItem1Click() 
    SaveSession()
EndFunc
Func MenuItem2Click()
    LoadSession()
EndFunc
Func MenuItem3Click();Exit
    Terminate()
EndFunc
Func MenuItem4Click()
    LoadSession(False)
EndFunc
Func MenuItem5Click(); Quick Save
    SaveSession(False)
EndFunc


Func OnAutoItExit()
    ConsoleWrite('CLOSED via Exit'&@CRLF)
EndFunc

Func Terminate()
    ConsoleWrite('CLOSED via Terminate'&@CRLF)
    Exit
EndFunc

As you can see there is much to be expanded on and cleaned up! If you have any suggestions or pointers, please let me know. Feel free to slap me around too <_<

It's a project that has taught me a lot about explorer and I'm so curious about what else is there!! It may spark further understanding, which is SO awesome!

That may in-turn, give me an idea for another project.

EDIT

Smoke_N, I'd really appreciate any assistance with the above project. Also, do you know how to use the MS Shell Interfaces? :)

EDIT

Bug Fix - file would not save

Edited by JohnBailey

A decision is a powerful thing

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
Sign in to follow this  
Followers 0