Returning data in LParam to Unmanaged Code

  • Thread starter Thread starter BruceR
  • Start date Start date
B

BruceR

Hello,

I am currently trying to respond to custom messages sent via
SendMessage/PostMessage from my unmanaged C++ dll in my C#
application.

I am responding to these types of messages by overriding WndProc
function in my C# form. This works fine until I attempt to populate an
LPARAM with a string so that my unmanaged code can use that string.
That Lparam property of the passed in message object is of type
IntPtr. How can I cast/convert that to a string? And, moreover, how do
I get that string down to my unmanaged code in the context of that
LParam? I would ideally not want to change my unmanaged code to handle
this.

Does someone out there know how to accomplish this? Code samples are
appreciated.

- Bruce
 
How are you declaring the method on C# ?

Have you tried this instead of IntPtr:

[DllImport(...)]
public static extern void Whatever(StringBuffer lparam);


HTH
Brian W
 
Does someone out there know how to accomplish this?

So the message sender passes you a buffer pointer in the lParam and
you want to copy a string to that buffer? You can do that with the
Marshal.Copy method (first convert the string to a char[] or byte[] as
needed) or using the Win32 API function CopyMemory.



Mattias
 
Mattias,

Yes, that is correct. I used the Marshal.Copy method as you have
suggested and that did the trick. Thanks a bunch!

- Bruce


Mattias Sjögren said:
Does someone out there know how to accomplish this?

So the message sender passes you a buffer pointer in the lParam and
you want to copy a string to that buffer? You can do that with the
Marshal.Copy method (first convert the string to a char[] or byte[] as
needed) or using the Win32 API function CopyMemory.



Mattias
 
Back
Top