Using SystemParametersInfo from C# (SPI_GETSCREENREADER SPI_SETSCREENREADER)

跟風遠走 提交于 2019-12-21 23:01:33

问题


Am I doing this correctly?

[DllImport("user32", CharSet = CharSet.Auto)]
internal static extern long SystemParametersInfo(long uAction, int lpvParam, ref bool uParam, int fuWinIni);

...

public static bool IsScreenReaderRunning()
{
    long SPI_GETSCREENREADER = 70L;
    bool bScreenReader = false;
    long retVal;

    retVal = SystemParametersInfo(SPI_GETSCREENREADER, 0, ref bScreenReader, 0);

    //uint iParam = 0;
    //uint iUpdate = 0;
    //bool result = false;
    //bool bReturn = SystemParametersInfo(SPI_GETSCREENREADER, iParam, &bScreenReader, iUpdate);
    return bScreenReader;
}

public static void ScreenReaderOn()
{
    long SPI_GETSCREENREADER = 71L;
    bool bScreenReader = true;
    long retVal;

    retVal = SystemParametersInfo(SPI_GETSCREENREADER, 0, ref bScreenReader, 0);
}

public static void ScreenReaderOff()
{
    long SPI_GETSCREENREADER = 71L;
    bool bScreenReader = false;
    long retVal;

    retVal = SystemParametersInfo(SPI_GETSCREENREADER, 0, ref bScreenReader, 0);
}

回答1:


The pinvoke declaration is completely wrong, it was copied from VB6 code. The return type and arguments are not long (the VB6 int32 type), they are int. Pinvoke.net is a decent site to get good declarations.

[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);

Don't forget to throw Win32Exception when you get a false return so failure isn't silent.




回答2:


Based on this article, the last parameter to SystemParametersInfo should be:

int SPIF_SENDCHANGE = 0x02;

When changing the value.



来源:https://stackoverflow.com/questions/5434709/using-systemparametersinfo-from-c-sharp-spi-getscreenreader-spi-setscreenreader

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!