Skip to content

Commit 1bbde20

Browse files
committed
Initial version - wrapper (clsCDP and cdp/*) of the CDP protocol with most of the types also generated, initial higher level driver (AutomateBrowser), generator for CDP protocol vba wrapper (generates cdp/*)
1 parent 481119c commit 1bbde20

File tree

262 files changed

+61784
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

262 files changed

+61784
-0
lines changed

src/AutomateBrowser.cls

Lines changed: 994 additions & 0 deletions
Large diffs are not rendered by default.

src/ClipboardUtils.bas

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Attribute VB_Name = "ClipboardUtils"
2+
'misc clipboard functions, allows selection Range and converting to CSV in clipboard to paste into another application
3+
Option Explicit
4+
5+
6+
' Work around for bug in Excel, must put directly in clipboard or results potentially scrambled when Explorer window open
7+
' https://msdn.microsoft.com/en-us/library/office/ff192913.aspx?f=255&MSPPError=-2147217396
8+
' https://www.experts-exchange.com/questions/28960655/after-upgrade-to-windows-10-vba-dataobject-putinclipboard-stops-working.html
9+
10+
Private Declare PtrSafe Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
11+
Private Declare PtrSafe Function EmptyClipboard Lib "user32.dll" () As Long
12+
Private Declare PtrSafe Function CloseClipboard Lib "user32.dll" () As Long
13+
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32.dll" (ByVal wFormat As Long) As Long
14+
Private Declare PtrSafe Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
15+
Private Declare PtrSafe Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As LongPtr) As Long
16+
Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongPtr
17+
Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
18+
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
19+
Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
20+
Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As LongPtr, ByVal lpString2 As LongPtr) As Long
21+
22+
Public Sub SetClipboard(sUniText As String)
23+
Dim iStrPtr As LongPtr
24+
Dim iLen As Long
25+
Dim iLock As LongPtr
26+
Const GMEM_MOVEABLE As Long = &H2
27+
Const GMEM_ZEROINIT As Long = &H40
28+
Const CF_UNICODETEXT As Long = &HD
29+
OpenClipboard 0&
30+
EmptyClipboard
31+
iLen = LenB(sUniText) + 2&
32+
iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen)
33+
iLock = GlobalLock(iStrPtr)
34+
lstrcpy iLock, StrPtr(sUniText)
35+
GlobalUnlock iStrPtr
36+
SetClipboardData CF_UNICODETEXT, iStrPtr
37+
CloseClipboard
38+
End Sub
39+
40+
Public Function GetClipboard() As String
41+
Dim iStrPtr As LongPtr
42+
Dim iLen As Long
43+
Dim iLock As LongPtr
44+
Dim sUniText As String
45+
Const CF_UNICODETEXT As Long = 13&
46+
OpenClipboard 0&
47+
If IsClipboardFormatAvailable(CF_UNICODETEXT) Then
48+
iStrPtr = GetClipboardData(CF_UNICODETEXT)
49+
If iStrPtr Then
50+
iLock = GlobalLock(iStrPtr)
51+
iLen = CLng(GlobalSize(iStrPtr)) ' GlobalSize returns a Long Long (LongPtr) so cast to Long
52+
sUniText = String$(iLen \ 2& - 1&, vbNullChar)
53+
lstrcpy StrPtr(sUniText), iLock
54+
GlobalUnlock iStrPtr
55+
End If
56+
GetClipboard = sUniText
57+
End If
58+
CloseClipboard
59+
End Function
60+

src/Functions.bas

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
Attribute VB_Name = "Functions"
2+
Option Explicit
3+
4+
5+
'from https://stackoverflow.com/questions/62172551/error-with-createpipe-in-vba-office-64bit
6+
7+
8+
Public Declare PtrSafe Function CreatePipe Lib "kernel32" ( _
9+
phReadPipe As LongPtr, _
10+
phWritePipe As LongPtr, _
11+
lpPipeAttributes As SECURITY_ATTRIBUTES, _
12+
ByVal nSize As Long) As Long
13+
14+
Public Declare PtrSafe Function ReadFile Lib "kernel32" ( _
15+
ByVal hFile As LongPtr, _
16+
ByVal lpBuffer As String, _
17+
ByVal nNumberOfBytesToRead As Long, _
18+
lpNumberOfBytesRead As Long, _
19+
ByVal lpOverlapped As Any) As Long
20+
21+
Public Declare PtrSafe Function CreateProcessA Lib "kernel32" ( _
22+
ByVal lpApplicationName As Long, _
23+
ByVal lpCommandLine As String, _
24+
lpProcessAttributes As Any, _
25+
lpThreadAttributes As Any, _
26+
ByVal bInheritHandles As Long, _
27+
ByVal dwCreationFlags As Long, _
28+
ByVal lpEnvironment As Long, _
29+
ByVal lpCurrentDirectory As Long, _
30+
lpStartupInfo As Any, _
31+
lpProcessInformation As Any) As Long
32+
33+
Public Declare PtrSafe Function CloseHandle Lib "kernel32" ( _
34+
ByVal hObject As LongPtr) As Long
35+
36+
Public Declare PtrSafe Function PeekNamedPipe Lib "kernel32" ( _
37+
ByVal hNamedPipe As LongPtr, _
38+
lpBuffer As Any, _
39+
ByVal nBufferSize As Long, _
40+
lpBytesRead As Long, _
41+
lpTotalBytesAvail As Long, _
42+
lpBytesLeftThisMessage As Long) As Long
43+
44+
45+
Declare PtrSafe Function WriteFile Lib "kernel32" ( _
46+
ByVal hFile As LongPtr, _
47+
ByRef lpBuffer As Any, _
48+
ByVal nNumberOfBytesToWrite As Long, _
49+
ByRef lpNumberOfBytesWritten As Long, _
50+
lpOverlapped As Long) As Long
51+
52+
Public Type SECURITY_ATTRIBUTES
53+
nLength As Long
54+
lpSecurityDescriptor As LongPtr
55+
bInheritHandle As Long
56+
End Type
57+
58+
59+
Public Type STARTUPINFO
60+
cb As Long
61+
lpReserved As LongPtr
62+
lpDesktop As LongPtr
63+
lpTitle As LongPtr
64+
dwX As Long
65+
dwY As Long
66+
dwXSize As Long
67+
dwYSize As Long
68+
dwXCountChars As Long
69+
dwYCountChars As Long
70+
dwFillAttribute As Long
71+
dwFlags As Long
72+
wShowWindow As Integer
73+
cbReserved2 As Integer
74+
lpReserved2 As LongPtr
75+
hStdInput As LongPtr
76+
hStdOutput As LongPtr
77+
hStdError As LongPtr
78+
End Type
79+
80+
'this is the structure to pass more than 3 fds to a child process
81+
82+
'see https://github.com/libuv/libuv/blob/v1.x/src/win/process-stdio.c
83+
Public Type STDIO_BUFFER
84+
number_of_fds As Long
85+
crt_flags(0 To 4) As Byte
86+
os_handle(0 To 4) As LongPtr
87+
End Type
88+
89+
' the fields crt_flags and os_handle must lie contigously in memory
90+
' i.e. should not be aligned to byte boundaries
91+
' you cannot define a packed struct in VBA
92+
' thats why we need to have a second struct
93+
94+
#If Win64 Then
95+
Public Type STDIO_BUFFER2
96+
number_of_fds As Long
97+
raw_bytes(0 To 44) As Byte
98+
End Type
99+
#Else
100+
Public Type STDIO_BUFFER2
101+
number_of_fds As Long
102+
raw_bytes(0 To 24) As Byte
103+
End Type
104+
#End If
105+
106+
Public Type PROCESS_INFORMATION
107+
hProcess As LongPtr
108+
hThread As LongPtr
109+
dwProcessId As Long
110+
dwThreadId As Long
111+
End Type
112+
113+
Public Const STARTF_USESTDHANDLES = &H100&
114+
Public Const NORMAL_PRIORITY_CLASS = &H20&
115+
Public Const STARTF_USESHOWWINDOW As Long = &H1&
116+
117+
' we need to move memory
118+
'Public Declare PtrSafe Function GetAddrOf Lib "kernel32" Alias "MulDiv" (ByVal nNumber As Any, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
119+
' This is the dummy function used to get the addres of a VB variable.
120+
121+
Public Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source As Any, ByVal Length As LongPtr)
122+
123+
124+
Private Declare PtrSafe Sub sleep2 Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
125+
126+
'Custom sleep function
127+
'change sleep period if processing is not robust
128+
Public Const cnlngSleepPeriod As Long = 1000
129+
130+
Public Sub Sleep(Optional dblFrac As Double = 1)
131+
DoEvents
132+
Call sleep2(cnlngSleepPeriod * dblFrac)
133+
DoEvents
134+
End Sub
135+
136+
137+
' returns [current (Now)] time as seconds since 1970, ie UnixTime
138+
Function UnixTime(Optional localDateTime As Variant) As Long
139+
If IsMissing(localDateTime) Then
140+
UnixTime = DateDiff("S", "1/1/1970", Now())
141+
Else
142+
UnixTime = DateDiff("S", "1/1/1970", CDate(localDateTime))
143+
End If
144+
End Function
145+
146+
147+
' returns True if no process found or successfully terminated process; false or error or user cancels
148+
Function TerminateProcess(ByVal ProcessName As String, Optional ByVal PromptBefore As Boolean = False) As Boolean
149+
On Error Resume Next ' for now ignore errors
150+
Dim processFound As Boolean: processFound = False
151+
Dim processes As Object
152+
Dim process As Object
153+
154+
Set processes = getObject("winmgmts:").ExecQuery("SELECT * FROM win32_process")
155+
156+
For Each process In processes
157+
If process.name = ProcessName Then
158+
processFound = True
159+
160+
If PromptBefore Then
161+
If MsgBox("Browser Automation requires that " & ProcessName & " be closed before initializing. " & vbCrLf & _
162+
"If you'll use Edge Automation routinely, it is suggested you use Chrome as your default browser. " & _
163+
"If you need help changing your default browser contact the macro administrator for assistance." & vbCrLf & vbCrLf & _
164+
"Click [OK] to terminate all Edge processes." & vbCrLf & _
165+
"Click [Cancel] to abort and ", vbOKCancel, "Edge Automation") = vbCancel Then
166+
'Abort runtime so user can finish up with their current Edge session(s)
167+
GoTo Cleanup ' returns False
168+
End If
169+
End If
170+
171+
TerminateProcess = (process.Terminate = 0)
172+
173+
Exit For
174+
End If
175+
Next
176+
177+
Cleanup:
178+
Set processes = Nothing
179+
Set process = Nothing
180+
TerminateProcess = TerminateProcess Or (Not processFound) ' successfully terminated process or no process found
181+
End Function
182+
183+

0 commit comments

Comments
 (0)