gpt4 book ai didi

c# - Powershell 使用来自 Add-Type 的 C# 代码使 shell 崩溃 (SystemParamtersInfo)

转载 作者:行者123 更新时间:2023-11-30 16:04:42 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本,在执行时为我切换鼠标属性……但我无法通过查询某些属性的过程。我不是 C# 程序员,只是一个崭露头角的 Powershell 脚本编写者,但我有使用其他脚本语言的经验……这让我觉得自己做了一些愚蠢的事情。

执行下面的脚本会导致崩溃,有用的信息很少....所以我想我的第一个问题是:有什么方法可以更好地了解我的脚本为什么会导致 powershell 崩溃?

就其他方式而言,我已经尝试修改注册表,但我不想重新启动以应用设置(这或多或少是我打开和打开设置的快速方法一旦我把它平方掉就关闭)...据我所知,SystemParametersInfo(来自 user32)是这样做的方法,但我没有 C# IDE 或在 C# 中编译程序的方法, 如果不太麻烦的话,宁愿使用 powershell。

此外,似乎使用 SPI 导入调用的 ref int lpvParam 版本有效(我将其与 SPI_GETMOUSESPEED 一起使用)但 ref int[] lpvParam 版本确实有效不是.. 也许是函数定义有问题? 我假设我需要多次导入它并重载它,但也许这是错误的?替换最后调用函数来测试这个——使用 $mouse.spiGetMouseSpeed

MS 的文档 ( https://msdn.microsoft.com/en-us/library/ms724947 ) 建议有多种变量类型可以进入 lpvParam 所以我认为重载它是要走的路......

这是代码。

#https://msdn.microsoft.com/en-us/library/ms724947

Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Enigma
{
public class Mouse {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(
int uAction,
int uParm,
ref int lpvParam,
int fuWinIni);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(
int uAction,
int uParm,
ref int[] lpvParam,
int fuWinIni);

private const int SPI_GETMOUSE = 0x0003;
private const int SPI_GETMOUSEHOVERHEIGHT = 0x0064;
private const int SPI_GETMOUSEHOVERTIME = 0x0066;
private const int SPI_GETMOUSEHOVERWIDTH = 0x0062;
private const int SPI_GETMOUSESPEED = 0x0070;
private const int SPI_GETMOUSETRAILS = 0x005E;
private const int SPI_GETMOUSEWHEELROUTING = 0x201C;

public int[] spiGetMouse() {
int[] res = new int[3];
SystemParametersInfo(SPI_GETMOUSE, 0, ref res, 0);
return res;
}

public int spiGetMouseSpeed() {
int res = 0;
SystemParametersInfo(SPI_GETMOUSESPEED, 0, ref res, 0);
return res;
}
}
}

"@

$mouse = New-Object Enigma.Mouse

# This function call fails and crashes PowerShell ISE
$mouse.spiGetMouse()

exit

最佳答案

数组是 .NET 中的引用类型,因此它们不需要额外的 ref 限定符即可通过引用传递。因此,您应该使用 int[] lpvParam 而不是 ref int[] lpvParamint (System.Int32) 是一个值类型,所以它必须有 ref 限定符才能通过引用传递。

关于c# - Powershell 使用来自 Add-Type 的 C# 代码使 shell 崩溃 (SystemParamtersInfo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34640103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com