Visual C # implement the Windows Messenger Service

Source: Internet
Author: User
Currently, many network management software are capable of transmitting information on the network in real time. Although some network communication software are powerful, some software can not only transmit text information, but also binary files. However, they all have an insurmountable disadvantage, that is, distribution is difficult. Both Computers of information transmission must install the client and server software of the communication software, information can be transmitted only when the software is enabled by both parties. However, messenger communication can overcome these shortcomings. Because the messenger service is built on Windows 2000 and later, the default status of this service is enabled after Windows 2000 and later are installed. Therefore, as long as two computers on the network are installed on Windows 2000 or later systems and no other software is required, the information can be transmitted through this service. Of course, this kind of network information transmission and distribution is inherently inadequate, that is, only text information can be transmitted. If you need to transmit complex information or data such as binary files, you cannot use this method.

1. Brief introduction to the Windows Messenger Service and its usage:

First, go to "Control Panel", select "service" in "Administrative Tools", and you will be able to get the interface 01, as shown in Figure 01 is the Windows Service Running window:


Figure 01: computer windows "service" running interface

The name of the Messenger Service in Windows is "Messager ". The Windows operating system defines this service as "sending and receiving messages delivered by the system administrator or" alarm "service. "The Messenger Service is started by default. If you find it in the" STOPPED "or" disabled "status, start it becauseProgramIf the service is in the "started" status.

In Windows, you can also send information through the messenger service. The procedure is as follows:

1. Select "my computer" on "desktop", right-click, and choose "manage" from the shortcut menu. The "Computer Management" dialog box is displayed:

2. open shared folder, select share, right-click, and select send console message from all tasks in the pop-up menu, as shown in 02, after completing the preceding operations, you can get the page shown in 03:


Figure 02: using the "courier service" to implement one of the information sending Interfaces

3. on the page shown in Figure 03, click "add", enter the IP address or host name of the computer to be sent, and enter the information to be sent in the "message" text box, click "send" to send the message to the other party through the messenger service.


Figure 03: use the "courier service" to implement one of the information sending Interfaces

The above are the specific steps to use Windows's own messenger service to send information.

Ii. Visual C # key technologies for network information transmission through the Messenger Service:

Although. net Framework SDK provides many class libraries for developing network programs, but these class libraries do not provide methods to call the information history service. Therefore, to call the information history method, only use the winapi function, this winapi function netmessagebuffersend is located in the "netapi32.dll" file. The following describes how to declare the netmessagebuffersend function in Visual C:

[Dllimport ("netapi32", charset = charset. Unicode)]
Public static extern int netmessagebuffersend (
String servername, // server name, null
String fromname, // receiver name, which can be an IP address or computer name
String msgname, // information name, null
String Buf, // Information
Int buflen); // information Length

Enter the corresponding receiver name and information content according to the netmessagebuffersend function parameters. It can be seen that netmessagebuffersend is very simple to use. The following describes in detail how Visual C # transfers network information through the history service.

Iii. Environment for program design, debugging, and running in this article:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2003 enterprise build,. NET Framework SDK 1.1 version 4322.

Iv. Visual C # specific steps for transmitting network information through the message history service:

The following are the specific steps for Visual C # To transfer network information through the history service:

1. Start Visual Studio. NET.

2. Select File, new, and project. The new project dialog box is displayed.

3. Set project type to Visual C # project ].

4. Set template to Windows application ].

5. In the Name text box, enter Visual C # To implement the messenger ].

6. Enter the "E: \ vs. Net Project" in the "location" text box, and click "OK. In this case, in "E: \. the. Net project "directory creates a folder named" Visual C # implement messenger ", which stores all files of the" Visual C # implement messenger "project.

7. set Visual Studio. switch the current window of net to the form1.cs window, drag the following components to the design form from the Windows Forms components tab in the toolbox, and perform the corresponding operations:

Two lable components.

The two textbox components are used to enter the recipient's IP address or computer name and send information content respectively.

