Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2004

Tip: Looking for answers? Try searching our database.

Best way to deal with WINDOWPOS in WndProc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oren Novotny - 29 May 2004 22:17 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.