如何调用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