Windows Console Application c#: RegisterRawInputDevices

0

I am using RegisterRawInputDevices of user32.dll to register the keyboard in one specific application and I'm getting Error 87 (ERROR_INVALID_PARAMETER) with GetLastWin32Error() function. When printing the Main Window Handle of the application I get "460346", it doesn't seem to be a wrong value. I also tried with The Current Process Main Handle but I got the same error.

Here is the code:

class Program
{

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint numberDevices, uint size);

    public const ushort RIDEV_INPUTSINK = 0x00000100;

    static void Main(string[] args)
    {
        RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];
        rid[0].usUsagePage = 0x01; // USB HID Generic Desktop Page
        rid[0].usUsage = 0x06;          // Keyboard Usage ID
        rid[0].dwFlags = RIDEV_INPUTSINK;
        foreach (Process pList in Process.GetProcesses())
        {
            if (pList.MainWindowTitle.Contains("WinBox"))
            {
                rid[0].hwndTarget = pList.MainWindowHandle;
                Console.Out.Write(pList.MainWindowHandle);
            }
        }
        if (!RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0])))
        {
            Console.Out.WriteLine("Error" + Marshal.GetLastWin32Error());
        }
        else
        {
            Console.Out.Write("ok");
        }
        Application.Run();
    }
}

}

what could be wrong in this solution? Thanks in advance!!

c#
windows
console-application
barcode-scanner
asked on Stack Overflow Jun 27, 2020 by German Artesi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0