日期:2014-05-20  浏览次数:20957 次

如何在C#中用WM_COPYDATA传递struct给另一个进程
如何通过
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
请大家帮帮忙,很急啊

------解决方案--------------------
问一句,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);
}
------解决方案--------------------
[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;