using System.Runtime.InteropServices;//添加引用
public struct COPYDATASTRUCT//信息结构
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] public string lpData;
}
const int WM_COPYDATA = 0x004A;//WINDOWS信息
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // 向接收信息的进程句柄
int Msg, // 消息
int wParam, // 消息参数
ref COPYDATASTRUCT lParam // 消息参数
);
public void send()
{
string msg = "测试";
byte[] sarr = System.Text.Encoding.Default.GetBytes(msg );
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr) 100;
cds.lpData = msg;
cds.cbData = len + 1;
SendMessage(int.Parse(Ptrstr), 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);
//mystr.lpData 就是传近来的字符串!
break;
default:
base.DefWndProc(ref m);
break;
}
}