2

I have a console game and i want to disable the mouse input. I've got the code from this page, but when i try to run it the GetConsoleMode function fails with errorcode 6, which stands for "Invalid Handle".

So my question: Why does the GetConsoleWindow() method return an invalid console handle?

Here my sourcecode:

private static void DisableMouseInput()
{
    IntPtr consoleHandle = GetConsoleWindow();
    uint consoleMode;

    Console.WriteLine(Marshal.GetLastWin32Error());         // get current console mode
    if (!GetConsoleMode(consoleHandle, out consoleMode))
    {
        // Error: Unable to get console mode.
        Console.WriteLine(Marshal.GetLastWin32Error());
        throw new Exception();
        return;
    }
     // Clear the mouse input bit in the mode flags
    consoleMode = consoleMode & 0xffbf; //0xffef = ~0x0040 = ~ENABLE_QUICK_EDIT
    // set the new mode
    if (!SetConsoleMode(consoleHandle, consoleMode))
    {
        // ERROR: Unable to set console mode
    }
}
Community
  • 1
  • 1
Kippi
  • 132
  • 11

1 Answers1

3

Did you try another way to get consoleHandle? For example:

const int STD_INPUT_HANDLE = -10;

[DllImport("kernel32.dll"]
static extern IntPtr GetStdHandle(int nStdHandle);

private static foo() {
  IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
  ....
}
Sergey Vasiliev
  • 803
  • 8
  • 19