Hi there,
I have overridden the WndProc method to look for custom messages in a C# project. I do a switch on the handled message then pass the message into the corresponding function that deals with that particular message.
One of the messages I handle contains in its lParam a pointer to a string. I'm using the following code to get at that data, but its not working. I always get a blank string returned.
string pointerValue="";
pointerValue = Marshal.PtrToStringAuto( m.LParam );
return pointerValue;
I know the app setting the lParam value was written in C+, so I don't need to concern myself with Garbage Collection issues.
Has anyone else had issues similar to mine, and how did you overcome them? Does anyone know of a generic Windows Message I could use that perhaps returns a pointer to a string in the lParam param?.
All comments are gladly received!
-
-
I can't claim to be an expert in interop, so I may have missed something here, but have you tried using the Message.GetLParam method?
In your case it would be m.GetLParam(typeof(string))
Hope that helps...
-
Hi Mike,
I did try that method first, thought it looked a cleaner way of doing things but I get the following exception when I try it,
An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll
Additional information: No parameterless constructor defined for this object.
I'll have a look on MSDN and see if there is an example I can hack.
This is driving me mad! -
ive been pulling my hair out all week trying to figure out something similar, try one of these methods:
string pointerValue = Marshal.PtrToStringAnsi(m.LParam);
or
string pointerValue = new string((char*)m.LParam, 0, length.here);
hope that helps -
Thanks for all the replys,
I think I *may* have cracked it (I shouldn't say so so soon!).
I'm working on code pinched from here :
http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/05/31/14828.aspx
It makes sense when you think about it, the pointer that I get passed is to a memory address in the app that sent the message. If I do a Marsal.Whatever it looks in my applications memory space.
Looks like you need to get the process of the app that sent the message, at read its memory at the location in the lParam.
I've propably made it sounds overly complicated, read the article and it will all become clear! -
Window messages that cross a process boundary have to be marshalled - the arguments need to be copied from the source process's address space to the destination process's address space. To do this, Windows needs to know what the parameters are - whether they're pointers or just numbers, and if pointers, how big the data pointed to is. If the message is unknown, Windows simply interprets the parameters as numbers. You can't just send arbitrarily-numbered messages and expect it to work correctly.
The Win32 API designers did think of this. There's a special message for copying data between processes - WM_COPYDATA. -
BugFree said:Thanks for all the replys,
I think I *may* have cracked it (I shouldn't say so so soon!).
I'm working on code pinched from here :
http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/05/31/14828.aspx
It makes sense when you think about it, the pointer that I get passed is to a memory address in the app that sent the message. If I do a Marsal.Whatever it looks in my applications memory space.
Looks like you need to get the process of the app that sent the message, at read its memory at the location in the lParam.
I've propably made it sounds overly complicated, read the article and it will all become clear!
Try
m.LParam.ToString()
m.LParam.ToInt32()
m.LParam.ToInt64()
m.LParam.ToPointer()
they are just properties.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.