There seem to be two basic ways of dealing with lParam values when
overriding the WndProc --
1) us Marshal.PtrToStructure/StructureToPtr
2) use an unsafe code block and do a cast
It seems like the latter is more annoying...
Suppose I want to change value of a struct member in the lParam. By method
#1, you'd need something like the following:
protected override bool OnWindowPosChanged(ref Message m)
{
Win32.WINDOWPOS w = Marshal.PtrToStructure(m.lParam,
typeof(Win32.WINDOWPOS));
w.cy += 3; // we're chaging the height of the window...
Marshal.StructureToPtr(m.lParam, w, true);
return false;
}
The key annoyance is that you have to also call the last StructureToPtr to
put the changed data back... Now if this was in one place, then ok, but if
you're doing this all over the place, it quickly becomes annoying.
Using an unsafe block we could do something like:
protected override bool OnWindowPosChanged(ref Message m)
{
unsafe
{
Win32.WINDOWPOS* w = (Win32.WINDOWPOS*)m.lParam;
w->cy += 3;
}
return false;
}
Using the unsafe block changes the original structure rather than a copy of
it. Are these really the two only options? Assuming that we have Full
Trust anyway (since we're doing a ton of P/Invoke already), is there a
reason not to use method #2?
Regards,
--Oren
Oren Novotny - 30 May 2004 18:53 GMT
> Win32.WINDOWPOS* w = (Win32.WINDOWPOS*)m.lParam;
>
> w->cy += 3;
Actually, this is most often done as one line, which only adds to the
nuisance of using the Marshal class...
((Win32.WINDOWPOS*)m.LParam)->cy += 3;
So, is there any real reason to use the Marshal class and not do this?
--Oren
"Ying-Shen Yu[MSFT]" - 31 May 2004 04:53 GMT
Hi Oren,
Either way is fine for C#, In Managed C++ we even have "It just works",
however these features may not available to those languages without
"pointer" concept (e.g. VB.NET and those script languages). So Marshal
class would be helpful in those scenario, also even in C# there are chances
we need passes the unmanaged structure to some other methods to let them
modify it, in this situation Marshal.PtrToStructure/StructureToPtr would be
useful since the unsafe code is expected to be used in a small scope.
Best regards,
Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
Oren Novotny - 03 Jun 2004 19:30 GMT
Thanks for the response -- I guess I was looking for a discussion on the
pro/cons on each way, but I guess no one has taken the hook.
Oh well,
--Oren
> Hi Oren,
>
[quoted text clipped - 15 lines]
> This mail should not be replied directly, please remove the word "online"
> before sending mail.