-
October 3rd, 2003, 08:03 AM
#1
Send messages between 2 c# applications
Hi,
Im trying to send messages between two C# applications using the API Sendmessage.
It works great if the message is send to the same application, using :
private void buttonSendMessage_Click(object sender, System.EventArgs e)
{
String s = "Hello";
IntPtr p = Marshal.StringToCoTaskMemAuto(s);
int retour=Win32.WindowsAPI.SendMessage(this.Handle,WM_MIMENS,IntPtr.Zero,p);
}
protected override void WndProc(ref Message m)
{
if (m.Msg==WM_MIMENS)
{
IntPtr pV=Win32.WindowsAPI.GlobalLock(m.LParam);
string s3 =System.Runtime.InteropServices.Marshal.PtrToStringAuto(pV);
Win32.WindowsAPI.GlobalUnlock(m.LParam);
Console.WriteLine(s3);
}
}
But if the message is sent by another application, the string written by Console.WriteLine(s3) is something like '%9%1' and not 'Hello'.
Help!
-
October 3rd, 2003, 05:05 PM
#2
i think another application such as c++ has a char - 1,
where as C# has a unicode format.
i guess reasons being one of that..
- Software Architect
-
October 5th, 2003, 12:18 PM
#3
Virtual memory problem, for you I mean.
I think what you are doing would work in Win98 but not in later OS-s. You need to find the C# equivelent to C++ GlobalAlloc().
Sorry I could not offer more help.
-
October 10th, 2003, 01:54 PM
#4
I do not have an answer for your post on sending message data between applications. But I am very interested in how you made as much progress as you did.
I would like to be able to send interger data from one application to another. Can you outline this with some code snapshots for me or could you point me towards the source where you learned to do this?
thanks
-
October 11th, 2003, 10:46 AM
#5
I find a way to send String between two applications.
The main problem was as the applications were running into different process, the pointers used were only known by host application, so u can't do what i wrote to send inter-process strings.
Nevertheless, it works fine with integers.
So, for integers between process, solution is :
const int WM_USER = 0x0400;
const int WM_CUSTOM_MESSAGE=WM_USER+ 0x0001;
//Sender
int numberToSend =1547;
SendMessage(targetApplicationHandle,WM_CUSTOM_MESSAGE, Intptr.Zero,numberToSend);
//*sender
//Receiver
protected override void WndProc(ref Message m)
{
if (m.Msg==WM_CUSTOM_MESSAGE)
{
int numberReceived = (int)m.Lparam;
//Do ur job with this integer
}else
{
base.WndProc(ref m);
}
}
//*receiver
Nb :
Using SendMessage API, the sender is stopped until receiver return. Look at MessageQueue if it's an issue for u.
For string interprocess, the easiest way i found is using Atom.
Last edited by dricks; October 11th, 2003 at 10:48 AM.
-
October 11th, 2003, 12:45 PM
#6
Hm...
What about WM_COPYDATA message???
Look at MSDN at this message. It allows you copying buffers between applications...
martin
-
October 11th, 2003, 05:45 PM
#7
How do you get the handle of another application?
I truly appreciate your response, and I understand exactly what you are saying.
The only piece of the puzzle I am missing is how you get the handle of another application?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|