接收端收不到 SendMessage发送的消息

使用C#的 SendMessage 发送消息给另一个进程, 如下:

 

public const int WM_COPYDATA = 0x004A;
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
    public int Reserved;
    public int Length;
    public int pData;
}
#region 打包数据结构
IntPtr pAgr = Marshal.AllocHGlobal(szData.Length);
Marshal.Copy(szData, 0, pAgr, szData.Length);

COPYDATASTRUCT cds;
cds.Length = szData.Length;
cds.Reserved = IntPtr.Zero;
cds.pData = (int)pAgr;

IntPtr pcbData = Marshal.AllocHGlobal(Marshal.SizeOf(cds));
Marshal.StructureToPtr(cds, pcbData, false);
#endregion

 
  
SendMessage((int)HWND, (uint)WM_COPYDATA, (int)WndHandle, (int)pcbData);

接收端死活都收不到数据, 百思不得其解, 后来实在没有办法, 修改了一下 COPYDATASTRUCT的定义为:
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
	public IntPtr Reserved;
	public int Length;
	public IntPtr pData;
}


 
  
居然就可以了...无语. 不明白是什么问题. 先用着先.

你可能感兴趣的:(C#)