您的位置:程序门 -> .net技术 -> c#



如何在c#中用wm_copydata传递struct给另一个进程


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


如何在c#中用wm_copydata传递struct给另一个进程[已结贴,结贴人:huheng_0_0]
发表于:2007-02-01 09:50:43 楼主
如何通过
struct   copydatastruct
{
public   int   dwdata;
public   int   cbdata;
public   intptr   lpdata;
}
将自定义的结构
struct   wholeinfo
{
public   string   cpath;
public   string   lpath;
bool   status;
}
传递给另外一个进程.
具体的问题可以参考一下  
http://community.csdn.net/expert/topic/5327/5327322.xml?temp=.4446985
请大家帮帮忙,很急啊
发表于:2007-02-01 09:57:461楼 得分:0
这个只有用api了     具体我也没太清楚   我记得见过   你google下吧
发表于:2007-02-01 10:18:382楼 得分:0
wholeinfo   h

lpdata   =   varptr(h);

public   int   varptr(object   e)
{
    gchandle   gc   =   gchandle.alloc(e,   gchandletype.pinned);
    int   gc   =   gc.addrofpinnedobject().toint32();
    gc.free();
    return   gc;
}
   
*****************************************************************************
欢迎使用csdn论坛专用阅读器   :   csdn   reader(附全部源代码)  

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
发表于:2007-02-01 10:20:143楼 得分:10
问一句,lz,为什么非要传结构呢,你把结构里的值一个个传也很好嘛:

1、传递进程参数:
                private   void   startprocess_click(object   sender,   eventargs   e)
                {
                        try
                        {
                                processstartinfo   startinfo   =   new   processstartinfo();
                                startinfo.filename   =   "e:\\graphicprotect\\debug\\utlogicedit.exe ";
                                startinfo.arguments   =   "\ "e:\\test\\exam6.utg\ "   \ "e:\\test\\复件   exam6.utg\ "   \ "0\ "   \ "e:\\resoure\ "   \ "true\ "     \ "工程名\ "   ";
                                //process.start( "e:\\graphicprotect\\debug\\graphicprotect.exe ",   "e:\\ ");
                                startinfo.useshellEXECute   =   false;
                                process.start(startinfo);
                        }
                        catch   (exception   ex)
                        {
                                messagebox.show(ex.message);
                        }
                }
2、接收进程参数:
                ///   </summary>
                [stathread]
                static   void   main(string[]   args)
                {
                        application.enablevisualstyles();
                        application.setcompatibletextrenderingdefault(false);
                        mainform   frm   =   new   mainform();
                        if   (args.length   >   0)
                        {
                                //frm.strfilename   =   args[0].trim();
                                for   (int   i   =   0;   i   <   args.length-4;   i++)
                                {
                                        string   s=args[i];
                                        s.trim();
                                        frm.filecoll.add(s);

                                }
                                frm.currindex   =   int32.parse(args[args.length   -   4].trim());
                                frm.resdir   =   args[args.length-3].trim()   ;
                                frm.username   =   args[args.length   -   2].trim();

                                frm.projectname   =   args[args.length   -   1].trim();
                                if   (args[args.length   -   1]   !=   "资源库 ")
                                        frm.isconfig   =   true;
                                else
                                        frm.isconfig   =   false;
                                //test

                                //foreach   (string   s   in   args)
                                //{
                                //         messagebox.show(s   +   ",len= "   +   s.length);
                                //}
                        }
                        else
                        {
                                frm.isprocess   =   false;
                        }
                        application.run(frm);
                }
