Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Win32 API to get desktop work area rectangle

I Want SystemParametersInfoA to return a System.Drawing.Rectangle but i have no idea how to proceed.

Here is my code so far:

[DllImport("user32.dll")]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, out IntPtr pvParam, uint fWinIni);
const uint SPI_GETWORKAREA = 0x0030;

void GetRect()
{
    IntPtr WorkAreaRect;
    SystemParametersInfo(SPI_GETWORKAREA, 0, out WorkAreaRect, 0);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Per the SPI_GETWORKAREA documentation:

The pvParam parameter must point to a RECT structure that receives the coordinates of the work area, expressed in physical pixel size.

The pointer in question is not an out value. It is an in value. You are supposed to pass in your own pointer to an existing RECT instance, which SPI_GETWORKAREA will then simply fill in.

You can use Marshal.AllocHGlobal() to allocate memory for a RECT, and then use Marshal.PtrToStructure() to extract the populated RECT from the memory.

Try this:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
const uint SPI_GETWORKAREA = 0x0030;

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left, Top, Right, Bottom;
}

void GetRect()
{
    IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RECT)));
    SystemParametersInfo(SPI_GETWORKAREA, 0, mem, 0);
    RECT r = new RECT;
    Marshal.PtrToStructure(mem, r);
    Rectangle WorkAreaRect = new Rectangle(r.Left, r.Top, r.Width, r.Height);
    Marshal.FreeHGlobal(mem);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading