简体   繁体   English

在C#中获取USB设备句柄

[英]Getting USB device handle in C#

I'm trying to create an addon for KSP that would allow the usage of the MFD functions of the X52 Pro joystick within the game. 我正在尝试为KSP创建一个插件,以允许在游戏中使用X52 Pro游戏杆的MFD功能。

I've ran into an issue though, where I have no idea how to the the device handle for the joystick. 但是我遇到了一个问题,我不知道如何操纵操纵杆的设备手柄。

Does anyone know how I would get the device handle? 有谁知道我该如何处理设备?

You could subclass WinProc on your Window C# Winforms class , since message.LParam and message.RParam are HID handles with semantic type HWND analogous to MFC. 您可以在Window C#Winforms类中继承WinProc类,因为message.LParam和message.RParam是HID句柄,其语义类型类似于MFC。

using System; 使用系统; using System.Collections.Generic; 使用System.Collections.Generic; using System.Linq; 使用System.Linq; using System.Windows.Forms; 使用System.Windows.Forms; using System.Runtime.InteropServices; 使用System.Runtime.InteropServices; using Microsoft.Win32; 使用Microsoft.Win32;

namespace WindowsFormsApplicationJoyStick { public class Program : RichTextBox { 命名空间WindowsFormsApplicationJoyStick {公共类Program:RichTextBox {

    /// <summary>
    /// Function to retrieve raw input data.
    /// </summary>
    /// <param name="hRawInput">Handle to the raw input.</param>
    /// <param name="uiCommand">Command to issue when retrieving data.</param>
    /// <param name="pData">Raw input data.</param>
    /// <param name="pcbSize">Number of bytes in the array.</param>
    /// <param name="cbSizeHeader">Size of the header.</param>
    /// <returns>0 if successful if pData is null, otherwise number of bytes if pData is not null.</returns>

    [DllImport("User32.dll")]
    extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);


    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)WindowMessages.RawInput)  // WindowMessages.RawInput = 0x00FF (WM_INPUT)
        {
            RAWINPUT input = new RAWINPUT();
            int outSize = 0;
            int size = Marshal.SizeOf(typeof(RAWINPUT));

            outSize = Win32API.GetRawInputData(m.LParam, RawInputCommand.Input, out input, ref size, Marshal.SizeOf(typeof(RAWINPUTHEADER)));
            if (outSize != -1)
            {
                if (input.Header.Type == RawInputType.Joystick)
                {
                          // Output X and Y coordinates of Joystick movements                  
               }
        }
        base.WndProc(ref m);
    }

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM