不想有客户端,只能用2000或NT了 ,其它它也是在后台运行messager
用API实现的代码如下二个单元一定行我在用。
//1
unit main;
interface
uses Unit2,
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls;
var
username:string;
type
tnetresourcearray=^tnetresource;
TfrmMain = class(TForm)
MesText: TMemo;
btnExit: TBitBtn;
btnSend: TBitBtn;
Label1: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
Bevel4: TBevel;
Bevel5: TBevel;
AddrBox: TComboBox;
StatusLabel: TLabel;
ComboBox1: TComboBox;
Label2: TLabel;
procedure btnSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MesTextChange(Sender: TObject);
procedure AddrBoxChange(Sender: TObject);
procedure ComboBox1Click(Sender: TObject);
private
UserName : string;
MessageHeader : TStringList;
function getusers(groupname:string;var list:tstringlist):boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses about;
{$R *.DFM}
function getserverlist(var list:tstringlist):boolean;
var
netresource:tnetresource;
buf
ointer;
count,bufsize,res:dword;
lphenum:thandle;
p:tnetresourcearray;
i,j:smallint;
networktypelist:tlist;
begin
result:=false;
networktypelist:=tlist.Create ;
list.Clear ;
res:=wnetopenenum(resource_globalnet,resourcetype_disk,resourceusage_container,
nil,lphenum);
if res <>no_error then exit;
count:=$ffffffff;
bufsize:=8192;
getmem(buf,bufsize);
res:=wnetenumresource(lphenum,count,pointer(buf),bufsize);
if (res=error_no_more_items) or (res<>no_error) then exit;
p:=tnetresourcearray(buf);
for i:=0 to count-1 do
begin
networktypelist.Add (p);
inc(p);
end;
res:=wnetcloseenum(lphenum);
if res<>no_error then exit;
for j :=0 to networktypelist.Count -1 do
begin
netresource:=tnetresource(networktypelist.Items[j]^);
res:=wnetopenenum(resource_globalnet,resourcetype_disk,
resourceusage_container,@netresource,lphenum);
if res<>no_error then break;
while true do
begin
count:=$ffffffff;
bufsize:=8192;
getmem(buf,bufsize);
res:=wnetenumresource(lphenum,count,pointer(buf),bufsize);
if (res=error_no_more_items) or (res<>no_error) then break;
p:=tnetresourcearray(buf);
for i:=0 to count-1 do
begin
list.Add (strpas(p^.lpremotename));
inc(p);
end;
end;
res:=wnetcloseenum(lphenum);
if res<>no_error then break;
end;
result:=true;
freemem(buf);
networktypelist.Destroy ;
// procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
//begin
//deletefile(temp);
//end;
end;
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 := '参数不正确!';
123 : frmMain.StatusLabel.Caption := '不支持此网络请求!';
2273 : frmMain.StatusLabel.Caption := '不能找到 '+frmMain.AddrBox.Text;
else frmMain.StatusLabel.Caption := '错误: '+IntToStr(res);
end;
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
strUser : PChar;
strSize : DWord;
sl:tstringlist;
i:integer;
list:tstringlist;
i1:integer;
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('---------------------------------');
sl:=tstringlist.Create ;
if getserverlist(sl) then
begin
for i:=0 to sl.Count-1 do
combobox1.Items.Add(sl.CommaText) ;
combobox1.Text :=combobox1.Items[0];
end ;
begin
addrbox.Items.Clear ;
try
list:=tstringlist.Create ;
if getusers(combobox1.text,list)then
if list.Count =0 then
else
for i1:=0 to list.count-1 do
begin
addrbox.Items.Add(copy(list.strings[i1],3,length(list.strings[i1])-2));
addrbox.text:=addrbox.Items[0];
end;
finally
list:=tstringlist.Create ;
end;
end;
end;
procedure TfrmMain.MesTextChange(Sender: TObject);
begin
StatusLabel.Caption := ' ';
end;
procedure TfrmMain.AddrBoxChange(Sender: TObject);
begin
StatusLabel.Caption := ' ';
end;
function tfrmmain.getusers (groupname:string;var list:tstringlist):boolean;
var
netresource:tnetresource;
buf
ointer;
count,bufsize,res:dword;
ind:integer;
lphenum:thandle;
temp:tnetresourcearray;
begin
result:=false;
list.Clear ;
fillchar(netresource,sizeof(netresource),0);//初始化网络层次信息
netresource.lpRemoteName :=@groupname[1];//指定工作组名称
netresource.dwDisplayType :=resourcedisplaytype_server;//类型为服务器
netresource.dwUsage :=resourceusage_container;
netresource.dwScope :=resourcetype_disk;
res:=wnetopenenum(resource_globalnet,resourcetype_disk,resourceusage_container,@netresource,lphenum);
if res<>no_error then exit;
while true do
begin
count:=$ffffffff;
bufsize:=8192;
getmem(buf,bufsize);
res:=wnetenumresource(lphenum,count,pointer(buf),bufsize);
if res=error_no_more_items then break;
if (res<>no_error)then exit;
temp:=tnetresourcearray(buf);
for ind:=0 to count-1 do//列举工作组的计算机名
begin
list.Add (temp^.lpremotename);
inc(temp);
end;
end;
res:=wnetcloseenum(lphenum);//关闭一次列举
if res<>no_error then exit;
result:=true;
freemem(buf);
end;
procedure TfrmMain.ComboBox1Click(Sender: TObject);
var
list:tstringlist;
i:integer;
begin
addrbox.Items.Clear ;
try
list:=tstringlist.Create ;
if getusers(combobox1.text,list)then
if list.Count =0 then
else
for i:=0 to list.count-1 do
begin
addrbox.Items.Add(copy(list.strings
,3,length(list.strings)-2));
addrbox.text:=addrbox.Items[0];
end;
finally
list:=tstringlist.Create ;
end;
end;
end. //主程序
unit Unit1;
interface
uses sysutils,classes;
function tounicode(str:string;destwidechar):integer;
function sendmsg(toh,from,msg:string):integer;
function netmessagebuffersend(servernamewidechar;
msgnamewidechar;
fromnamewidechar;
bufwidechar;
var buflen:integer):integer;cdecl;
implementation
function tounicode(str:string;destwidechar):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.