高分求教,net send 的网络问题,做过这个的进来(200分)

  • 主题发起人 主题发起人 seilfer
  • 开始时间 开始时间
S

seilfer

Unregistered / Unconfirmed
GUEST, unregistred user!
我调用NETMESSAGEBUFFERSEND
NET_API_STATUS ret = NetMessageBufferSend(NULL,sendto,sendfrom,(LPBYTE)&message,m_sMessage.length)
我用自己的机器发给自己是可以的,可是发给同一个网段别的机器函数返回NERR_NameNotFound The user name could not be found. 在同事的机器上试了一下,发给自己也报这个错误。 系统都是2000 SERVER ,相互之间用NET SEND都没有问题。 我怀疑
是NetMessageBufferSend(NULL 这个函数不应该设置成NULL吧, 做过此类程序的人来讲讲啊
 
function NetSend(dest, Source, Msg: string): Longint; overload;

type

TNetMessageBufferSendFunction = function(servername, msgname, fromname: PWideChar;

buf: PWideChar; buflen: Cardinal): Longint;

stdcall;

var

NetMessageBufferSend: TNetMessageBufferSendFunction;
SourceWideChar: PWideChar;
DestWideChar: PWideChar;
MessagetextWideChar: PWideChar;
Handle: THandle;

begin

Handle := LoadLibrary('NETAPI32.DLL');
if Handle = 0 then

begin
Result := GetLastError;
Exit;
end;

@NetMessageBufferSend := GetProcAddress(Handle, 'NetMessageBufferSend');

if @NetMessageBufferSend = nil then
begin
Result := GetLastError;
Exit;
end;

MessagetextWideChar := nil;
SourceWideChar := nil;
DestWideChar := nil;

try

GetMem(MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);

GetMem(DestWideChar, 20 * SizeOf(WideChar) + 1);

StringToWideChar(Msg, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);

StringToWideChar(Dest, DestWideChar, 20 * SizeOf(WideChar) + 1);

if Source = '' then

Result := NetMessageBufferSend(nil, DestWideChar, nil,

MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1)

else

begin

GetMem(SourceWideChar, 20 * SizeOf(WideChar) + 1);

StringToWideChar(Source, SourceWideChar, 20 * SizeOf(WideChar) + 1);

Result := NetMessageBufferSend(nil, DestWideChar, SourceWideChar,

MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);

FreeMem(SourceWideChar);

end;

finally

FreeMem(MessagetextWideChar);

FreeLibrary(Handle);

end;

end;

给你个不错的函数
 
我看了一下和我写的差不多,我是用VC写的,但是我发给别人会报错啊。 我觉得可能和我们这里网络设置有关
 
如果返回NERR_NameNotFound 就是找不到计算机 可能跨网段发不过去吧
 
是在同一个网段得,而且用NET SEND 都可以发,应该不是那个得问题
 
能用Net send
可以用
shellexecute(self.handle, nil, 'net.exe', pchar('send '+機器名或IP地址+' '+發送內容), nil, SW_HIDE);
 
servername 是wideString類型, 不知你注意到沒有??
 
我想直接调用那个API函数,因为比较容易返回出错信息。SERVERNAME应该填什么?
那个是WIDESTRING 我知道的。我填的NULL
 
以前用VB写了一个,根据IP或者计算机名发送,也是用这个API,找不到源码了.

另:哪位高人知道如何截获Net send 发过来的消息,以前看到过一个软件,但是现在找不到了
 
这是VB的源码,不知道对你有没有什么帮助
'发送子程序
Public Function Sendmsg(strTo As String, strFrom As String, strMessage As String) As Boolean
Dim bytTo() As Byte
Dim bytFrom() As Byte
Dim bytMsg() As Byte
bytTo = strTo & vbNullChar
bytFrom = strFrom & vbNullChar
bytMsg = strMessage & vbNullChar
On Local Error Resume Next
Sendmsg = (NetMessageBufferSend(ByVal 0&, bytTo(0), bytFrom(0), bytMsg(0), UBound(bytMsg)) = NERR_Success)
End Function

SERVERNAME是0&的话,就是本机发送~~
 
第一個參數servername, 是wideString類型, 如果你照平時的string類型輸入, 操作就會找不到!
 
試試如下代碼

function NetMessageBufferSend(ServerName, MsgName, FromName: PWideChar;
Buf: Pointer; BufLen: Word): word; stdcall; external 'netapi32.dll' name 'NetMessageBufferSend';

function NetSend(const MsgName,SendMsg: string): Boolean;
var
PWCMsgName, PWCSendMsg: PWideChar;
Error: Word;
begin
try
Error := 1;
GetMem(PWCMsgName, 2*Length(MsgName)+2);
GetMem(PWCSendMsg, 2*Length(SendMsg)+2);
StringToWideChar(MsgName, PWCMsgName, Length(MsgName)+2);
StringToWideChar(SendMsg, PWCSendMsg, Length(SendMsg)+2);
Error :=
NetMessageBufferSend(nil, PWCMsgName, nil, PWCSendMsg,
Length(SendMsg)*2);
finally
FreeMem(PWCMsgName);
FreeMem(PWCSendMsg);
Result := Error = 0;
end;
end;
 
调试通过:

function NetMessageBufferSend(ServerName: PWideChar;
MsgName: PWideChar;
FromName: PWideChar;
Buf: PWideChar;
BufLen: integer): Integer; stdcall;
external 'netapi32.dll'
function NetUserChangePassword(
domainname: PWideChar;
username: PWideChar;
oldpassword: PWideChar;
newpassword: PWideChar): LongInt; stdcall;
external 'netapi32.dll';
function NetUserChangePWD(DomainName,UserName,OldPWD,NewPWD:WideString):Longint;
begin
Result:=NetUserChangePassword(PWideChar(DomainName),PWideChar(UserName),PWideChar(OldPWD),PWideChar(NewPWD));
end;
function SendMsg(RemoteHost,FromName,MsgStr:WideString):integer; // RemoteHost:远程主机,不是一个子网用IP;FromName:发送消息者名称;MsgStr:消息内容 ,WideString在调用时直接用字符串;
begin
Result:=NetMessageBufferSend(
nil,pwidechar(RemoteHost),
pwidechar(FromName),
pwidechar(MsgStr),
length(MsgStr) * SizeOf(PWideChar)
);

end;

procedure TForm1.BtnSendMsgClick(Sender: TObject);
var re:integer;
MsgStr:string;
begin
msgstr:=MemoMsg.text;
re:=SendMsg(EdtRemoteIP.Text,EdtFromName.text,msgstr);
case re of
0 : MemoStatus.lines.add('给 '+EdtRemoteIP.text+' 的信息发送成功'+' at:'+timetostr(time()));
87 : MemoStatus.lines.add('参数是不正确的'+' at:'+timetostr(time()));
123 : MemoStatus.lines.add('sintax is incorrect'+' at:'+timetostr(time()));
2273 : MemoStatus.lines.add('找不到主机: '+EdtRemoteIP.text+' at:'+timetostr(time()));
end;

end;

 
后退
顶部