CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Posts
    4

    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!

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  3. #3
    Join Date
    Jun 2003
    Posts
    15

    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.

  4. #4
    Join Date
    Jun 2003
    Posts
    15
    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

  5. #5
    Join Date
    Aug 2003
    Posts
    4
    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.

  6. #6
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Hm...

    What about WM_COPYDATA message???

    Look at MSDN at this message. It allows you copying buffers between applications...

    martin

  7. #7
    Join Date
    Jun 2003
    Posts
    15

    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
  •  





Click Here to Expand Forum to Full Width

Featured