Visual C # implements the Windows Messenger service

Source: Internet
Author: User
Tags interface key window visual studio
Visual|window now have a lot of network management software has the network information real-time transmission function, although some network communication software function is quite powerful, some software not only can transmit the text information, but also can transmit the binary file and so on. But they all have an insurmountable disadvantage, that is, distribution is more difficult, both computers need to install communication software client and server software, and only if both sides open the appropriate software, it is possible to carry out information transmission. Messenger communications can overcome these drawbacks because the Messenger service is self-contained above the Windows 2000 system, and the default state of the service after installing Windows 2000 is open. So as long as the network of two computers installed more than Windows 2000 system, no longer need to install additional software, you can use this service to achieve information transmission. Of course, this network information transmission and distribution is congenitally deficient, is only the transmission of text information, if you need to transfer the binary files and other complex information or data, you can not use this method.

   I. Brief introduction to the Messenger service of Windows and how to use it:

First you go to Control Panel, and after you select services in Administrative Tools, you get the interface shown in Figure 01, Figure 01 is the Windows Service running window:


Figure 01: The Computer Windows Services run interface
The service name that the Messenger service corresponds to in the Windows operating system is "Messager". The Windows operating system defines this service as a "send and receive message from a system administrator or an alarm service." "In the default state the Messenger service is started and if it is found to be in a" stop "or" disabled "state, start it, because one of the conditions described in the following procedure will work correctly even if the service is in the" Startup "state.

Using Windows systems, you can also send information through the Messenger service, as follows:

1. After selecting "My Computer" on "desktop", click the right mouse button and select "Manage" in the pop-up menu, pop-up "Computer Management" dialog box:

2. Open "Shared Folders", select "Share", click the right mouse button, in the pop-up menu select "All Tasks" in the "Send Console Message", as shown in Figure 02, when the above operation can be achieved as shown in Figure 03 interface:


Figure 02: One of the messaging interfaces implemented using the Messenger service
3. In the interface shown in Figure 03, click the Add button, enter the IP address or host name of the computer you want to send to, and then, when you enter the information you want to send in the Message text box, click the Send button, and the message is sent to each other via the Messenger service.


Figure 03: One of the messaging interfaces implemented using the Messenger service
These are the specific steps to use Windows ' own Messenger service to send information.

   two. Visual C # delivers key technologies for network messaging via Messenger services:

Although the. Net FrameWork SDK provides a number of class libraries for developing network programs, but they do not provide a way to invoke the service of the letter history, the WINAPI function is invoked only by using the WINAPI function, which netmessagebuffersend It is located in the "Netapi32.dll" file. Here's how the Netmessagebuffersend function is declared in Visual C #:

[DllImport ("Netapi32", CharSet = CharSet.Unicode)]
public static extern int Netmessagebuffersend (
string servername,//server name, NULL
String fromname,//receiver name, can be IP or computer name
String msgname,//information name, NULL
String buf,//Information
int buflen); Information length


In contrast to the Netmessagebuffersend function parameters, the corresponding receiver name and information content can be entered respectively, and the Netmessagebuffersend method is very simple. Here is a detailed description of Visual C # through the history of service to achieve network information transmission of the specific implementation methods.

Three. The 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 number 4322.

Four. Visual C # through the history of services to achieve the implementation of network Information transfer steps:

The following is the concrete implementation steps of Visual C # to realize network information transmission through the service of the History of letters:

1. Start visual Studio. Net.

2. When you select the menu "file" | "new" | "Project", The New Project dialog box pops up.

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

4. Set the template to Windows application.

5. In the Name text box, enter Visual C # implementing the Communication Messenger.

6. Enter the E:\VS in the text box in the location. NET project, and then click the OK button. So in "E:\VS." NET project directory, you create a folder called the Visual C # implementation communication Messenger, which holds all the files for the Visual C # implementation Communication Messenger project.

7. Take visual Studio. NET to the Form1.cs window, and from the Windows Forms Components tab in the Toolbox, drag the following components into the design form and do the appropriate action:

two lable components.

Two textbox components that are used to enter the IP address or computer name of the receiver, and to send the message content, respectively.

A button button that, when the component is dragged into the design window, double-clicks on each of them, the system will produce the processing code for this component click event in Form1.cs respectively.

8. Take visual Studio. NET's current window switches to the Form1.cs Code editing window, and in Form1.cs's header, the code area that introduces the namespace, replaces the introduced namespace code generated automatically 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 needs to use this namespace


9. Replace the system-generated InitializeComponent process with the following code. The following code initializes and defines a button component Click event for the component that joins the form and the global variables created:

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.button1_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 # implementation communication Messenger";
This. ResumeLayout (FALSE);
}


The preliminary work on the interface design and functionality implementation of the Visual C # Communication Messenger project is complete, as shown in Figure 04:


Figure 04: Design interface for the Visual C # implementation Communication Messenger project
10. After the main procedure in Form1.cs adds the following code, the following code is used to define the Button1 click event, in which the declared Netmessagebuffersend function is invoked, and the information is routed through the letter history service to the specified network computer:

private void Button1_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 Button1 Click event, add the following code, which is the function of the following code to affirm the Netmessagebuffersend function:

[DllImport ("Netapi32", CharSet = CharSet.Unicode)]
public static extern int Netmessagebuffersend (
string servername,//server name, NULL
String fromname,//receiver name, can be IP or computer name
String msgname,//information name, NULL
String buf,//Information
int buflen); Information length


At this point, all of the work on the Visual C # implementation Communication Messenger project is complete after all of the steps are completed correctly and saved. When you click the shortcut "F5" to run the program, in the IP address or computer name: text box, enter each other's IP address or computer name, in the Content: text box, after entering the information to be transferred, click the Send button, the program will send the information entered to the designated network computer.

   Five. Summary:

The key of Visual C # to realize the transmission of network information through the history service is to understand and master the method of affirming and invoking the Netmessagebuffersend function in Visual C #, although. NET launched and more than three years, but there are still many imperfect places, sometimes to rely on the use of COM, sometimes to the aid of WINAPI function can be successfully resolved, this article is a typical example. I think over time, the. Net FrameWork SDK will be more perfect, netmessagebuffersend and other WINAPI functions will also find the appropriate location, which also reduces the programmer's difficulty, but also to the program to bring higher stability.



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.