c# – WinAPI form dragging, warning CA1901, CA1060 NativeMethods

Question:

Implemented dragging the form by the body using WinAPI:

public partial class FormMain : Form
{
    [DllImport("user32", CharSet = CharSet.Auto)]
    internal extern static bool PostMessage(IntPtr hWnd, uint Msg, uint WParam, uint LParam);

    [DllImport("user32", CharSet = CharSet.Auto)]
    internal extern static bool ReleaseCapture();

    const uint WM_SYSCOMMAND = 0x0112;
    const uint DOMOVE = 0xF012;
    const uint DOSIZE = 0xF008;

    public FormMain()
    {
        InitializeComponent();
    }


    private void FormMain_MouseDown(object sender, MouseEventArgs e)
    {
        ReleaseCapture();
        PostMessage(this.Handle, WM_SYSCOMMAND, DOMOVE, 0);
    }
}

I got 2 warnings while analyzing the code:

  1. Warning CA1060 Because the method is a P/Invoke method, 'FormMain.PostMessage(IntPtr, uint, uint, uint)' must be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.

How to correctly implement the NativeMethods class?

Solved, thanks Uranus

public partial class FormMain : Form
{
    public static class NativeMethods
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        internal extern static bool PostMessage(IntPtr hWnd, uint Msg, uint WParam, uint LParam);

        [DllImport("user32", CharSet = CharSet.Auto)]
        internal extern static bool ReleaseCapture();
    }
    const uint WM_SYSCOMMAND = 0x0112;
    const uint DOMOVE = 0xF012;
    const uint DOSIZE = 0xF008;

    public FormMain()
    {
        InitializeComponent();
    }


    private void FormMain_MouseDown(object sender, MouseEventArgs e)
    {
        NativeMethods.ReleaseCapture();
        NativeMethods.PostMessage(this.Handle, WM_SYSCOMMAND, DOMOVE, DOSIZE);
    }
}
  1. Warning CA1901 As declared in your code, the 'WParam' parameter for the P/Invoke 'FormMain.PostMessage(IntPtr, uint, uint, uint)' will be 4 bytes on 64-bit platforms. This is incorrect because the current native declaration for this API specifies that it should be 8 bytes for 64-bit platforms. Refer to the MSDN Platform SDK documentation and find out which data type should be used instead of 'uint'.

Help with fixing CA1901 by implementing constants for UintPtr, thanks. After a few hours, I start to think that the error is in the studio and not in my code.

Warning CA1901 As declared in your code, the 'Msg' parameter for P/Invoke 'FormMain.NativeMethods.PostMessage(IntPtr, UIntPtr, UIntPtr, UIntPtr)' will be 8 bytes on 64-bit platforms. This is incorrect because the current native declaration for this API specifies that it should be 4 bytes in size for 64-bit platforms. Refer to the MSDN Platform SDK documentation and find out which data type should be used instead of 'UIntPtr'.

It's a vicious circle ? Or is there still a panacea, I still ask you to help.

Answer:

It was necessary to cast the uint types to IntPtr .

public static class NativeMethods
{
    [DllImport("user32", CharSet = CharSet.Auto)]
    internal extern static bool PostMessage(IntPtr hWnd, uint Msg, IntPtr WParam, IntPtr LParam);

    [DllImport("user32", CharSet = CharSet.Auto)]
    internal extern static bool ReleaseCapture();
}

const uint WM_SYSCOMMAND = 0x0112;
const uint DOMOVE = 0xF012;
const uint DOSIZE = 0xF008;

private void FormMain_MouseDown(object sender, MouseEventArgs e)
{
    NativeMethods.ReleaseCapture();
    NativeMethods.PostMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)DOMOVE, (IntPtr)0);
}
Scroll to Top