VC ++ monitors remote computer screens

Source: Internet
Author: User
Tags bmp image
VC ++ monitors remote computer screens
Author: anonymous nameArticleSource: daily hits:

669 updated on:
Summary : This article introduces a method to monitor remote computer screens through Socket network programming and screen capture technology.

Keywords: Socket; screen capture; remote monitoring; Network

Preface

In actual projects, there are often situations where the construction site and the control center are different, in this case, engineering and technical personnel usually shuttle between them to understand the remote construction site and correct control of the control center. Obviously, the efficiency of this work method is very low, and the powerful advantages of computer networks are not fully utilized, in fact, network programming allows technicians to monitor and control remote computers on the engineering site in the control center. Although there are many remote terminal control software such as "Super spyware" and "glacier" on the Internet, it cannot be guaranteed that there are no other backdoors during programming due to its black and soft nature, therefore, we should develop such software from the perspective of computer security. To prevent the technology described in this article from being used to create hacker software, this article will not describe the control part of remote terminals, but will focus on monitoring the remote computer screen interface. 1. transmission of data information over the network

Because the local computer monitors remote computers through the network, it is necessary to program the NIC to achieve smooth network communication of data between the two parties. The available solutions include sockets, mail slots, named pipelines, and so on. This article selects stream sockets with flexible development and applications as the basis for network communication. Considering the actual situation, the remote monitored host provides screen information services for the local monitored host at any time. Therefore, the entire system can be divided into two modules: server and client, the system runs on the remote host and the local monitoring host respectively. The client sends a connection request to the server. After the connection is established, the server regularly sends the remote screen information to the client, after receiving data from the server, the client displays the data on the local host.

The main process of Network Programming Using Streaming sockets can be expressed. Before using a socket, the server must first have a socket, which can be created using the socket () function:

Sock = socket (af_inet, sock_stream, 0 );

Af_inet and sock_stream specify the stream sockets that use the TCP/IP address family. The socket actually provides a communication port, which can communicate with any computer with a socket port. Once a new socket is obtained, BIND () should be used to associate the socket with a port on the local machine. Enter necessary information, such as the local port number and local host address, for a sockaddr_in structure that points to information containing the local IP address and port information, and use bind () identify the server process on the network:

Sockin_s.sin_family = af_inet;
Sockin_s.sin_addr.s_addr = 0;
Sockin_s.sin_port = htons (port );
BIND (sock, (lpsockaddr) & sockin_s, sizeof (sockin_s ));

After the listen () Listener is completed, you need to use accept () to wait for the connection from the receiving client. Because the function is blocked before the client applies for a connection, therefore, we need to open up a separate thread for it to avoid affectingProgramOverall:

Afxbeginthread (server, null); // create a new thread
......
Uint server (lpvoid)
{
Csurveillant_serverview * pview = (csurveillant_serverview *) (cframewnd *)
Afxgetapp ()-> m_pmainwnd)-> getactiveview ());
Int nlen = sizeof (sockaddr );
Pview-> newskt_s = accept (pview-> sock, (lpsockaddr) & pview-> sockin_s, (lpint) & nlen );
Wsaasyncselect (pview-> newskt_s, pview-> m_hwnd, wm_msg, fd_close );
Pview-> settimer (0, 2500, null );
Return 1;
}

Here, the wsaasyncselect () asynchronous selection function is used to asynchronously respond to the network event fd_close of interest and customize the wm_msg message when the event occurs, by responding to this message, you can obtain that the client program currently in contact with the server has been closed and exited. Because the server is partly running on the remote engineering site, in order to enable the Monitoring Program (customer) of the Control Center) when a monitoring request is sent to provide services, You need to disable the new socket newskt_s generated by accept () in the message response function of wm_msg, and restart the thread to wait for the monitoring program to connect again. After the accept () function returns successfully, you can use the send () function in the timer response function to send the captured remote screen information to the monitoring host that has established a connection with it.

As a customer's Monitoring Program, the implementation process is much simpler than the server. To receive data, set the network events to be monitored in the asynchronous selection function to fd_close and fd_read. In the message response function, you can identify the specific network event and respond to it by determining the low byte of the message parameter. The following are the main components of the monitoring program network.Code:

