如何调用NetMessageBufferSend发送消息?
问题描述:
如何调用WINNT/2K/XP下的API函数NetMessageBufferSend模拟net send命令来发送消息?
解决方案:
下面代码已测试成功,直接导入PBL即可
$PBExportHeader$w_main.srw
forward
global type w_main from Window
end type
type mle_1 from multilineedit within w_main
end type
type cb_1 from commandbutton within w_main
end type
type sle_1 from singlelineedit within w_main
end type
type st_2 from statictext within w_main
end type
type st_1 from statictext within w_main
end type
end forward
type icmp_echo_reply from structure
unsignedlong address
unsignedlong status
unsignedlong roundtriptime
unsignedlong datasize
unsignedlong reserved[3]
character data[250]
end type
global type w_main from Window
int X=1056
int Y=484
int Width=1531
int Height=1152
boolean TitleBar=true
string Title="NETMESSAGESEND"
long BackColor=80269524
boolean ControlMenu=true
boolean MinBox=true
boolean Resizable=true
mle_1 mle_1
cb_1 cb_1
sle_1 sle_1
st_2 st_2
st_1 st_1
end type
global w_main w_main
type prototypes
Function ulong NetMessageBufferSend(ulong servername, ref char msgname[],ulong fromname, ref char buf[], ulong buflen) Library "netapi32.dll" Alias for "NetMessageBufferSend"
Function ulong IcmpCreateFile () Library "icmp.dll"
Function long IcmpSendEcho (ulong IcmpHandle, ulong DestinationAddress, string RequestData,long RequestSize, long RequestOptions, Ref icmp_echo_reply ReplyBuffer, long ReplySize, long Timeout ) Library "icmp.dll" Alias for "IcmpSendEcho"
Function long IcmpCloseHandle (ulong IcmpHandle) Library "icmp.dll"
Function ulong inet_addr (string cp) Library "ws2_32.dll" Alias for "inet_addr"
end prototypes
type variables
CONSTANT ulong NERR_Success = 0
end variables
forward prototypes
public subroutine wf_string_to_unicode (string as_string, ref character ac_unicode[])
public subroutine wf_string_to_unicode (string as_string, ref character ac_unicode[])
public function boolean wf_netmessagebuffersend (string as_sendto, string as_msgtext)
public function boolean wf_ping (string as_ipaddress, string as_echomsg)
end prototypes
public subroutine wf_string_to_unicode (string as_string, ref character ac_unicode[]);Integer li_loop, li_len, li_uni
li_len = Len(as_string)
FOR li_loop = 1 TO li_len
li_uni = li_uni + 1
ac_unicode[li_uni] = Mid(as_string, li_loop, 1)
li_uni = li_uni + 1
ac_unicode[li_uni] = Char(0)
NEXT
li_uni = li_uni + 1
ac_unicode[li_uni] = Char(0)
li_uni = li_uni + 1
ac_unicode[li_uni] = Char(0)
end subroutine
public function boolean wf_netmessagebuffersend (string as_sendto, string as_msgtext);Ulong lul_result, lul_buflen
Char lc_msgname[],lc_msgtext[]
wf_string_to_unicode(as_sendto, lc_msgname)
wf_string_to_unicode(as_msgtext, lc_msgtext)
lul_buflen = UpperBound(lc_msgtext)
lul_result = NetMessageBufferSend(0, lc_msgname,0, lc_msgtext, lul_buflen)
If lul_result = NERR_Success Then
Return True
Else
Return False
End If
end function
public function boolean wf_ping (string as_ipaddress, string as_echomsg);ULong lul_address, lul_handle
Long ll_rc, ll_size
String ls_reply
icmp_echo_reply lstr_reply
lul_address = inet_addr(as_ipaddress)
If lul_address > 0 Then
lul_handle = IcmpCreateFile()
ll_size = Len(as_echomsg)
ll_rc = IcmpSendEcho(lul_handle, lul_address, &
as_echomsg, ll_size, 0, &
lstr_reply, 278, 200)
IcmpCloseHandle(lul_handle)
If ll_rc <> 0 Then
If lstr_reply.Status = 0 Then
ls_reply = String(lstr_reply.Data)
If ls_reply = as_echomsg Then
Return True
End If
End If
End If
End If
Return False
end function
on w_main.create
this.mle_1=create mle_1
this.cb_1=create cb_1
this.sle_1=create sle_1
this.st_2=create st_2
this.st_1=create st_1
this.Control[]={this.mle_1,&
this.cb_1,&
this.sle_1,&
this.st_2,&
this.st_1}
end on
on w_main.destroy
destroy(this.mle_1)
destroy(this.cb_1)
destroy(this.sle_1)
destroy(this.st_2)
destroy(this.st_1)
end on
type mle_1 from multilineedit within w_main
int X=27
int Y=264
int Width=1399
int Height=604
int TabOrder=20
BorderStyle BorderStyle=StyleLowered!
long TextColor=33554432
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type cb_1 from commandbutton within w_main
int X=1070
int Y=904
int Width=357
int Height=108
int TabOrder=30
string Text=" 发送(&S)"
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
event clicked;if not wf_ping(trim(sle_1.text),"") then
messagebox("提示","指定目标地址不存在或不通!")
return
end if
if wf_NetMessageBufferSend(trim(sle_1.text),trim(mle_1.text)) then
messagebox("提示","发送成功!")
else
messagebox("提示","发送失败!")
end if
end event
type sle_1 from singlelineedit within w_main
int X=430
int Y=48
int Width=997
int Height=92
int TabOrder=10
BorderStyle BorderStyle=StyleLowered!
boolean AutoHScroll=false
long TextColor=33554432
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type st_2 from statictext within w_main
int X=14
int Y=172
int Width=379
int Height=76
boolean Enabled=false
string Text="发送内容:"
boolean FocusRectangle=false
long TextColor=33554432
long BackColor=67108864
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type st_1 from statictext within w_main
int X=14
int Y=52
int Width=379
int Height=76
boolean Enabled=false
string Text="目标地址:"
boolean FocusRectangle=false
long TextColor=33554432
long BackColor=67108864
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
**************************************************
另一种方法转换UNICODE:
$PBExportHeader$w_main.srw
forward
global type w_main from Window
end type
type mle_1 from multilineedit within w_main
end type
type cb_1 from commandbutton within w_main
end type
type sle_1 from singlelineedit within w_main
end type
type st_2 from statictext within w_main
end type
type st_1 from statictext within w_main
end type
end forward
type icmp_echo_reply from structure
unsignedlong address
unsignedlong status
unsignedlong roundtriptime
unsignedlong datasize
unsignedlong reserved[3]
character data[250]
end type
global type w_main from Window
int X=1056
int Y=484
int Width=1531
int Height=1152
boolean TitleBar=true
string Title="NETMESSAGESEND"
long BackColor=80269524
boolean ControlMenu=true
boolean MinBox=true
boolean Resizable=true
mle_1 mle_1
cb_1 cb_1
sle_1 sle_1
st_2 st_2
st_1 st_1
end type
global w_main w_main
type prototypes
Function ulong IcmpCreateFile () Library "icmp.dll"
Function long IcmpSendEcho (ulong IcmpHandle, ulong DestinationAddress, string RequestData,long RequestSize, long RequestOptions, Ref icmp_echo_reply ReplyBuffer, long ReplySize, long Timeout ) Library "icmp.dll" Alias for "IcmpSendEcho"
Function long IcmpCloseHandle (ulong IcmpHandle) Library "icmp.dll"
Function ulong inet_addr (string cp) Library "ws2_32.dll" Alias for "inet_addr"
Function ulong MultiByteToWideChar(ulong CodePage,ulong dwFlags,ref string lpMultiByteStr,ulong cchMultiByte,ref string lpWideCharStr,ulong cchWideChar) LIBRARY "kernel32.dll"
Function ulong NetMessageBufferSend(ulong servername, ref string msgname,ulong fromname, ref string buf, ulong buflen) Library "netapi32.dll" Alias for "NetMessageBufferSend"
end prototypes
type variables
CONSTANT ulong NERR_Success = 0
end variables
forward prototypes
public function boolean wf_netmessagebuffersend (string as_sendto, string as_msgtext)
public function boolean wf_ping (string as_ipaddress, string as_echomsg)
public function boolean wf_netmessagebuffersend (string as_sendto, string as_msgtext)
public function boolean wf_ping (string as_ipaddress, string as_echomsg)
end prototypes
public function boolean wf_netmessagebuffersend (string as_sendto, string as_msgtext);string ls_msgtext,ls_sendto,ls_wmsgtext,ls_wsendto,ls_char
Ulong ll_result,ll_lenstr
ls_sendto = as_sendto
ls_msgtext = as_msgtext
setnull(ls_char)
ll_lenstr = MultiByteToWideChar(0, 0, ls_sendto, -1, ls_char, 0)
ls_wsendto = space(ll_lenstr * 2)
ll_lenstr = MultiByteToWideChar(0, 0, ls_sendto, -1, ls_wsendto, ll_lenstr)
setnull(ls_char)
ll_lenstr = MultiByteToWideChar(0, 0, ls_msgtext, -1, ls_char, 0)
ls_wmsgtext = space(ll_lenstr * 2)
ll_lenstr = MultiByteToWideChar(0, 0, ls_msgtext, -1, ls_wmsgtext, ll_lenstr)
ll_result = NetMessageBufferSend(0, ls_wsendto,0 , ls_wmsgtext, ll_lenstr * 2)
If ll_result = NERR_Success Then
Return True
Else
Return False
End If
end function
public function boolean wf_ping (string as_ipaddress, string as_echomsg);ULong lul_address, lul_handle
Long ll_rc, ll_size
String ls_reply
icmp_echo_reply lstr_reply
lul_address = inet_addr(as_ipaddress)
If lul_address > 0 Then
lul_handle = IcmpCreateFile()
ll_size = Len(as_echomsg)
ll_rc = IcmpSendEcho(lul_handle, lul_address, &
as_echomsg, ll_size, 0, &
lstr_reply, 278, 200)
IcmpCloseHandle(lul_handle)
If ll_rc <> 0 Then
If lstr_reply.Status = 0 Then
ls_reply = String(lstr_reply.Data)
If ls_reply = as_echomsg Then
Return True
End If
End If
End If
End If
Return False
end function
on w_main.create
this.mle_1=create mle_1
this.cb_1=create cb_1
this.sle_1=create sle_1
this.st_2=create st_2
this.st_1=create st_1
this.Control[]={this.mle_1,&
this.cb_1,&
this.sle_1,&
this.st_2,&
this.st_1}
end on
on w_main.destroy
destroy(this.mle_1)
destroy(this.cb_1)
destroy(this.sle_1)
destroy(this.st_2)
destroy(this.st_1)
end on
type mle_1 from multilineedit within w_main
int X=27
int Y=264
int Width=1399
int Height=604
int TabOrder=20
BorderStyle BorderStyle=StyleLowered!
long TextColor=33554432
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type cb_1 from commandbutton within w_main
int X=1070
int Y=904
int Width=357
int Height=108
int TabOrder=30
string Text=" 发送(&S)"
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
event clicked;if isnull(sle_1.text) or isnull(mle_1.text) or trim(sle_1.text) = "" or trim(mle_1.text) = "" then
messagebox("提示","有未填项目!")
return
end if
if not wf_ping(trim(sle_1.text),"") then
messagebox("提示","指定目标地址不存在或不通!")
return
end if
if wf_NetMessageBufferSend(trim(sle_1.text),trim(mle_1.text)) then
messagebox("提示","发送成功!")
else
messagebox("提示","发送失败!")
end if
end event
type sle_1 from singlelineedit within w_main
int X=430
int Y=48
int Width=997
int Height=92
int TabOrder=10
BorderStyle BorderStyle=StyleLowered!
boolean AutoHScroll=false
long TextColor=33554432
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type st_2 from statictext within w_main
int X=14
int Y=172
int Width=379
int Height=76
boolean Enabled=false
string Text="发送内容:"
boolean FocusRectangle=false
long TextColor=33554432
long BackColor=67108864
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type
type st_1 from statictext within w_main
int X=14
int Y=52
int Width=379
int Height=76
boolean Enabled=false
string Text="目标地址:"
boolean FocusRectangle=false
long TextColor=33554432
long BackColor=67108864
int TextSize=-10
int Weight=400
string FaceName="方正姚体"
FontCharSet FontCharSet=GB2312CharSet!
FontPitch FontPitch=Variable!
end type