您的位置:程序门 -> vb ->



vb向c#程序发送消息的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb向c#程序发送消息的问题
发表于:2007-10-24 13:30:43 楼主
    我在vb程序中向c#程序发送消息,用sendmessage函数时c#程序可以收到,但改成postmessage时c#程序就收不到
vb发出的消息。vb的代码如下,请大家看看可能是什么原因

        '\\   declarations
        private   declare   function   postmessagelong   lib   "user32"   alias   "postmessagea"   (byval   hwnd   as   long,   byval   wmsg   as   long,   byval   wparam   as   long,   lparam   as   any)   as   long

        private   declare   sub   copymemory   lib   "kernel32"   alias   "rtlmovememory"   (byref   hpvdest   as   any,   byref   hpvsource   as   any,   byval   cbcopy   as   long)

        private   declare   function   findwindow   lib   "user32"   alias   "findwindowa"   (byval   lpclassname   as   string,   byval   lpwindowname   as   string)   as   long

private   sub   command1_click()
                dim   sstring   as   string
                dim   lhwnd   as   long
                dim   cds   as   copydatastruct

                dim   buf(255)   as   byte

                sstring   =   "string   to   send"
                if   sstring   =   ""   then   exit   sub
                '
                '   get   the   handle   of   the   target   application's   visible   window.
                '
                lhwnd   =   findwindow(vbnullstring,   cwindow_title)
                if   lhwnd   then
                        '
                        '   copy   the   string   into   a   byte   array,
                        '   converting   it   to   ascii.   assign   lpdata
                        '   the   address   of   the   byte   array.
                        '
                        call   copymemory(buf(1),   byval   sstring,   lenb(strconv(sstring,   vbfromunicode)))
                        with   cds
                                .dwdata   =   3
                                .cbdata   =   lenb(strconv(sstring,   vbfromunicode))   +   1
                                .lpdata   =   varptr(buf(1))
                        end   with
                        msgbox   (cds.cbdata)
                       
                        '
                        '   send   the   string.
                        '
                        call   postmessagelong(lhwnd,   wm_copydata,   me.hwnd,   cds)
                end   if
end   sub
发表于:2007-10-24 13:42:341楼 得分:0
将   cds   定义成模块的全局变量。
postmessage   一般要在你的   command1_click()   事件结束后进行响应,而作为过程级变量,cds   已经释放。
发表于:2007-10-24 16:07:542楼 得分:0
postmessage     是向消息队列发送消息所以要有一个接受的   事件去响应
sendmessage     是向窗口发送消息所以不用
发表于:2007-10-25 17:06:033楼 得分:0
no
区别在于   sendmessage   是马上响应消息,发送者必须等待消息处理结束;postmessage   是将消息加入队列稍后处理,发送者的过程不会中断。
fudy   的问题在于稍后处理时所需要的   cds   已经被释放,lhwnd   的消息处理过程无法得到正确的参数数据。
发表于:2007-10-25 18:51:204楼 得分:0
tiger_zhao回答的非常之正确
发表于:2007-10-27 20:53:035楼 得分:0
楼主要是好好看看msdn,估计就不会问这个问题了,下面内容是我从vs   2002   msdn中粘过来的:

wm_copydata
an   application   sends   the   wm_copydata   message   to   pass   data   to   another   application.  

to   send   this   message,   call   the   sendmessage   function   with   the   following   parameters   (do   not   call   the   postmessage   function).  

sendmessage(  
    (hwnd)   hwnd,                             //   handle   to   destination   window  
    wm_copydata,                             //   message   to   send
    (wparam)   wparam,                     //   handle   to   window   (hwnd)
    (lparam)   lparam                       //   data   (pcopydatastruct)
);
parameters
wparam  
handle   to   the   window   passing   the   data.  
lparam  
pointer   to   a   copydatastruct   structure   that   contains   the   data   to   be   passed.  
return   values
if   the   receiving   application   processes   this   message,   it   should   return   true;   otherwise,   it   should   return   false.  

remarks
the   data   being   passed   must   not   contain   pointers   or   other   references   to   objects   not   accessible   to   the   application   receiving   the   data.  

while   this   message   is   being   sent,   the   referenced   data   must   not   be   changed   by   another   thread   of   the   sending   process.  

the   receiving   application   should   consider   the   data   read-only.   the   lparam   parameter   is   valid   only   during   the   processing   of   the   message.   the   receiving   application   should   not   free   the   memory   referenced   by   lparam.   if   the   receiving   application   must   access   the   data   after   sendmessage   returns,   it   must   copy   the   data   into   a   local   buffer.  

example   code
for   an   example,   see   using   data   copy.  

requirements  
    windows   nt/2000/xp:   included   in   windows   nt   3.1   and   later.
    windows   95/98/me:   included   in   windows   95   and   later.
    header:   declared   in   winuser.h;   include   windows.h.

see   also
sendmessage,   copydatastruct  




快速检索

最新资讯
热门点击