如何在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
请大家帮帮忙,很急啊
...全文
332 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenxianwqc 2008-08-06
  • 打赏
  • 举报
回复
帮顶,sdl2005lyx 3楼的方法可以用
huheng_0_0 2007-02-01
  • 打赏
  • 举报
回复
哦,谢谢,我仔细学习一下你提供的代码 :) 谢谢
先把问题解决了再说,^_^ ,不过我现在有点钻牛角尖,非常想通过那种方式也能把这个问题解决了,呵呵~~~
sdl2005lyx 2007-02-01
  • 打赏
  • 举报
回复
lz:不知到你具体用途是什么,我也是一次传全部啊,并没有等哪部分,如果你要保证每次都是一个整体,那你的进程参数加一个记数量控制也行啊。。。。。
huheng_0_0 2007-02-01
  • 打赏
  • 举报
回复
to feiyun0112(http://feiyun0112.cnblogs.com/)
运行这句话
GCHandle GC = GCHandle.Alloc(e, GCHandleType.Pinned);的时候会出现以下错误.
{"Object contains non-primitive or non-blittable data."}
请你详细的指点一下,谢谢 :)
-------------------------------------------------------
to sdl2005lyx()
因为我接收方的进程必须一次性处理这些信息,不能等待,是要一次把这些综合起来分析的.不能让他先等到一部分信息,然后等待,接收到新的之后,我在判断这次传过来的是否和上一次不全的是一个整体,或者是另外一个新的处理过程,这样是在接收方不能接收的,呵呵.
sdl2005lyx 2007-02-01
  • 打赏
  • 举报
回复
问一句,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);
}
feiyun0112 2007-02-01
  • 打赏
  • 举报
回复
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
lr21shiji 2007-02-01
  • 打赏
  • 举报
回复
这个只有用api了 具体我也没太清楚 我记得见过 你google下吧
huheng_0_0 2007-02-01
  • 打赏
  • 举报
回复
to feiyun0112(http://feiyun0112.cnblogs.com/), 你能给我讲讲这句吗?
GCHandle GC = GCHandle.Alloc(Bytes, GCHandleType.Pinned);
为什么要这么用,我原来的问题除了对结构内部的字符串数组没有定义正确这外,还有哪些地方不对呢?
请你能给我讲讲,这个才是问题的价值~~~ :)
huheng_0_0 2007-02-01
  • 打赏
  • 举报
回复
恩,你给的是对的,非常感谢! 谢谢你的帮助!
xiaoliangwh 2007-02-01
  • 打赏
  • 举报
回复
帮顶了!
feiyun0112 2007-02-01
  • 打赏
  • 举报
回复
[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
  • 打赏
  • 举报
回复
学习,有时间去研究下hoho
huheng_0_0 2007-02-01
  • 打赏
  • 举报
回复
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."}
feiyun0112 2007-02-01
  • 打赏
  • 举报
回复
用Marshal.StructureToPtr看看

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