发表于:2007-02-01 10:51:374楼 得分:0
to   feiyun0112(http://feiyun0112.cnblogs.com/)
运行这句话
gchandle   gc   =   gchandle.alloc(e,   gchandletype.pinned);的时候会出现以下错误.
{ "object   contains   non-primitive   or   non-blittable   data. "}
请你详细的指点一下,谢谢   :)
-------------------------------------------------------
to   sdl2005lyx()  
因为我接收方的进程必须一次性处理这些信息,不能等待,是要一次把这些综合起来分析的.不能让他先等到一部分信息,然后等待,接收到新的之后,我在判断这次传过来的是否和上一次不全的是一个整体,或者是另外一个新的处理过程,这样是在接收方不能接收的,呵呵.
发表于:2007-02-01 11:31:505楼 得分:0
lz:不知到你具体用途是什么,我也是一次传全部啊,并没有等哪部分,如果你要保证每次都是一个整体,那你的进程参数加一个记数量控制也行啊。。。。。
发表于:2007-02-01 12:40:356楼 得分:0
哦,谢谢,我仔细学习一下你提供的代码   :)     谢谢
先把问题解决了再说,^_^   ,不过我现在有点钻牛角尖,非常想通过那种方式也能把这个问题解决了,呵呵~~~
发表于:2007-02-01 13:14:367楼 得分:0
用marshal.structuretoptr看看
发表于:2007-02-01 13:42:328楼 得分:0
to   feiyun0112(http://feiyun0112.cnblogs.com/)
这个我上次就试过,不知道为什么还是不行,我把我的测试代码贴出来,请你看看.

发送方:
                                wholeinfo   mystruct   =   new   wholeinfo();
                                .....   //   对里面的变量赋值
                                intptr   p   =   marshal.allochglobal(marshal.sizeof(mystruct));
                                marshal.structuretoptr(mystruct,   p,   true);
                                cds.lpdata   =   p;
                                sendmessage(window_handler,   wm_copydata,   0,   ref   cds);
-----------------------------------------------------------------------------
接收方:
                    protected   override   void   defwndproc(ref   system.windows.forms.message   m)
                {
                        switch   (m.msg)
                        {
                                case   wm_copydata:
                                        copydatastruct   mystr   =   new   copydatastruct();
                                        type   mytype   =   mystr.gettype();
                                        mystr   =   (copydatastruct)m.getlparam(mytype);
                                        wholeinfo   h;
                                        h   =   (wholeinfo)marshal.ptrtostructure(mystr.lpdata,   typeo(wholeinfo));                                        
                                        this.textbox1.text   =   h.cpath;
                                        this.textbox2.text   =   h.lpath;
                          }.......

--------------------------------------------------------------
先是在h   =   (wholeinfo)marshal.ptrto......
处报错, "external   component   has   thrown   an   exception. "
如果继续往下走,则在main函数中的application.run(new   accepterdlg());
处报错,{ "attempted   to   read   or   write   protected   memory.   this   is   often   an   indication   that   other   memory   is   corrupt. "}
发表于:2007-02-01 13:45:529楼 得分:0
学习,有时间去研究下hoho
发表于:2007-02-01 15:00:5310楼 得分:40
[structlayout(layoutkind.sequential)]
public   struct   copydatastruct
{
public   int   dwdata;
public   int   cbdata;
public   int   lpdata;
}
                [structlayout(layoutkind.sequential)]
                struct   wholeinfo
                {
                        [marshalas(unmanagedtype.byvaltstr,   sizeconst   =   10)]
                        public   string   cpath;
                        [marshalas(unmanagedtype.byvaltstr,   sizeconst   =   10)]
                        public   string   lpath;
                        public   bool   status;
                }

发送方:
wholeinfo   h=new   wholeinfo();
                        h.lpath= "lpath ";
                        h.cpath   = "cpath ";
                        h.status=true;
                        int   size   =   marshal.sizeof(typeof(wholeinfo   ));
                        byte[]   bytes   =   new   byte[size];
                        gchandle   gc   =   gchandle.alloc(bytes,   gchandletype.pinned);
                        intptr   ptr1   =   gc.addrofpinnedobject();
                        marshal.structuretoptr(h,   ptr1,false   );
                        senddata.lpdata   =   ptr1.toint32();
                        senddata.cbdata   =size;

接收方:
copydatastruct   recvdata   =   (copydatastruct)m.getlparam(typeof(copydatastruct));                                                                                  
wholeinfo   h   =   (wholeinfo)marshal.ptrtostructure((intptr)recvdata.lpdata             ,   typeof(wholeinfo));
                                          this.textbox1.text   =   h.cpath   ;

   
*****************************************************************************
欢迎使用csdn论坛专用阅读器   :   csdn   reader(附全部源代码)  

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
发表于:2007-02-01 16:03:2511楼 得分:0
帮顶了!
发表于:2007-02-01 16:16:0412楼 得分:0
恩,你给的是对的,非常感谢!   谢谢你的帮助!
发表于:2007-02-01 17:08:1013楼 得分:0
to   feiyun0112(http://feiyun0112.cnblogs.com/),   你能给我讲讲这句吗?
gchandle   gc   =   gchandle.alloc(bytes,   gchandletype.pinned);
为什么要这么用,我原来的问题除了对结构内部的字符串数组没有定义正确这外,还有哪些地方不对呢?  
请你能给我讲讲,这个才是问题的价值~~~   :)


快速检索

最新资讯
热门点击