关于使用NetMessageBufferSend函数实现信使功能的问题(50分)

  • 主题发起人 主题发起人 网际浪子20032003
  • 开始时间 开始时间

网际浪子20032003

Unregistered / Unconfirmed
GUEST, unregistred user!
[blue]我现在的程序是好用了,可是NetMessageBufferSend函数有个参数,就是发送者机器名,fromname[/blue]

如下:
[red]function netmessagebuffersend(servername:pwidechar;
toname:pwidechar;
fromname:pwidechar;
buf: pwidechar;
var buflen:integer):integer;stdcall; external 'netapi32.dll' name 'NetMessageBufferSend';[/red]


[green]我在用的时候,当发给自己的时候,那个fromname用什么都可以,都能正常显示,但是发给别人的时候,要么设成nil,要么必须和我的机器名相同,否则发送不出去,返回的函数值是2273,不知道为什么?看到好多信使工具都能随意更改发送者姓名,为什么我的不行呢?发给自己却怎么改都可以,那位帮帮忙??谢谢,我应该如何实现我想要得这个功能呢?(关键是给别人发送)[/green]
 
[:D]自己顶一下,希望能够引来好心的朋友帮忙
 
不要用NetMessageBufferSend来,不方便
----------------
方法1:
//用winexec 实现信使服务
function NetSendMsg(DesHost, Msg: string) : string;
var str : string;
begin
str:='net send '+DesHost+' '+msg;

if winexec(PChar(str),sw_hide)>31 then
Result:='消息已发送出去!'
else
Result:='消息未能发送出去!';
end;
------------------------------------------
方法2:用邮件槽实现信使服务
//用邮件槽 实现信使服务
procedure SendNetMessage(YourName, ComputerName, MessageStr: String);
Var
MsHandle: Longint;
MsgText: String;
BytesWritten: Cardinal;
MailSlotName: String;
begin
MailSlotName := '//' + ComputerName + '/mailslot/messngr';
MsgText := YourName + Chr(0) + ComputerName + Chr(0) + MessageStr + Chr(0);
MsHandle := CreateFile(PChar(MailSlotName), GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, $FFFFFFFF);
WriteFile(MsHandle, Pointer(MsgText)^, Length(MsgText), BytesWritten, nil);
CloseHandle(MsHandle);
end;
=================================
上面是写成过程或函数,你调用一下就行
 
强烈感谢楼上的高人,我还想知道些事情,邮件槽是什么,使用你给的函数是可以发送,可是无法知道发送状态阿???有没有发送成功都不知道阿,用NetMessageBufferSend函数可以有返回值,能知道为何发送失败或已经发送成功。不想用winexec 实现信使服务,感觉不好,呵呵。请赐教。
 
用NetMessageBufferSend我给个例子,没认真测试过;
---------------------------------------------
//unit2 主函数
unit Unit2;
interface
uses SysUtils, Classes;

function ToUnicode(str:string;dest:PWideChar):integer;
function SendMsg(Toh,From,Msg:string):integer;
function NetMessageBufferSend(servername:PWideChar;
MsgName:PWideChar;
FromName:PWideChar;
Buf: PWideChar;
var BufLen:integer):integer;cdecl;
implementation
function ToUnicode(str:string;dest:PWideChar):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.
 
楼上的可以用,我用过
 
后退
顶部