用NetMessageBufferSend我给个例子,没认真测试过;
---------------------------------------------
//unit2 主函数
unit Unit2;
interface
uses SysUtils, Classes;
function ToUnicode(str:string;dest
WideChar):integer;
function SendMsg(Toh,From,Msg:string):integer;
function NetMessageBufferSend(servername
WideChar;
MsgName
WideChar;
FromName
WideChar;
Buf: PWideChar;
var BufLen:integer):integer;cdecl;
implementation
function ToUnicode(str:string;dest
WideChar):integer;
var
len:integer;
begin
StringToWideChar(str,dest,len);
Result:=len;
end;
function NetMessageBufferSend; external 'netapi32.dll' name 'NetMessageBufferSend';
function SendMsg(Toh,From,Msg:string):integer;
var
ToName :array [0..64] of WideChar;
WMsgText:array [0..1000] of WideChar;
MsgLen, i:integer;
begin
for i := 0 to 64 do ToName
:= #0;
ToUnicode(Toh,ToName);
for i := 0 to 1000 do WMsgText := #0;
ToUnicode(Msg,WMsgText);
Result:=NetMessageBufferSend(nil,ToName,nil,@WMsgText,MsgLen);
end;
end.
--------------------
//unit main 为1调用函数
unit main;
interface
uses Unit2,
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls;
type
TfrmMain = class(TForm)
MesText: TMemo;
btnSend: TBitBtn;
Label1: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
Bevel4: TBevel;
Bevel5: TBevel;
AddrBox: TComboBox;
StatusLabel: TLabel;
procedure btnSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MesTextChange(Sender: TObject);
procedure AddrBoxChange(Sender: TObject);
private
UserName : string;
MessageHeader : TStringList;
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses about;
{$R *.DFM}
procedure TfrmMain.btnSendClick(Sender: TObject);
var
i, res: Integer;
begin
if AddrBox.Text <> '' then
begin
StatusLabel.Font.Color := clBlack;
StatusLabel.Caption := '请稍等......';
Update;
if AddrBox.Items.IndexOf(AddrBox.Text) = -1 then
AddrBox.Items.Add(AddrBox.Text);
res := SendMsg(AddrBox.Text,'',MessageHeader.Text+mesText.Text);
if res = 0 then
frmMain.StatusLabel.Font.Color := clBlue
else
frmMain.StatusLabel.Font.Color := clRed;
case res of
0 : frmMain.StatusLabel.Caption := '消息已成功送出';
87 : frmMain.StatusLabel.Caption := '指定参数错误';
2273 : frmMain.StatusLabel.Caption := '不能找到该IP '+frmMain.AddrBox.Text;
else frmMain.StatusLabel.Caption := '错误 '+IntToStr(res);
end;
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
strUser : PChar;
strSize : DWord;
begin
MesText.Lines.Clear;
strUser := StrAlloc(100);
strSize := 100;
GetUserName(strUser,strSize);
UserName := strUser;
StrDispose(strUser);
MessageHeader := TStringList.Create;
MessageHeader.Add('---------------------------------');
MessageHeader.Add(' from '+UserName);
MessageHeader.Add('---------------------------------');
end;
procedure TfrmMain.MesTextChange(Sender: TObject);
begin
StatusLabel.Caption := ' ';
end;
procedure TfrmMain.AddrBoxChange(Sender: TObject);
begin
StatusLabel.Caption := ' ';
end;
end.