forked from lee-soft/ViPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellHelper.bas
100 lines (78 loc) · 2.99 KB
/
ShellHelper.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Attribute VB_Name = "ShellHelper"
Private Declare Function EnumDesktopWindows Lib "user32" (ByVal hDesktop _
As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Private shellWindowHandle As Long
Public Function HandleError(ByVal errorCode As Long)
MsgBox "error retrieving handle: " & errorCode
End Function
Public Function FindShellWindow() As Long
'IntPtr progmanHandle;
'IntPtr defaultViewHandle = IntPtr.Zero;
'IntPtr workerWHandle;
'int errorCode = NativeMe
Dim progmanHandle As Long
Dim defaultViewHandle As Long
Dim workerWHandle As Long
Dim errorCode As Long
Dim ret As Long
'Try the easy way first. "SHELLDLL_DefView" is a child window of "Progman".
progmanHandle = FindWindowEx(0, 0, "Progman", vbNullString)
If Not (progmanHandle = 0) Then
defaultViewHandle = FindWindowEx(progmanHandle, 0, "SHELLDLL_DefView", vbNullString)
errorCode = GetLastError()
End If
If Not (defaultViewHandle = 0) Then
FindShellWindow = defaultViewHandle
Exit Function
ElseIf Not errorCode = ERROR_SUCCESS Then
HandleError errorCode
Exit Function
End If
'Try the not so easy way then. In some systems "SHELLDLL_DefView" is a child of "WorkerW"
errorCode = ERROR_SUCCESS
workerWHandle = FindWindowEx(0, 0, "WorkerW", vbNullString)
Debug.Print "FindShellWindow.workerWHandle: " & workerWHandle
If Not (workerWHandle = 0) Then
defaultViewHandle = FindWindowEx(workerWHandle, 0, "SHELLDLL_DefView", vbNullString)
errorCode = GetLastError()
End If
If Not (defaultViewHandle = 0) Then
FindShellWindow = defaultViewHandle
Exit Function
ElseIf Not (errorCode = ERROR_SUCCESS) Then
HandleError errorCode
Exit Function
End If
shellWindowHandle = 0
'Try the hard way. In some systems "SHELLDLL_DefView" is a child or a child of "Progman".
If EnumChildWindows(progmanHandle, AddressOf EnumWindowsProc, ret) = 0 Then
errorCode = GetLastError()
If Not (errorCode = ERROR_SUCCESS) Then
HandleError errorCode
Exit Function
End If
End If
'Try the even more harder way. Just in case "SHELLDLL_DefView" is in another desktop.
If (shellWindowHandle = 0) Then
If EnumDesktopWindows(0, AddressOf EnumWindowsProc, progmanHandle) Then
errorCode = GetLastError()
If Not (errorCode = ERROR_SUCCESS) Then
HandleError errorCode
Exit Function
End If
End If
End If
FindShellWindow = shellWindowHandle
End Function
Public Function EnumWindowsProc(ByVal handle As Long, ByVal lParam As Long) _
As Long
Dim foundHandle As Long
foundHandle = FindWindowEx(handle, 0, "SHELLDLL_DefView", vbNullString)
If Not (foundHandle = 0) Then
shellWindowHandle = foundHandle
EnumWindowsProc = 0
Exit Function
End If
'Continue Enumerating
EnumWindowsProc = 1
End Function