利用WM_COPYDATA可以轻松实现两个进程间的通信,但是一般都是传string数据,今天看到CSDN上有人问如何传Struct。下面是我的解决方法。
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public int lpData;
}
[DllImport( " user32 " , EntryPoint = " SendMessageA " )]
public static extern int SendMessage( int Hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam);
const int WM_COPYDATA = 0x004A ;
// 定义要传递的Struct
[StructLayout(LayoutKind.Sequential)]
struct WholeInfo
{
//SizeConst指定字符串很重要,后面要用到
[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();
// 获得Struct对应的IntPtr
Marshal.StructureToPtr(h, ptr1, false );
SendData.lpData = ptr1.ToInt32();
SendData.cbData = size;
int intHWnd = FindWindow( null , @" 接收方 " );
if (intHWnd > 0 )
{
SendMessage(intHWnd , WM_COPYDATA, 0,ref (SendData));
}
// 接收方代码
// 处理消息
protected override void DefWndProc( ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT RecvData = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
WholeInfo h = (WholeInfo)Marshal.PtrToStructure((IntPtr)RecvData.lpData, typeof(WholeInfo));
this.textBox1.Text = h.cPath ;
break;
default:
base.DefWndProc(ref m);
break;
}
}
public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public int lpData;
}
[DllImport( " user32 " , EntryPoint = " SendMessageA " )]
public static extern int SendMessage( int Hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam);
const int WM_COPYDATA = 0x004A ;
// 定义要传递的Struct
[StructLayout(LayoutKind.Sequential)]
struct WholeInfo
{
//SizeConst指定字符串很重要,后面要用到
[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();
// 获得Struct对应的IntPtr
Marshal.StructureToPtr(h, ptr1, false );
SendData.lpData = ptr1.ToInt32();
SendData.cbData = size;
int intHWnd = FindWindow( null , @" 接收方 " );
if (intHWnd > 0 )
{
SendMessage(intHWnd , WM_COPYDATA, 0,ref (SendData));
}
// 接收方代码
// 处理消息
protected override void DefWndProc( ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT RecvData = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
WholeInfo h = (WholeInfo)Marshal.PtrToStructure((IntPtr)RecvData.lpData, typeof(WholeInfo));
this.textBox1.Text = h.cPath ;
break;
default:
base.DefWndProc(ref m);
break;
}
}
关键的代码就是
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
这样C#才能分配正确的内存位置,但是要记住一个汉字占2位