Return string using SendMessage from native code

  • Thread starter Thread starter muntyanu
  • Start date Start date
M

muntyanu

Hi all,

In C# system service I need to get some string from MFC mainframe
window by SendMessage() API call. I managed to send message, but can
not find the way to return string to C# app.
Is that possible to receive string back in C# from return value, wparam
or lparam?

Please if someone did it post the example,

I would greatly appreciate,
Roman
 
Roman,

Yes, it is. You can declare the parameter (lParam or wParam) as a
StringBuilder instance (since I assume that the MFC code is only writing to
memory that is already allocated. If it is not, then shame on it) when
declaring the SendMessage API.

Hope this helps.
 
Thanks, for quick response,
actually I did it like this:
C# side
StringBuilder sb = new StringBuilder(256);
SendMessage(process.MainWindowHandle, WM_GET_AUTH_MEMBER, 0, sb);

C++ side
CString member = "SomeString";
strcpy((char*)lParam, member.GetBuffer());

Then on C# side sb has not been changed
Any idea ?
Thanks
 
It is
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int
wParam, StringBuilder lparam);
 
I forgot to tell that the window I am sending the message is in
different process.
Probably I need to lock that memory in StringBuilder buffer to prevent
it from being destroyed
between processes
Thanks
 
What you are trying to accomplish is not possible using SendMessage.
SendMessage can only be used to pass a message to a Window, the receiving
Window procedure gets a copy of the message, but it cannot change or return
to the original message buffer.

Willy.
 
If it is in another process, then I don't think this will work. The
reason for this is that in the other process, the address that you pass in
isn't valid in that context (since virtual address spaces are local to the
process).

You won't be able to get that string across the process boundary through
SendMessage.
 
Thanks all of you,

can you suggest if there is any way to get information from the process
when I created instance of Process object in C# system service using
just process ID? I need to get some string from MainFrame member
variable.

Thank you in advance
Roman
 
I forgot to tell that the window I am sending the message is in
different process.
Probably I need to lock that memory somehow from being destroyed.
Thanks
 
If you need to transfer messages back and forth between processes you'll
need to set-up some form of IPC. One way to accomplish this is using .NET
Remoting this requires both processes to be .NET based, which as far as I
understood is not the case.
There are other forms of IPC possible, but in this case I would simply use a
file and write the string to it so that the other process can read it back.

Willy.
 
Back
Top