请问,如何在DELPHI中用 net send 发广播。不可以的话有什么方法。我的的是这样。。。(100分)

  • 主题发起人 主题发起人 weadvance
  • 开始时间 开始时间
W

weadvance

Unregistered / Unconfirmed
GUEST, unregistred user!
winexec(pchar('net send ' + trim(EDIT2.Text) + ' ' + c),SW_HIDE);
 
可以做成 bat文件
 
是一种方法。但每次这样加,很不对啊。再说程序又要设置路径等。
我刚想,可以不可以把数据库里的数据读出来一个个的做。
还有啊,我记得IP地址最后一位,254不就是广播吗?为什么不能用。
 
广播是255.255.255.255
 
不行。怎么样把局域网中的计算机自动刷新呢?
 
用个函数
function GetUsers(GroupName: string; var List: TStringList): Boolean;

type

TNetResourceArray = ^TNetResource; //网络类型的数组

var

i: Integer;
Buf: Pointer;
Temp: TNetResourceArray;
lphEnum: THandle;
NetResource: TNetResource;
Count, BufSize, Res: DWord;

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; //缓冲区大小设置为8K

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 i := 0 to Count - 1 do //列举工作组的计算机名称

begin

//获取工作组的计算机名称,+2表示删除"/",如/wangfajun=>wangfajun

List.Add(Temp^.lpRemoteName + 2);

inc(Temp);

end;

end;

Res := WNetCloseEnum(lphEnum); //关闭一次列举

if Res <> NO_ERROR then exit; //执行失败

Result := True;

FreeMem(Buf);

end;
 
shellexecute(self.handle, nil, 'net.exe', pchar('send '+機器名或IP地址+' '+發送內容), nil, SW_HIDE);
 
unit NetSend;
interfaceuses
Windows;
const
netapi = 'netapi32.dll';
type
LPBYTE = PByte;
NET_API_STATUS = DWord;
const
// success
NERR_Success = 0;
// Base address of error codes
NERR_BASE = 2100;
// A general network error occurred.
NERR_NetworkError = NERR_BASE + 36;
// The user name could not be found.
NERR_NameNotFound = NERR_BASE + 173;
// The user does not have access to the requested information.
// ERROR_ACCESS_DENIED - declared in Windows.pas (= 5)
// This network request is not supported.
// ERROR_NOT_SUPPORTED - declared in Windows.pas (=50)
// The specified parameter is invalid.
// ERROR_INVALID_PARAMETER - declared in Windows.pas (=87)
function NetMessageBufferSend(servername, msgname, fromname: LPCWSTR;
buf: LPBYTE; buflen: DWORD
): NET_API_STATUS; stdcall;
function NetSendMsg(serv, name, text: string): NET_API_STATUS;
implementation
function NetMessageBufferSend; external netapi name 'NetMessageBufferSend';
// serv = name of the remote server on which the function is to execute
// (The string must begin with // or empty ('') for local computer
// (you need some privileges to do this on other machines ;)
// name = the message alias to which the message should be sent
// text = string that contains the message text (multiline is no problem)
function NetSendMsg(serv, name, text: string): NET_API_STATUS;
var
msgserv: PWideChar;
servlen: Cardinal;
msgname: PWideChar;
namelen: Cardinal;
msgtext: PWideChar;
textlen: Cardinal;
begin
Result := ERROR_INVALID_PARAMETER;
servlen := 2 * (Length(serv) + 1);
namelen := 2 * (Length(name) + 1);
textlen := 2 * (Length(text) + 1);
if (servlen > 2) then msgserv := GetMemory(servlen) else msgserv := nil;
msgname := GetMemory(namelen);
msgtext := GetMemory(textlen);
if Assigned(msgname) and Assigned(msgtext) then
try
if Assigned(msgserv) then StringToWideChar(serv, msgserv, servlen div 2);
StringToWideChar(name, msgname, namelen div 2);
StringToWideChar(text, msgtext, textlen div 2);
Result := NetMessageBufferSend(msgserv, msgname, nil, PByte(msgtext), textlen);
finally
if Assigned(msgtext) then FreeMemory(msgtext);
if Assigned(msgname) then FreeMemory(msgname);
if Assigned(msgserv) then FreeMemory(msgserv);
end;
end;
end.
 
net send * 广播的消息内容
 
后退
顶部