|
| 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