Visual C#实现Windows信使服务 (2)

    技术2022-05-11  123

    http://ike.126.com

    三.本文中的程序设计、调试和运行的环境:  (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函数需要使用到此命名空间  9. 以下面代码替代系统产生的InitializeComponent过程。下面代码是对加入窗体的组件以及创建的全局变量进行初始化和定义一个Button组件的Click事件: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 ) ;}  至此【Visual C#实现通讯信使】项目的界面设计和功能实现的前期工作就完成了,设计界面如图04所示:图04:【Visual C#实现通讯信使】项目的设计界面  10. 在Form1.cs中的Main过程之后添加下列代码,下列代码的作用是定义button1的Click事件,在此事件中调用申明的NetMessageBufferSend函数,把信息通过信史服务传送到指定的网络计算机上去: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. 在添加完button1的Click事件后,再添加下列代码,下列代码的作用是申明NetMessageBufferSend函数:[DllImport ( "Netapi32" , CharSet = CharSet.Unicode ) ] public static extern int NetMessageBufferSend (  string servername , //服务器名称,为NULL string fromname , //接收方名称,可为IP或计算机名称 string msgname , //信息名称,为NULL string buf , //信息 int buflen ) ; //信息长度  至此,在上述步骤都正确完成,并全部保存后,【Visual C#实现通讯信使】项目的全部工作就完成了。此时单击快捷键【F5】运行程序后,在【IP地址或计算机名:】文本框中输入对方的IP地址或计算机名,在【内容:】文本框中输入要传送的信息后,单击【发送】按钮,则程序就会把输入的信息传送到指定的网络计算机上了。  五.总结:  Visual C#通过信史服务实现网络信息传送的关键是要了解、掌握NetMessageBufferSend函数在Visual C#中的申明、调用方法,虽然.Net推出以及三年多了,但仍有很多不完善的地方,有时要借助于COM,有时要借助于WinAPI函数才能顺利解决,本文就是一个典型的例子。我想随着时间的推移,.Net FrameWork SDK一定会更加完善,NetMessageBufferSend等WinAPI函数一定也会在其中找到相应的位置,这样也就减少了程序员的工作难度,也给程序带来更高的稳定性。

    http://ike.126.com

    关键字:系统管理  网络管理  设计  软件  共享  服务器  操作系统  windows 2000  os  ie 


    最新回复(0)