PC Review


Reply
Thread Tools Rate Thread

Return string using SendMessage from native code

 
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
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

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      20th Jul 2005
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
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

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      20th Jul 2005
What is your declaration of SendMessage?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
It is
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int
wParam, StringBuilder lparam);

 
Reply With Quote
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
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

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      20th Jul 2005
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.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      20th Jul 2005
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
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

 
Reply With Quote
 
muntyanu@hotmail.com
Guest
Posts: n/a
 
      20th Jul 2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Marshaling Struct for native SendMessage michelqa Microsoft C# .NET 4 14th Jul 2008 03:25 PM
native compiler (dotnet to native code, executeable without .netframework) Michael Reichenbach Microsoft Dot NET 2 25th May 2007 06:12 PM
[repost] Static initialization of native code in CLI/native mixed environment bvisscher@bellsouth.net Microsoft VC .NET 1 27th Apr 2006 06:39 AM
Sending a string using SendMessage Nick Microsoft VB .NET 1 27th Apr 2005 01:26 PM
Is there a SendMessage method in C# just like the SendMessage in eVC++ SXT Microsoft Dot NET Compact Framework 2 17th Oct 2003 11:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:47 AM.