Visual C # implements the Windows Messenger Service (2)

Source: Internet
Author: User

Http://ike.126.com

three. The environment in which the program is designed , debugged, and run 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 network information transmission of the specific steps:

The following are the steps of Visual C # to realize the 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 perform the appropriate actions:

Two lable components.

Two textbox components that are used to enter the recipient's IP address or computer name and to send the message content.

button buttons and double-click them after the component is dragged into the design window, the processing code corresponding to this component Click event is generated in Form1.cs.

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 introduction of the 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 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.

Http://ike.126.com

Keywords: System Management network management design software sharing server operating system Windows OS IE

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.