Home All Groups Group Topic Archive Search About

Marshal.PtrToStructure crashes

Author
9 Apr 2005 7:11 PM
Tyron
I want to get the Position of the Mouse when the User click in the
non-client area of a Window.
WM_NCLBUTTONDOWN seems to be the right Message for this - and the LParam
contains a POINTS struct of the Mouse Position (so the speak on MSDN.
[http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm_nclbuttondown.asp])
But when I try to convert the IntPtr to a Struct, it throws a
Argument.NullReferenceException
Code:


        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
        public struct POINT {
          public Int16 x;
          public Int16 y;
        }

        protected override void WndProc(ref Message m)
        {
            if(m.Msg==WM_NCLBUTTONDOWN) {
                POINT pnt = new POINT();
                // crashes
                pnt = (POINT)Marshal.PtrToStructure(m.LParam,typeof(POINT));
//                   label2.Text = pnt.x + "/" + pnt.y;
            }
            base.WndProc(ref m);
        }

Author
10 Apr 2005 2:30 AM
Dave
I belive the X and Y coords of the unmanged Point structure are 32 bit integers.

You can use System.Windows.Forms.Control.MousePosition as an easier solution.


--
Dave Sexton
d***@www..jwaonline..com
-----------------------------------------------------------------------
Show quoteHide quote
<Tyron> wrote in message news:Os7vffTPFHA.2252@TK2MSFTNGP15.phx.gbl...
>I want to get the Position of the Mouse when the User click in the
> non-client area of a Window.
> WM_NCLBUTTONDOWN seems to be the right Message for this - and the LParam
> contains a POINTS struct of the Mouse Position (so the speak on MSDN.
> [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm_nclbuttondown.asp])
> But when I try to convert the IntPtr to a Struct, it throws a
> Argument.NullReferenceException
> Code:
>
>
> [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
> public struct POINT {
>   public Int16 x;
>   public Int16 y;
> }
>
> protected override void WndProc(ref Message m)
> {
> if(m.Msg==WM_NCLBUTTONDOWN) {
> POINT pnt = new POINT();
> // crashes
> pnt = (POINT)Marshal.PtrToStructure(m.LParam,typeof(POINT));
> //    label2.Text = pnt.x + "/" + pnt.y;
> }
> base.WndProc(ref m);
> }
Are all your drivers up to date? click for free checkup

Author
10 Apr 2005 2:32 AM
John Sun
Tyron wrote:
Show quoteHide quote
> I want to get the Position of the Mouse when the User click in the
> non-client area of a Window.
> WM_NCLBUTTONDOWN seems to be the right Message for this - and the LParam
> contains a POINTS struct of the Mouse Position (so the speak on MSDN.
> [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm_nclbuttondown.asp])
> But when I try to convert the IntPtr to a Struct, it throws a
> Argument.NullReferenceException
> Code:
>
>
>         [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
>         public struct POINT {
>           public Int16 x;
>           public Int16 y;
>         }
>
>         protected override void WndProc(ref Message m)
>         {
>             if(m.Msg==WM_NCLBUTTONDOWN) {
>                 POINT pnt = new POINT();
>                 // crashes
>                 pnt = (POINT)Marshal.PtrToStructure(m.LParam,typeof(POINT));
> //                   label2.Text = pnt.x + "/" + pnt.y;
>             }
>             base.WndProc(ref m);
>         }
  System.Drawing.Point actually takes m.LParam in one of its
constructor, I think this may be a easier way for you to get the positon.

HTH.

Jianwei
Author
10 Apr 2005 12:44 PM
Tyron
Yay, a simple Point pnt = new Point((int)m.LParam) works wonderful.

Thanks Guys!

Bookmark and Share