title: RegisterRawInputDevices throws error 0
body: <p>I'm currently working on my barcodescanner application, and now I'm stuck at the <code>RegisterRawInputDevices</code> function.</p>
  
  <p><code>Marshal.GetLastWin32Error()</code> always throws the error 0. I can't tell why.
  I've got a handle of my current console window and the properties (usagePage, usage, Flags) are set correctly.</p>
  
  <p>Therefore it should be able to register a rawinput-device which my window can listen at.
  I'm going through the example code of an <a href="http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard#_Toc156395975" rel="nofollow">article written by Emma Burrows and Steve Messer at codeplex</a>.</p>
  
  <p>The Method "InputDevice" is the constructor of the InputDevice class of the WPF example code.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// InputDevice constructor; registers the raw input devices
  /// for the calling window.
  /// &lt;/summary&gt;
  /// &lt;param name="hwnd"&gt;Handle of the window listening for key presses&lt;/param&gt;
  public InputDevice(IntPtr hwnd)
  {
      RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];
  
      rid[0].usUsagePage = 0x01; // -&gt; set to hid
      rid[0].usUsage = 0x06; // -&gt; set to keyboard
      rid[0].dwFlags = RIDEV_INPUTSINK; // -&gt; set flag to let the window recieve
                                               WM-Messages even if it is not focused
      rid[0].hwndTarget = hwnd; // -&gt; the handle of the window
  
      if (!RegisterRawInputDevices(rid, (uint)rid.Length,
                                      (uint)Marshal.SizeOf(rid[0])))
      {
          throw new ApplicationException (Marshal.GetLastWin32Error().ToString());
          //throw new ApplicationException("Failed to register raw input device(s).");
      }
  }
  </code></pre>
  
  <p>In the Program.cs of my application the <code>StartWndProcHandler()</code> function is called, the function itself, gets the handler of the window and instances an object of <code>InputDevice</code>.</p>
  
  <pre><code>// Gets the handler of the window and then instances a new inputdevice object
  static void StartWndProcHandler()
  {
      IntPtr hwnd = IntPtr.Zero;
      IntPtr myWin = GetConsoleWindow();
  
      try
      {
          hwnd = myWin;
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex);
      }
  
      device = new InputDevice(hwnd); // instances a new inputdevice
      //NumberOfKeyboards = device.EnumerateDevices();
      //device.KeyPressed += device_KeyPressed; 
  }
  </code></pre>
  
  <p>I'm sure that I've understood the functionality of the RawInput registering and I've studied the source code to find out what it does.</p>
  
  <p>But now I'm at the end of my wisdom... It simply seems unlogical to me why the application cannot register any device.</p>
  
  <p>I hope someone could give me a hint or an approach.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I was able to extract a detailed error code. It is error 1008 which means, that I wanted to reference a token that doesn't exist. So the solution is a step nearer. :)</p>
  
  <p><strong>UPDATE (01-12-2014) 2:</strong></p>
  
  <p>Hello Guys,
  After hours of debugging and searching for the problem I was not really able to find a solution.
  All needed values are passed to the <code>RawInputDevice</code> Struct, but i still get the error 1008.</p>
  
  <p>I've tested the original WPF Demoproject of the article mentioned above. It can register a device without problems, but can't capture the raw input from keyboard.
  So the registering of the devices should work i guess.</p>
  
  <p>So i hope that someone eventuelly could give me any advice or aproach.</p>
  
  <p>Best regards</p>
  
  <p><strong>UPDATE (23-12-2014):</strong></p>
  
  <p>Hello guys,</p>
  
  <p>we solved the problem completely different. Because we use a debian machine where we connected the scangun, we now were able to read out the scangun input easily with a bashscript. 
  The problem is still unsolved, but not focused anymore by us now.</p>
  
  <p>Best regards</p>