首页 > 解决方案 > SPI_GETCURSORSHADOW 0x101A - 无法获取布尔值

问题描述

这是底部的 PowerShell 代码,我尝试编写但未能检索光标阴影状态。

我在这里查看了帮助:https ://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow

它指出:

SPI_GETCURSORSHADOW 0x101A

确定光标周围是否有阴影。pvParam 参数必须指向一个 BOOL 变量,如果启用阴影,则接收 TRUE,如果禁用,则接收 FALSE。仅当系统的色深超过 256 色时才会出现此效果。

我的代码尝试可以保存为 .ps1 文件并在 PS ISE 中测试

$CSharpSig = @'
  [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
  public static extern bool SystemParametersInfo(
      int uiAction, uint uiParam, uint pvParam, uint fWinIni);
'@

$SPI_GETCURSORSHADOW = 0x101A

$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo -PassThru

# SPI_GETCURSORSHADOW - pvParam 0 or 1 (3rd argument)
$CursorRefresh::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, $BOOLTOGGLE, 0)
write-output $BOOLTOGGLE

False即使不是这种情况,它也会一直作为状态返回。

即使在阅读了与 PowerShell 没有直接关联的类似线程后,我也不知道如何得到这个: Messed with SystemParametersInfo and Booleans pvParam parameter


编辑,新问题:

我对代码的全部意图是尝试切换光标阴影的设置,所以这是我最近的尝试。我已将与问题相关的注释放在代码中。

此代码适用于我最初的问题,但当我在底部添加注释代码时无效。

# More info here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow

# Get the original setting
$CSharpSigGet = @'
  [DllImport("user32.dll")]
  public static extern bool SystemParametersInfo(
      int uiAction, uint uiParam, out bool pvParam, uint fWinIni);
'@
$SPI_GETCURSORSHADOW = 0x101A
$CursorGet = Add-Type -MemberDefinition $CSharpSigGet -Name WinAPICall -Namespace SystemParamInfo -PassThru
[bool] $getBool = $false
$CursorGet::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, [ref] $getBool, 0).value # Stores the boolean.
$cursorShadowBool = ([ref] $getBool).value
write-output $cursorShadowBool

# Toggle the original setting
# This code is now not working as it throws an error when this code below is uncommented..
# Error below:
# TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand

# $SPI_SETCURSORSHADOW = 0x101B
# $CSharpSigSet = @'
#   [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
#   public static extern bool SystemParametersInfo(
#       int uiAction, uint uiParam, uint pvParam, uint fWinIni);
# '@
# $CursorSet = Add-Type -MemberDefinition $CSharpSigSet -Name WinAPICall -Namespace SystemParamInfo -PassThru
# $CursorSet::SystemParametersInfo($SPI_SETCURSORSHADOW, 0, -not ([ref] $getBool).value, 0)

标签: powershell

解决方案


根据文档

SPI_GETCURSORSHADOW 0x101A

确定光标周围是否有阴影。该pvParam 参数必须指向一个BOOL变量,TRUE如果启用了阴影,则接收该变量,FALSE如果它被禁用。仅当系统的色深超过 256 色时才会出现此效果。

所以你的签名不正确;第三个参数必须指向a BOOL。假设您只需要针对这种特定情况调用它,并且我们不需要为泛型而烦恼IntPtr

$CSharpSig = @'
  [DllImport("user32.dll")]
  public static extern bool SystemParametersInfo(
      int uiAction, uint uiParam, out bool pvParam, uint fWinIni);
'@

[bool] $BOOLTOGGLE = $false
if ($CursorRefresh::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, [ref] $BOOLTOGGLE, 0)) {
    write-output $BOOLTOGGLE
}

推荐阅读