利用WM_COPYDATA可以轻松实现两个进程间的通信,但是一般都是传string数据,今天看到CSDN上有人问如何传Struct。下面是我的解决方法。
[StructLayout(LayoutKind.Sequential)]
public
struct
COPYDATASTRUCT
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
定义要传递的Struct
[StructLayout(LayoutKind.Sequential)]
struct
WholeInfo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//SizeConst指定字符串很重要,后面要用到
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string cPath;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string lPath;
public bool status;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
发送方代码
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int
intHWnd
=
FindWindow(
null
,
@"
接收方
"
);
if
(intHWnd
>
0
)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
SendMessage(intHWnd , WM_COPYDATA, 0,ref (SendData));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
接收方代码
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
处理消息
protected
override
void
DefWndProc(
ref
System.Windows.Forms.Message m)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
switch(m.Msg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
关键的代码就是
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
这样C#才能分配正确的内存位置,但是要记住一个汉字占2位