切换 增强指针精度

Sep*_*hrM 6 c# pinvoke marshalling mouse-cursor

我们基本上是在创建一个控制面板小程序。我们需要在鼠标属性中切换“增强指针精度”。
为此,我们需要SystemParametersInfo调用SPI_GETMOUSE. 它有一个包含 3 个元素的数组作为其第三个参数。我是 PInvoke 的新手,我尝试过很多签名,但到目前为止还没有成功。这是我为签名所做的尝试:

[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);
Run Code Online (Sandbox Code Playgroud)

上述方法都不适合我,这是我遇到的异常::
System.AccessViolationException尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
在搜索时我想出了这个用VB编写的。

解决方案:感谢GWLlosa 的回答,我想出了解决方案

[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);
}
Run Code Online (Sandbox Code Playgroud)

该文档也被证明是有帮助的。

GWL*_*osa 2

你有没有尝试过:

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

无耻地摘自:

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