本程序介紹如何在局域網內安裝了信使服務的Windows 2000計算機之間傳遞消息。
向窗體上添加兩個TLabel組件、兩個TEdit組件和一個TButton組件
首先聲明NetMessageBufferSend函數,該函數在netapi32.dll庫中:
type NET_API_STATUS = LongInt; function NetMessageBufferSend(servername: LPCWSTR; msgname: LPCWSTR; fromname: LPCWSTR; buf: Pointer; buflen: DWORD): NET_API_STATUS; stdcall;external ’netapi32.dll’;
在程序運行過程中,單擊Send按鈕,就會向Computer文本框指定的計算機發送Content文本框中輸入的消息,響應代碼如下:
procedure TForm1.Button1Click(Sender: TObject); var WideMsg:PWideChar; DestName:PWideChar; begin DestName:=PWideChar(WideString(Edit1.Text)); WideMsg:=PWideChar(WideString(Edit2.Text)); NetMessageBufferSend(nil,DestName,nil,WideMsg,Length(Edit2.Text)*2); end;
程序代碼如下:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type NET_API_STATUS = LongInt; function NetMessageBufferSend(servername: LPCWSTR; msgname: LPCWSTR; fromname: LPCWSTR; buf: Pointer; buflen: DWORD): NET_API_STATUS; stdcall;external ’netapi32.dll’; type TForm1 = class(TForm) Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var WideMsg:PWideChar; DestName:PWideChar; begin DestName:=PWideChar(WideString(Edit1.Text)); WideMsg:=PWideChar(WideString(Edit2.Text)); NetMessageBufferSend(nil,DestName,nil,WideMsg,Length(Edit2.Text)*2); end; end.
保存文件,然後按F9鍵運行程序,在Computer對應的文本框中輸入目的計算機名,在Content對應的文本框中輸入消息內容。
單擊Send按鈕,就會向指定的計算機發送消息,在接收消息的計算機上就會顯示一個對話框。