[DllImport ( "Netapi32" , CharSet = CharSet.Unicode ) ]
public static extern int NetMessageBufferSend (
string servername , //服务器名称,为NULL
string fromname , //接收方名称,可为IP或计算机名称
string msgname , //信息名称,为NULL
string buf , //信息
int buflen ) ; //信息长度
对照NetMessageBufferSend函数参数分别输入相应的接收方名称和信息内容即可,可见NetMessageBufferSend的使用方法还是非常简单的。下面就来详细介绍Visual C#通过信史服务实现网络信息传送的具体实现方法。
三.本文中的程序设计、调试和运行的环境:
(1).微软公司视窗2000服务器版。
(2).Visual Studio .Net 2003企业构建版,.Net FrameWork SDK 1.1版本号4322。
四.Visual C#通过信史服务实现网络信息传送的具体实现步骤:
以下就是Visual C#通过信史服务实现网络信息传送的具体实现步骤:
1. 启动Visual Studio .Net。
2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。
3. 将【项目类型】设置为【Visual C#项目】。
4. 将【模板】设置为【Windows应用程序】。
5. 在【名称】文本框中输入【Visual C#实现通讯信使】。
6. 在【位置】的文本框中输入【E:/VS.NET项目】,然后单击【确定】按钮。这样在"E:/VS.NET项目"目录中就创建了一个名称为"Visual C#实现通讯信使"的文件夹,里面存放的就是"Visual C#实现通讯信使"项目的所有文件。
7. 把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往设计窗体中拖入下列组件,并执行相应操作:
二个Lable组件。
二个TextBox组件,分别用来输入接收方的IP地址或计算机名和发送信息内容。
一个Button按钮,并在这个组件拖入设计窗口后分别双击它们,则系统会在Form1.cs中分别产生这一个组件Click事件对应的处理代码。
8. 把Visual Studio .Net的当前窗口切换到Form1.cs的代码编辑窗口,在Form1.cs的首部的引入命名空间的代码区中,用下列代码替换Form1.cs中由系统自动产生的引入命名空间代码:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Runtime.InteropServices ;
//申明WinAPI函数需要使用到此命名空间
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 = "发送" ;
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地址或计算机名:" ;
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 = "内容:" ;
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#实现通讯信使" ;
this.ResumeLayout ( false ) ;
}
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 ) ;
}
[DllImport ( "Netapi32" , CharSet = CharSet.Unicode ) ]
public static extern int NetMessageBufferSend (
string servername , //服务器名称,为NULL
string fromname , //接收方名称,可为IP或计算机名称
string msgname , //信息名称,为NULL
string buf , //信息
int buflen ) ; //信息长度