We mainly create a control panel applet. We need to switch "Improve pointer accuracy" in the mouse properties.
To do this, we need to call SystemParametersInfo
using SPI_GETMOUSE
. As a third parameter, it has an array of three elements. I am new to PInvoke and I have tried many signatures, but still no luck. Here is what I tried to sign:
[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SystemParametersInfo(uint uiAction, uint uiParam, [MarshalAs(UnmanagedType.LPArray)] ref long[] vparam, SPIF fWinIni); static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref long[] vparam, SPIF fWinIni);
None of the above worked for me, and here is the exception I get:
System.AccessViolationException
: attempt to read or write protected memory. This often indicates that another memory is corrupted.
During the search, I came up with this one , which is in VB.
Solution: Thanks to the answer of GWLlosa and this one I came up with the solution:
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)] public static extern bool SystemParametersInfoGet(uint action, uint param, IntPtr vparam, SPIF fWinIni); public const UInt32 SPI_GETMOUSE = 0x0003; [DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)] public static extern bool SystemParametersInfoSet(uint action, uint param, IntPtr vparam, SPIF fWinIni); public const UInt32 SPI_SETMOUSE = 0x0004; public static bool ToggleEnhancePointerPrecision(bool b) { int[] mouseParams = new int[3];
Also this documentation turned out to be useful.