A button, and double-click the components after they are dragged into the design window. The system will generate the corresponding processing for the Click Event of the component in form1.cs.Code.

8. set Visual Studio. the current window of net switches to the code editing window of form1.cs. In the code section of the form1.cs header introducing the namespace, replace the imported namespace code automatically generated by the system in form1.cs with the following code:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;
// Declare that the winapi function must use this namespace

9. The following code replaces the initializecomponent process generated by the system. The following code initializes the components added to the form and the created global variables and defines the click events of a button component:

Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. textbox2 = new system. Windows. Forms. Textbox ();
This. button1 = new system. Windows. Forms. Button ();
This. label1 = new system. Windows. Forms. Label ();
This. label2 = new system. Windows. Forms. Label ();
This. suspendlayout ();
This. textbox1.location = new system. Drawing. Point (124, 58 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (212, 21 );
This. textbox1.tabindex = 0;
This. textbox1.text = "";
This. textbox2.location = new system. Drawing. Point (124,126 );
This. textbox2.multiline = true;
This. textbox2.name = "textbox2 ";
This. textbox2.size = new system. Drawing. Size (212, 82 );
This. textbox2.tabindex = 1;
This. textbox2.text = "";
This. button1.location = new system. Drawing. Point (122,232 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (106, 36 );
This. button1.tabindex = 3;
This. button1.text = "send ";
This. button1.click + = new system. eventhandler (this. button#click );
This. label1.location = new system. Drawing. Point (8, 66 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (132, 23 );
This. label1.tabindex = 4;
This. label1.text = "IP address or computer name :";
This. label2.location = new system. Drawing. Point (78,134 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (46, 23 );
This. label2.tabindex = 5;
This. label2.text = "content :";
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (356,297 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textbox2 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. label2 );
This. Controls. Add (this. label1 );
This. formborderstyle = system. Windows. Forms. formborderstyle. fixedsingle;
This. maximizebox = false;
This. Name = "form1 ";
This. Text = "Visual C # implement communication messenger ";
This. resumelayout (false );
}

Now, the interface design and function implementation of the [Visual C # implement messenger] project have been completed, as shown in Figure 04:


Figure 04: design interface of the [Visual C # implement messenger] project

10. add the following code after the main process in form1.cs. The following code defines the click event of button1 and calls the declared netmessagebuffersend function in this event, send the information to the specified network computer through the history service:

Private void button#click (Object sender, system. eventargs E)
{
Byte [] bbuffer = system. Text. encoding. Unicode. getbytes (textbox2.text );
Int nret = netmessagebuffersend (null, textbox1.text, null, textbox2.text, textbox2.text. length * 2 + 2 );
}

11. After adding the click event of button1, add the following code. The following Code declares the netmessagebuffersend function:

[Dllimport ("netapi32", charset = charset. Unicode)]
Public static extern int netmessagebuffersend (
String servername, // server name, null
String fromname, // receiver name, which can be an IP address or computer name
String msgname, // information name, null
String Buf, // Information
Int buflen); // information Length

So far, after the above steps are completed correctly and all are saved, all the work of the [Visual C # implementation messenger] project is completed. Click the shortcut key [F5] to run the program. In the [IP address or computer name:] text box, enter the IP address or computer name of the other Party. In the [content:] In the text box, enter the information to be sent and click send. Then, the program sends the entered information to the specified network computer.

V. Summary:

Visual C # The Key to transmitting network information through the message history service is to understand and master the Declaration and call methods of the netmessagebuffersend function in Visual C #, though. net has been launched for more than three years, but there are still a lot of imperfections. Sometimes we need to use com, and sometimes we need to use winapi functions to Solve the Problem smoothly. This article is a typical example. I think over time ,. net Framework SDK will be more complete, and netmessagebuffersend and other winapi functions will certainly find corresponding positions in it, which reduces the difficulty of the programmer's work and brings higher stability to the program.

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.