Pointer Accuracy Toggle Enhance

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]; // Get the current values. SystemParametersInfoGet(SPI_GETMOUSE, 0, GCHandle.Alloc(mouseParams, GCHandleType.Pinned).AddrOfPinnedObject(), 0); // Modify the acceleration value as directed. mouseParams[2] = b ? 1 : 0; // Update the system setting. return SystemParametersInfoSet(SPI_SETMOUSE, 0, GCHandle.Alloc(mouseParams, GCHandleType.Pinned).AddrOfPinnedObject(), SPIF.SPIF_SENDCHANGE); } 

Also this documentation turned out to be useful.

+2
source share
1 answer

You tried:

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

Shamelessly filmed from:

http://www.pinvoke.net/default.aspx/user32/SystemParametersInfo.html

+1
source

Source: https://habr.com/ru/post/1383798/


All Articles