2023-02-11, 19:36:49
You can also take some idea in the code in CScript to look the msedgewebview2.exe loaded and kiill if you want.
Code:
' Test de AntView ocx with CScript.
' This code is a basic sample.
' Defaults :
' 1 - Can not use the default webview2 folder ( CreateWebView() failed ).
' 2 - Error "violation memory" in line :
' Set EdgeWebBrowser = Wscript.CreateObject( "AntViewAx.Antview", "EdgeWebBrowser_" )
' but works.
' Using 2023-02-10 :
' - Windows 10 up to date.
' - AntWiew ocx 1.1.236.
option explicit
' Run in CScript if it is running in VBScript to have the console.
If InStr( LCase( WScript.FullName ), "cscript.exe" ) = 0 Then
CreateObject( "WScript.Shell" ).Run "cscript.exe //NoLogo """ & WScript.ScriptFullName & ""
WScript.Quit
End If
Dim aUrls ' Array to choice on URL to use.
aUrls = Array( _
"https://www.cabinet-louis-reynaud.eu/index.php/clr-labs-3/" _
, "https://antview.dev/" _
, "https://www.google.fr/" _
)
Const ForWriting = 2
Const TristateTrue = -1
Public pu_cHtml : pu_cHtml = bv_DirBase() & "\_Result_html.txt" ' TXT source file created.
Public pu_cPdf : pu_cPdf = bv_DirBase() & "\_Result_pdf.pdf" ' PDF file created.
Public pu_cProp : pu_cProp = bv_DirBase() & "\_Result_Properties.txt" ' TXT EdgeWebBrowser properties file created.
Public pu_cWeb : pu_cWeb = "c:\Url_UserDataFolder" ' Webview2 files folder.
Public pu_nSpace : pu_nSpace = 37 ' Spaces in the EdgeWebBrowser properties functions.
Public pu_nEvent ' Indicate when the event function is finish.
' 0 = In work.
' 1 = Finshed ok.
' 2 = Finished fail.
Dim cJscript ' Script to execute.
Dim cPids ' String for the msedgewebview2.exe old running.
Dim cProperties ' Variable for the EdgeWebBrowser properties
Dim cUrl ' URL to use.
Dim Document ' AntViewAx.AntViewDocument object.
Dim EdgeWebBrowser ' AntViewAx.Antview object.
Dim xChoice ' Choice of the URL to use.
Dim i ' Count variable.
bv_QOut( "AntView activeX test in CScript." )
bv_QOut( "" )
bv_QOut( "Old msedgewebview2.exe running PID(s) before kill :" )
cPids = bv_GetAllPidForExeRun( "msedgewebview2.exe" )
bv_QOut( cPids )
If Len( cPids ) > 0 Then
i = MsgBox( "Do you want to kill the old(s) msedgewebview2.exe ?", vbYesNo + vbQuestion, "Question" )
If i = vbYes Then
bv_QOut( "" )
bv_QOut( bv_KillAllExeByPids( cPids ) )
bv_QOut( "" )
bv_QOut( "Old msedgewebview2.exe running PID(s) after killed :" )
cPids = bv_GetAllPidForExeRun( "msedgewebview2.exe" )
bv_QOut( cPids )
bv_QOut( "" )
End If
End If
bv_QOut( "Select an URL to read :" )
bv_QOut( "" )
For i = LBound( aUrls ) to UBound( aUrls ) Step 1
bv_QOut( i & "->" & aUrls( i ) )
Next
bv_QQOut( "Select your URL : " )
xChoice = WScript.StdIn.ReadLine
xChoice = CLng( xChoice )
If xChoice < LBound( aUrls ) Or xChoice > UBound( aUrls ) Then
msgbox "Bad choice in select URL, abort."
wscript.Quit
End If
bv_QOut( "" )
bv_QOut( "URL selected :" )
cUrl = aUrls( xChoice )
bv_QOut( cUrl )
bv_QOut( "" )
' Erase the old files.
bv_fErase( pu_cPdf )
bv_fErase( pu_cHtml )
bv_fErase( pu_cProp )
' Create a directory for the user datas, not works with the default folder.
bv_Makedir( pu_cWeb )
bv_QOut( "Error Violation memory, no exception, click on << OK >>" )
bv_QOut( "" )
On Error Resume Next
Set EdgeWebBrowser = Wscript.CreateObject( "AntViewAx.Antview", "EdgeWebBrowser_" )
If Err.Number <> 0 Then
MsgBox( "***Error = " & Err.Number & " Description = " & Err.Description )
Err.Clear
wscript.Quit
End If
On Error GoTo 0
' CreateWebView() failed if use defaut directory.
EdgeWebBrowser.UserDataFolder = pu_cWeb
bv_QOut( "Create the WebView." )
EdgeWebBrowser.CreateWebView
For i = 1 to 60 Step 1
if EdgeWebBrowser.WebViewCreated Then Exit For
bv_Sleep( 1 )
Next
If not EdgeWebBrowser.WebViewCreated Then
Set EdgeWebBrowser = Nothing
MsgBox "WebViewCreated() fail !"
wscript.Quit
End If
Set Document = Wscript.CreateObject( "AntViewAx.AntViewDocument", "Document_" )
Document.BrowserDispatch( EdgeWebBrowser.IDispatchPointer )
bv_QOut( "Open the URL." )
bv_QOut( "" )
pu_nEvent = 0
EdgeWebBrowser.Navigate( cUrl )
For i = 1 to 60 Step 1
If pu_nEvent <> 0 Then Exit For
bv_Sleep( 1 )
Next
If pu_nEvent <> 1 Then
MsgBox "Navigate() fail !"
EdgeWebBrowser.CloseBrowserProcess
EdgeWebBrowser.CloseWebView
Set EdgeWebBrowser = Nothing
Set Document = Nothing
wscript.quit
End If
bv_QOut( "New msedgewebview2.exe running PID(s) after loading :" )
cPids = bv_GetAllPidForExeRun( "msedgewebview2.exe" )
bv_QOut( cPids )
bv_QOut( "" )
bv_QOut( "Manage cookies." )
' Thanks to Wila : https://forums.antwise.com/showthread.php?tid=147
If cUrl = "https://www.cabinet-louis-reynaud.eu/index.php/clr-labs-3/" Then
cJscript = "var button = document.getElementsByClassName(""cmplz-accept"");" & vbCrLf
cJscript = cJscript & "button[0].click();"
ElseIf cUrl = "https://www.google.fr/" then
cJscript = "var button = document.getElementsByClassName(""QS5gu sy4vM"");" & vbCrLf
cJscript = cJscript & "button[1].click();" ' button[0] = not accept.
End If
If Not IsEmpty( cJscript ) Then
pu_nEvent = 0
EdgeWebBrowser.ExecuteScript cJscript
For i = 1 to 60 Step 1
If pu_nEvent <> 0 Then Exit For
bv_Sleep( 1 )
Next
bv_Sleep( 1 )
End If
bv_QOut( "Print to PDF." )
pu_nEvent = 0
EdgeWebBrowser.PrintToPdf pu_cPdf, ""
For i = 1 to 60 Step 1
If pu_nEvent <> 0 Then Exit For
bv_Sleep( 1 )
Next
bv_QOut( "Get HTML source." )
pu_nEvent = 0
Document.RequestCurrentHtml()
For i = 1 to 60 Step 1
If pu_nEvent <> 0 Then Exit For
bv_Sleep( 1 )
Next
bv_QOut( "List EdgeWebBrowser properties." )
cProperties = "EdgeWebBrowser properties." & vbCrLf
cProperties = cProperties & "!" & String( pu_nSpace , "-" )& "!-------------!" & vbCrLf
cProperties = cProperties & "!Name" & String( pu_nSpace - 4, " ") & "!Type !Value" & vbCrLf
cProperties = cProperties & "!" & String( pu_nSpace , "-") & "!-------------!" & vbCrLf
On Error Resume Next : cProperties = cProperties & WriteProperty( "Active" , EdgeWebBrowser.Active ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AdditionalBrowserArguments" , EdgeWebBrowser.AdditionalBrowserArguments ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AlignDisabled" , EdgeWebBrowser.AlignDisabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AlignWithMargins" , EdgeWebBrowser.AlignWithMargins ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AllowSingleSignOnUsingOSPrimaryAccount", EdgeWebBrowser.AllowSingleSignOnUsingOSPrimaryAccount ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AlignDisabled" , EdgeWebBrowser.AlignDisabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AutoScroll" , EdgeWebBrowser.AutoScroll ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AutoSize" , EdgeWebBrowser.AutoSize ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "AxBorderStyle" , EdgeWebBrowser.AxBorderStyle ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "BorderWidth" , EdgeWebBrowser.BorderWidth ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "BrowserAcceleratorKeysEnabled" , EdgeWebBrowser.BrowserAcceleratorKeysEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "BrowserProcessID" , EdgeWebBrowser.BrowserProcessID ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "BrowserVersionString" , EdgeWebBrowser.BrowserVersionString ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "BuiltInErrorPageEnabled" , EdgeWebBrowser.BuiltInErrorPageEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "CanGoBack" , EdgeWebBrowser.CanGoBack ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "CanGoForward" , EdgeWebBrowser.CanGoForward ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Caption" , EdgeWebBrowser.Caption ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Color" , EdgeWebBrowser.Color ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ContainsFullScreenElement" , EdgeWebBrowser.ContainsFullScreenElement ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Controller" , EdgeWebBrowser.Controller ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "CreateWebViewOnCreate" , EdgeWebBrowser.CreateWebViewOnCreate ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "CurrentPPI" , EdgeWebBrowser.CurrentPPI ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DefaultContextMenusEnabled" , EdgeWebBrowser.DefaultContextMenusEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DefaultInterface" , EdgeWebBrowser.DefaultInterface ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DefaultScriptDialogsEnabled" , EdgeWebBrowser.DefaultScriptDialogsEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DefaultUserDataFolderLocation" , EdgeWebBrowser.DefaultUserDataFolderLocation ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DemoDaysLeft" , EdgeWebBrowser.DemoDaysLeft ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DevToolsEnabled" , EdgeWebBrowser.DevToolsEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DockSite" , EdgeWebBrowser.DockSite ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DocumentTitle" , EdgeWebBrowser.DocumentTitle ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DoubleBuffered" , EdgeWebBrowser.DoubleBuffered ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DownloadDialogCornerAlignment" , EdgeWebBrowser.DownloadDialogCornerAlignment ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "DropTarget" , EdgeWebBrowser.DropTarget ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Enabled" , EdgeWebBrowser.Enabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Environment" , EdgeWebBrowser.Environment ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "EventsUseHexadecimal" , EdgeWebBrowser.EventsUseHexadecimal ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ExplicitHeight" , EdgeWebBrowser.ExplicitHeight ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ExplicitLeft" , EdgeWebBrowser.ExplicitLeft ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ExplicitTop" , EdgeWebBrowser.ExplicitTop ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ExplicitWidth" , EdgeWebBrowser.ExplicitWidth ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Font" , EdgeWebBrowser.Font ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "GeneralAutofillEnabled" , EdgeWebBrowser.GeneralAutofillEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "HelpFile" , EdgeWebBrowser.HelpFile ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "IDispatchPointer" , EdgeWebBrowser.IDispatchPointer ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "KeyPreview" , EdgeWebBrowser.KeyPreview ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Language" , EdgeWebBrowser.Language ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "LastErrorCode" , EdgeWebBrowser.LastErrorCode ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "LastErrorMessage" , EdgeWebBrowser.LastErrorMessage ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "MouseInClient" , EdgeWebBrowser.MouseInClient ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "NextFocusWindowHandle" , EdgeWebBrowser.NextFocusWindowHandle ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "NextFocusWindowHandleUInt" , EdgeWebBrowser.NextFocusWindowHandleUInt ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PasswordAutosaveEnabled" , EdgeWebBrowser.PasswordAutosaveEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PixelsPerInch" , EdgeWebBrowser.PixelsPerInch ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PopupMode" , EdgeWebBrowser.PopupMode ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PreviousFocusWindowHandle" , EdgeWebBrowser.PreviousFocusWindowHandle ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PreviousFocusWindowHandleUInt" , EdgeWebBrowser.PreviousFocusWindowHandleUInt ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "PrintScale" , EdgeWebBrowser.PrintScale ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Scaled" , EdgeWebBrowser.Scaled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ScaleFactor" , EdgeWebBrowser.ScaleFactor ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ScreenSnap" , EdgeWebBrowser.ScreenSnap ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ScriptEnabled" , EdgeWebBrowser.ScriptEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Settings" , EdgeWebBrowser.Settings ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "SizeRatio" , EdgeWebBrowser.SizeRatio ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "SnapBuffer" , EdgeWebBrowser.SnapBuffer ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "Source" , EdgeWebBrowser.Source ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "StatusBarEnabled" , EdgeWebBrowser.StatusBarEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "SynchronousTimeOut" , EdgeWebBrowser.SynchronousTimeOut ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "TargetCompatibleBrowserVersion" , EdgeWebBrowser.TargetCompatibleBrowserVersion ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "UseDockManager" , EdgeWebBrowser.UseDockManager ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "UserAgent" , EdgeWebBrowser.UserAgent ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "UserDataFolder" , EdgeWebBrowser.UserDataFolder ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "VersionString" , EdgeWebBrowser.VersionString ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "WebMessageEnabled" , EdgeWebBrowser.WebMessageEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "WebResourceResponseReceivedEnabled" , EdgeWebBrowser.WebResourceResponseReceivedEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "WebView2LoaderPath" , EdgeWebBrowser.WebView2LoaderPath ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "WebViewCreated" , EdgeWebBrowser.WebViewCreated ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "WindowClosedRequestEnabled" , EdgeWebBrowser.WindowClosedRequestEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ZoomControlEnabled" , EdgeWebBrowser.ZoomControlEnabled ) : Err.Clear : On Error GoTo 0
On Error Resume Next : cProperties = cProperties & WriteProperty( "ZoomFactor" , EdgeWebBrowser.ZoomFactor ) : Err.Clear : On Error GoTo 0
cProperties = cProperties & "!" & String( pu_nSpace , "-") & "!-------------!" & vbCrLf
bv_MemoWrit pu_cProp, cProperties
bv_QOut( "Close the browser and clear memory." )
bv_QOut( "" )
EdgeWebBrowser.CloseBrowserProcess
EdgeWebBrowser.CloseWebView
Set EdgeWebBrowser = Nothing
Set Document = Nothing
bv_Sleep( 1 ) ' Delay to close the browser.
bv_QOut( "New msedgewebview2.exe running PID(s) after close the avtiveX :" )
cPids = bv_GetAllPidForExeRun( "msedgewebview2.exe" )
bv_QOut( cPids )
bv_QOut( "" )
bv_QOut( "End of script." )
MsgBox "End of script, look the created files."
' ***** Function *****
Function WriteProperty( cName, xProperty )
Dim cStringe ' String created.
cStringe = "!" & cName & String( pu_nSpace - Len( cName ), " ") & "!"
cStringe = cStringe & TypeName( xProperty ) & String( 13 - Len( TypeName ( xProperty ) ), " ") & "!"
WriteProperty = cStringe & xProperty & vbCrLf
End Function
' ***** Event functions *****
Sub Document_OnRequestCurrentHtml( cHtml )
pu_nEvent = 1
bv_MemoWrit pu_cHtml, cHtml
End Sub
Sub EdgeWebBrowser_OnExecuteScript( HResult, JsonObject )
If HResult = 0 Then
pu_nEvent = 1
Else
pu_nEvent = 2
MsgBox "OnExecuteScript() fail, HResult = ] " & HResult
End If
End Sub
Sub EdgeWebBrowser_OnNavigationCompleted( IsSuccess, WebErrorStatus, NavigationId )
If IsSuccess Then
pu_nEvent = 1
Else
pu_nEvent = 2
End If
End Sub
Sub EdgeWebBrowser_OnPrintToPdfCompleted( HResult, IsSuccessful )
If IsSuccessful Then
pu_nEvent = 1
Else
pu_nEvent = 2
MsgBox "OnPrintToPdfCompleted() fail, HResult = ] " & HResult
End If
End Sub
' ***** Used tools *****
' Get the default directory.
Function bv_DirBase()
bv_DirBase = Left( WScript.ScriptFullName, InStrRev( WScript.ScriptFullName, "\" ) )
End Function
' Erase a file.
Sub bv_fErase( cFile_bv )
Dim oFso_bv
Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
If oFso_bv.FileExists( cFile_bv ) Then
oFso_bv.DeleteFile cFile_bv
End If
Set oFso_bv = Nothing
End Sub
' Get a string with coma with the PIDs of an exe is running.
Function bv_GetAllPidForExeRun( cExe )
Dim cPids
Dim oPid
Dim oLocator
Dim oProcess
Dim oWMI
Set oLocator = CreateObject( "WbemScripting.SWbemLocator" )
Set oWMI = oLocator.ConnectServer( ".", "root\cimv2" )
oWMI.Security_.ImpersonationLevel = 3
Set oProcess = oWMI.ExecQuery( "Select * from Win32_Process where Name = '" & cExe & "'" )
cPids = ""
For each oPid in oProcess
cPids = cPids & Cstr( oPid.Handle ) & ","
Next
Set oLocator = Nothing
Set oProcess = Nothing
Set oWMI = Nothing
bv_GetAllPidForExeRun = cPids
End Function
' Kill all exe by pids ( paramater string of pids with coma ).
Function bv_KillAllExeByPids( cPids )
Dim cReturn
Dim cPid
Dim cString
Dim i
cString = cPids
cReturn = ""
Do While ( True )
i = InStr( cString, "," )
If i = 0 Then Exit Do
cPid = Left( cString, i - 1 )
If Len( cPid ) = 0 Then Exit Do
cReturn = cReturn & bv_KillExeByPid( cPid )
cString = Right( cString, Len( cString ) - i )
If Len( cString ) = 0 Then Exit Do
Loop
bv_KillAllExeByPids = cReturn
End Function
' Kill an exe with his PID.
Function bv_KillExeByPid( cPid )
Dim cReturn
Dim oPid
Dim oLocator
Dim oProcess
Dim oWMI
Set oLocator = CreateObject( "WbemScripting.SWbemLocator" )
Set oWMI = oLocator.ConnectServer( ".", "root\cimv2" )
oWMI.Security_.ImpersonationLevel = 3
Set oProcess = oWMI.ExecQuery( "Select * from Win32_Process where Handle = " & cPid )
cReturn = ""
For each oPid in oProcess
cReturn = cReturn & cPid & " "
On Error Resume Next
oPid.Terminate
If Err.Number <> 0 Then
cReturn = cReturn & "Not "
Err.Clear
End If
On Error GoTo 0
cReturn = cReturn & "Killed" & vbCrLf
Next
Set oLocator = Nothing
Set oProcess = Nothing
Set oWMI = Nothing
bv_KillExeByPid = cReturn
End Function
' Create a directory.
Sub bv_Makedir( cDir_bv )
Dim oFso_bv
Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
If Not oFso_bv.FolderExists( cDir_bv ) Then
oFso_bv.CreateFolder( cDir_bv )
End If
Set oFso_bv = Nothing
End Sub
' Write a string in a file.
Sub bv_MemoWrit( cFile_bv, cString_bv )
Dim oFile_bv
Dim oFso_bv
Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
If oFso_bv.FileExists( cFile_bv ) Then
oFso_bv.DeleteFile cFile_bv
End If
Set oFile_bv = oFso_bv.OpenTextFile( cFile_bv, ForWriting, True, TristateTrue )
oFile_bv.Write cString_bv
oFile_bv.Close
Set oFso_bv = Nothing
End Sub
' Write a line in the console.
Sub bv_QOut( cText )
WScript.StdOut.WriteLine cText
End Sub
' Write character(s) in the console.
Sub bv_QQOut( cText )
WScript.StdOut.Write cText
End Sub
' Pause in script in second(s).
Sub bv_Sleep( n )
WScript.Sleep Int( n * 1000 )
End Sub