#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    How to send a windows message from C# to C++ (MFC)?


    Hello mates,

    I'm trying to send a windowsmessage from an app in C# (Compact Frame) to another one in C++ (MFC), both of them in a PDA.

    In the receiver app (MFC) I realize that I receive the message but I can't read the string message, it shows a little square.

    This is the code of the Sender (C#)

    Code:
    OpenNETCF.Win32.Win32Window.SendMessage(hwndVal, typeMsgVal, intValue, stringMessage);
    And this is the code of the receiver (C++)

    Code:
    CString myStr = (CString) lParam;
        AfxMessageBox(myStr);

    Thanks a lot in advance!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Did you compile your C++ code with UNICODE enabled?

    In any event, putting a breakpoint on
    Code:
    CString myStr = (CString) lParam;
        AfxMessageBox(myStr);  // breakpoint here!
    and running the code in the debugger is the thing to do.

    When you hit the breakpoint, use the debugger to examine lParam and myStr in detail to see if you can figure out exactly what the object is.

    Maybe it's a pointer to a UNICODE string, and you assume it's ASCII.
    Maybe it's just a resource handle, or perhaps a pointer to a pointer.

    The point is, if you dig deep enough, you should be able to figure it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    There may also be an issue with the different class types. .NET's String (which I assume that stringMessage is based on) is different from CString. You might need to work out a conversion from String to something that CString will be able to handle upon receipt. I remember encountering that kind of problem when I first tried to work with C++ under .NET, having to convert from string to String and back again in order to work with the GUI, which is why I decided to just learn C# instead. So just exactly what is it that the C# app is sending?

    And, of course, the UNICODE vs ASCII issue as well which should be looked into first.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Thanks for your answers.

    The C# have to send a string message for a printer configuration.
    The problem is that I can't debug the C++ app in the PDA, I can only debug the C# app.

    The C++ code:

    Code:
    CString myStr = (CString) lParam;     AfxMessageBox(myStr);
    I tried to put the string in C# as:

    - String stringMessage= "hello"
    - System.String.Format("hello")
    - System.String.Format("hello").ToString()


    I'm trying to send the string as a Byte[] but i don't know how to convert the Byte[](C#) to a CString of C++

    Any ideas?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82

IMN logo majestic logo threadwatch logo seochat tools logo