......
Ipaddr = inet_addr (strip );
Sock = socket (af_inet, sock_stream, 0); // create a socket
Sockin_c.sin_family = af_inet;
Sockin_c.sin_addr.s_un.s_addr = ipaddr;
Sockin_c.sin_port = m_port;
Connect (sock, (lpsockaddr) & sockin_c, sizeof (sockin_c); // connect to the server
......
Wsaasyncselect (sock, m_hwnd, wm_msg, fd_read | fd_close );
......

By setting the asynchronous selection function, the wm_msg message is triggered by fd_read when data arrives, and the screen information of the remote host is received from the network to the cache by calling Recv () in the processing function, and re-display on the local machine. After completing the preceding steps, you have initially achieved network communication between the remote server and the local client to complete the network transfer task of screen information.

VC ++ monitors remote computer screens
Author: anonymous author: daily hits:

670 updated on:

Capture and display remote computer screens

The previous work only provides a low-layer network data communication capability for the entire monitoring system. It can also be said that it provides a communication channel for the on-site host and monitoring center. As for the central topic of this article-remote monitoring, screen capture and information reproduction must be completed in the field host and monitoring center. Screen capturing can first obtain the desktop window pointer and establish a compatible device environment, create a memory bitmap compatible with the desktop window pointer and copy the screen image to the newly created bitmap as a bitmap:

Char dot [1572864]; // 1024*768*2
Cbitmap BMP; // memory bitmap
Cdc wdc; // device Environment
CDC * PDC; // The device environment pointer pointing to the desktop window
......
Static cwindowdc DDC (getdesktopwindow (); // reference the desktop window pointer definition object DDC
PDC = & DDC; // point the pointer PDC to DDC
WDC. createcompatibledc (PDC); // establish a device environment compatible with DDC
BMP. createcompatiblebitmap (PDC, 1024,768); // create a DDC-compatible bitmap
WDC. SelectObject (& BMP); // select BMP
......
WDC. bitblt (1024,768, PDC, srccopy); // copy the desktop image to the BMP of WDC.

At this time, although the screen information is obtained and copied to the memory bitmap, it cannot be directly sent at this time. You need to call the cbitmap class member function getbitmapbits () to copy the image information from the memory bitmap to the cache, and send the screen information stored in the cache from the host to the control center through the network through the send () function of the socket.

The reproduction of the screen information of the on-site host in the control center is basically the inverse process of screen capture: first, establish a device environment related to the customer zone and establish a compatible device environment, create a memory-compatible bitmap in the memory according to the bitmap format. After receiving screen information from the network, copy the cached screen information to the memory bitmap in the bitmap format through the cbitmap member function setbitmapbits (), and finally display the memory bitmap. The main process is as follows:

CDC * PDC = getdc (); // reference the user window pointer definition object PDC
WDC. createcompatibledc (PDC); // create a device context compatible with PDC
BMP. createcompatiblebitmap (PDC, 1024,768); // creates a PDC-compatible bitmap.
WDC. SelectObject (& BMP );
......
Ireadlen = Recv (sock, buffer, 60000,0); // receives data from the network
For (I = 0; I {
Dot [pointer] = buffer [I];
Pointer ++;
If (pointer = 1572864) // determines whether the received information is full.
{
Getclientrect (& rect );
BMP. setbitmapbits (1572864, (lpvoid) DOT); // copy the memory data to BMP.
// Copy the BMP image to the user window
PDC-> stretchblt (1024,768, rect. Width (), rect. Height (), & WDC, srccopy );
Pointer = 0; // After receiving a screen, the pointer is reset to receive the next screen.
}
}

Automatic service program loading and Expansion

In terms of functions, the server program is only responsible for providing services to remote customers. During full operation, there is no need for external intervention. Therefore, you can hide the interface and make it a background service program:

Bool cmainframe: precreatewindow (createstruct & CS)
{
......
CS. Cx = 200;
CS. Cy = 10;
CS. Style = ws_popup;
CS. dwexstyle | = ws_ex_toolwindow;
Return true;
}

In addition, many computers now have the function of Remote Wake-up through MODEM. Therefore, if the service program has the self-starting function, the remote on-site host will be unattended. There are multiple ways to enable automatic startup: Add startup commands to files such as autoexec. BAT and win. ini, add shortcuts to programs in the "Start" menu, and modify the registry. Because the registry is usually slightly changed by humans, it is safer to enable auto-start by modifying the registry. The method used in this article is: first copy the service program to the system directory through the API function copyfile (), and then write a string key value to the Software \ Microsoft \ Windows \ CurrentVersion \ Run of HKEY_LOCAL_MACHINE, the content of this key value is the full path of the service program in the system directory:

DWORD type = REG_SZ;
DWORD size = max_path;
Lpctstr rgspath = "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run ";
......
Getsystemdirectory (syspath, size); // obtain the system directory
Getmodulefilename (null, currentpath, size); // get the program path
Filecurrentname = currentpath;
Filenewname = lstrcat (syspath, "\ surveillant.exe ");
Ret = copyfile (filecurrentname, filenewname, true); // copy the program to the system directory
......
// Open the Registry
Ret = regopenkeyex (HKEY_LOCAL_MACHINE, rgspath, 0, key_write, & hkey );
......
// Write to the Registry
Ret = regsetvalueex (hkey, "surveillant", null, type, filenewname, size );
......
// Close the registry
Regclosekey (hkey );

As for the remote control of the on-site host in the monitoring center, the data used to identify the message is sent to the other program and sendmessage () is used after the remote host receives the message () createProcess (); can be used to start the program of the on-site host to respond to the message. This article cannot be further described as this technology can also be used to compile hacker software.

Summary:

this article mainly introduces in detail the design and implementation of low-layer network communication modules based on stream sockets and screen capture and restoration technologies based on this module. The monitoring system described in this article has achieved good results in practical applications. In this way, engineers and technicians can learn about the dynamic display of indicator charts on the computer screen at the project site in the control center, and make timely decisions based on the monitoring results. The program described in this article is compiled by Microsoft Visual C ++ under Windows 2000 Professional.

Related Article

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.