当前位置: 代码迷 >> PB >> []pb中怎么使用wm_copydata接收和发送数据
  详细解决方案

[]pb中怎么使用wm_copydata接收和发送数据

热度:143   发布时间:2016-04-29 09:31:56.0
[求助]pb中如何使用wm_copydata接收和发送数据?
最近在PB中使用WM_COPYDATA消息与第三方程序通讯时遇到困难,恳请指点!

//开发环境:winxp sp3 pro + pb8.04 10501
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//定义copydatastruct
global type str_copydatastruct from structure
  long dwdata
  long cbdata
  long lpdata
end type

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//声明本地外部函数
SUBROUTINE CopyMemory(long pDesc, ref str_copydatastruct pSrc,ulong size) LIBRARY 'kernel32' ALIAS FOR 'RtlMoveMemory'
SUBROUTINE CopyMemory(ref str_copydatastruct pDesc, long pSrc,ulong size) LIBRARY 'kernel32' ALIAS FOR 'RtlMoveMemory'

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//w_test windows
//定义实例变量
CONSTANT Long WM_COPYDATA = 74 //74是WM_COPYDATA消息的消息号

//other event监听
//使用WM_COPYDATA消息接收数据
LONG ll_msg
string ls_test
str_copydatastruct lstr_data

setnull(ll_msg)
ll_msg=Message.Number
if ll_msg= WM_COPYDATA then
  CopyMemory(lstr_data,lparam,12)
  ls_test=string(lstr_data.lpdata,"Address")
  mle_out.text=mle_out.text+'<<: '+ls_test+char(13)+char(10)
  mle_out.text=mle_out.text+'--------------------------------'+char(13)+char(10)
  LocalFree(lparam)
  setnull(lstr_data.dwdata)
  setnull(lstr_data.cbdata)
  setnull(lstr_data.lpdata)
  setnull(ls_test)
end if  

return

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//cb_send使用WM_COPYDATA消息发送数据
long ll_rc,ll_len,long ll_copyDataPointer,ll_address
str_copydatastruct lstr_cds
string ls_src,ls_dst,ls_test

ls_src="我来了"
ls_dst =space(255)  
ll_address=lstrcpy(ls_dst,ls_src)
//将ls_src的内容复制到ls_dst,并返回ls_dst的存储地址  

ll_len=len(ls_src)+1
lstr_cds.dwdata=0
lstr_cds.cbdata=ll_len
lstr_cds.lpdata=ll_address

ll_copyDataPointer=LocalAlloc(0,12)
CopyMemory(ll_copyDataPointer,lstr_cds,12)
ll_rc=Send(hWnd_client,WM_COPYDATA,hWnd_server,ll_copyDataPointer)
//hWnd_client 为第三方程序句柄
//hWnd_server 为程序句柄
LocalFree(ll_copyDataPointer)

return

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
问题:
1.第三程序数据是不重复的,PB中所有接收数据与第一次接收数据相同.
2.使用WM_COPYDATA消息发送数据ll_rc返回值总是0,调用不成功.
3.PB中使用WM_COPYDATA消息与第三方程序通讯是否可行?
恳请指点,帮忙看看,我哪做错了?

谢谢!
Sam
[email protected]

------解决方案--------------------
以前用delphi实现过,当时好像用PB实现时有问题,建议用delphi或者VC之类的来实现吧

你的代码,我测试一下,帮你一起分析一下原因
------解决方案--------------------
接收:
C/C++ code
//other event监听 //使用WM_COPYDATA消息接收数据 LONG ll_msg string ls_test setnull(ll_msg) ll_msg = Message.Number if ll_msg = WM_COPYDATA then    ls_test = string(lparam,"Address")     mle_out.text=mle_out.text+' < <: '+ls_test+char(13)+char(10)     mle_out.text=mle_out.text+'--------------------------------'+char(13)+char(10) end if
------解决方案--------------------

//other event监听 
//使用WM_COPYDATA消息接收数据 
LONG ll_msg 
string ls_test 

setnull(ll_msg) 
ll_msg = Message.Number 

if ll_msg = WM_COPYDATA then
ls_test = string(lparam,"Address") 
mle_out.text=mle_out.text+' < <: '+ls_test+char(13)+char(10) 
  相关解决方案