Q :关于给居于网络里面的计算机发送消息的问题?
主要解答者:jianzhang提交人:zflying2000
感谢:li_zhifu、hnxrm
审核者:dext论坛对应贴子:查看
A :
我想问一个问题,就是如何通过程序控制对局域网内或者广域网中能ping通的计算机发送消息并且可以在另外一台计算机上显示出来。就是利用windows的message服务。相似的例子就是那个信史服务软件的功能。
不知道在delphi中如何调用。或者其原来是如何?
用那些API函数功能?
非常感谢!
保证给分数!
---------------------------------------------------------------
Winexec('net send 168.1.100.8 this is a test',sw_hide);
---------------------------------------------------------------
Winexec('net send * this is a test',sw_hide);
这就能广播了
把this is a test这句话,用个字符串变量来代替就可以发任何消息.
不过这办法有些笨,应该是有API函数实现这个功能的.
---------------------------------------------------------------
用以下函数
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;
dest 是目的主机名
Source 是源主机名可任以设置
Msg 是消息