communication between apps via win messages
i'm trying to let 2 application communicate with each other using windows message. however, i've been getting AccessViolationException (attempted to read or write protected memor开发者_如何学JAVAy) during allocating memory and marshaling of data.
Can someone explain to me what's wrong or suggest a better way? Thanks.
EDIT: using WM_COPYDATA as suggested, but now i've got another problem - the other application isn't receiving the WM_COPYDATA message. what's wrong?
code for sending msg:
public const int WM_COPYDATA = 0x004A;
public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public DATA lpData;
}
public struct DATA
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
public char[] msg1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
public char[] msg2;
}
[DllImport("User32.dll")]
public static extern int SendMessage(int hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
private void button1_Click(object sender, EventArgs e)
{
// data, with null terminated strings
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.lpData.msg1 = textBox2.Text.PadRight(50, '\0').ToCharArray();
cds.lpData.msg2 = textBox3.Text.PadRight(50, '\0').ToCharArray();
cds.cbData = Marshal.SizeOf(cds.lpData);
int result = SendMessage(hwnd, WM_COPYDATA, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle, ref cds); // winAPI
}
code for receiving msg (in the other app):
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{
// doesn't get into this part
COPYDATASTRUCT cds = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
....
}
base.WndProc(ref m);
}
Pointers in Windows (and other modern operating systems) are process-relative. You are sending a pointer to an address in your source process' virtual memory. In the destination process, this same virtual memory location may not be mapped, and if it is mapped, it will be to something completely different.
To copy data between processes, use the WM_COPYDATA message and the Windows OS COPYDATASTRUCT. The OS gives these special handling: it copies the referenced data between the processes, and gives the destination process a pointer to the copied data in the destination process' memory space.
In addition, your Marshal.StructureToPtr call looks wrong. Your struct contains two pointers (because char[] is a reference type), so you are copying two pointers into your HGLOBAL, rather than the contents of the byte arrays. You may be able to use MarshalAsAttribute(UnmanagedType.ByValArray) to get around this but you will need to add sizing info in this case. A simpler solution may be to use the Marshal.Copy method to copy the arrays into the unmanaged memory.
Finally, if you control both applications and can use .NET 3.0 or above, consider hosting a WCF service and sending the data that way. You will probably find that easier than using Windows messages, because it will handle the serialisation and deserialisation of the data for you -- but I realise this may not be an option for you or that you may have already ruled it out.
You need to send a WM_COPYDATA
message.
See this thread.
To answer your edited question:
Are you sure that you have the right HWND?
Use Spy++ to make sure that the HWND is correct, and check whether Spy++ sees the message.
Make sure that you've defined the WM_COPYDATA
constant correctly in both apps.
精彩评论