本程序介绍如何在局域网内安装了信使服务的Windows
计算机之间传递消息
向窗体上添加两个TLabel组件两个TEdit组件和一个TButton组件设计完成的主界面如图所示
图 主界面首先声明NetMessageBufferSend函数该函数在netapidll库中
type
NET_API_STATUS = LongInt;
function NetMessageBufferSend(servername: LPCWSTR; msgname: LPCWSTR;
fromname: LPCWSTR; buf: Pointer;
buflen: DWORD): NET_API_STATUS;
stdcall;external netapidll;
在程序运行过程中单击Send按钮就会向Computer文本框指定的计算机发送Content文本框中输入的消息响应代码如下
procedure TFormButtonClick(Sender: TObject);
var
WideMsg:PWideChar;
DestName:PWideChar;
begin
DestName:=PWideChar(WideString(EditText));
WideMsg:=PWideChar(WideString(EditText));
NetMessageBufferSend(nilDestNamenilWideMsgLength(EditText)*);
end;
程序代码如下
unit Unit;
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 netapidll;
type
TForm = class(TForm)
Edit: TEdit;
Label: TLabel;
Label: TLabel;
Edit: TEdit;
Button: TButton;
procedure ButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form: TForm;
implementation
{$R *dfm}
procedure TFormButtonClick(Sender: TObject);
var
WideMsg:PWideChar;
DestName:PWideChar;
begin
DestName:=PWideChar(WideString(EditText));
WideMsg:=PWideChar(WideString(EditText));
NetMessageBufferSend(nilDestNamenilWideMsgLength(EditText)*);
end;
end
保存文件然后按F键运行程序程序运行的初始画面如图所示
图 程序运行的初始画面在Computer对应的文本框中输入目的计算机名在Content对应的文本框中输入消息内容如图所示
单击Send按钮就会向指定的计算机发送消息在接收消息的计算机上就会显示一个对话框如图所示
图 指定计算机名和消息内容
图 程序运行结果