title: How do I use GetConsoleDisplayMode in C#?
body: <p><strong>Background</strong></p>
  
  <p>I'm working with the console and PInvokes in C#, just to see what magic you can do when you're not limiting yourself to the .NET framework. I stumbled upon a function in Kernel32 called GetConsoleDisplayMode that basically lets you see if the console is running in windowed, full-screen or hardware full-screen mode. The documentation for this function can be found <a href="https://docs.microsoft.com/en-us/windows/console/getconsoledisplaymode" rel="nofollow noreferrer">here</a>.</p>
  
  <p>GetConsoleDisplayMode takes only one parameter: an LPDWORD in which the display mode can be stored. The function returns a bool, indicating the success of the call. <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx" rel="nofollow noreferrer">According to this documentation</a>, an LPDWORD is a pointer to a DWORD, which in turn is simply a 32-bit unsigned integer (<em>uint32</em> in C#).</p>
  
  <p>So, from what I understand from the docs is that this should work:</p>
  
  <pre><code>[DllImport("kernel32", EntryPoint = "GetConsoleDisplayMode", ExactSpelling = false, CharSet = CharSet.Unicode, SetLastError = true)]
  public static extern bool GetConsoleDisplayMode(ref uint lpModeFlags);
  
  static void Main(string[] args) {
      uint mode = 0;
      var success = GetConsoleDisplayMode(ref mode);
  
      Console.WriteLine(success);
      Console.WriteLine(mode);
  
      Console.ReadKey(true);
  }
  </code></pre>
  
  <p>However, the function always returns false!</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>Of course I have tried to modify the code, in hopes of getting it to work properly.</p>
  
  <p>A call to GetLastError() returns the most curious error code 3221684230 of which even <a href="https://www.google.nl/search?q=GetConsoleDisplayMode%203221684230" rel="nofollow noreferrer">Google</a> and <a href="https://msdn.microsoft.com/nl-nl/library/windows/desktop/ms681381(v=vs.85).aspx" rel="nofollow noreferrer">Microsoft Documentation</a> have no knowledge (nor is it uint.MaxValue or int.MaxValue, I have checked). This does not get me a step further, so I dropped this option.</p>
  
  <p>Another thing I've tried (in the hopes that I had read or understood the documentation incorrectly) is changing <em>ref</em> to <em>out</em>, which passes an uninitialized variable for the function to initialize for me:</p>
  
  <pre><code>[DllImport("kernel32", EntryPoint = "GetConsoleDisplayMode", ExactSpelling = false, CharSet = CharSet.Unicode, SetLastError = true)]
  public static extern bool GetConsoleDisplayMode(out uint lpModeFlags);
  
  static void Main(string[] args) {
      var success = GetConsoleDisplayMode(out var mode);
      if (!success) {
          Console.WriteLine(Kernel32.GetLastError());
      }
  
      Console.WriteLine(success);
      Console.WriteLine(mode);
  
      Console.ReadKey(true);
  }
  </code></pre>
  
  <p>Alas, this too was unsuccessful and, interestingly, produces the same error code.</p>
  
  <p><strong>My question(-s)</strong></p>
  
  <p>Why does my call to GetConsoleDisplayMode always return false, why does GetLastError return such an obscure error code and most importantly: how do I find the error that's bugging me and/or get this to work?</p>
  
  <p><strong>More information</strong></p>
  
  <p>I am running Windows 10 x64 build 1709 (Fall Creators Update).</p>
  
  <p>Maybe <a href="https://books.google.nl/books?id=1K-nJd1ZUv0C&amp;pg=PA145&amp;lpg=PA145&amp;dq=getconsoledisplaymode%20not%20working&amp;source=bl&amp;ots=YbNzjVFcsj&amp;sig=tqQVTFHT6jbFSLgBn1M1U0NUKTk&amp;hl=nl&amp;sa=X&amp;ved=0ahUKEwiRldv0xrXZAhVLLcAKHaB4BBUQ6AEIKzAB#v=onepage&amp;q=getconsoledisplaymode%20not%20working&amp;f=false" rel="nofollow noreferrer">this excerpt</a> from John Paul Mueller's book on the Windows API and the .NET framework can be of help (that's about the best documentation of GetConsoleDisplayMode that I can find).</p>