2014-04-11 102 views
0

我正在尝试为KSP创建一个插件,该插件允许在游戏中使用X52 Pro游戏杆的MFD功能。在C#中获取USB设备句柄

我遇到了一个问题,虽然我不知道如何操纵杆的设备句柄。

有谁知道我将如何获得设备句柄?

+1

我不能回答你直接的问题,但确实http://msdn.microsoft.com/en- us/library/windows/hardware/jj649944(v = vs.85).aspx有帮助吗? –

+0

可悲的是,我需要的是一个void *(IntPtr到C++的家伙)指向设备 – viperfan7

+0

你是否打开一个管道到设备自己?如果是这样,那么CreateFile()将把句柄返回给你。我假设你不是,而你想钩住手柄。这并不容易。您可能需要做的是在控制杆驱动程序的顶部插入一个过滤器驱动程序 - 然后您可以向该驱动程序发出请求,这是一个已知的接口,并将它们转发给游戏杆设备。 – Preston

回答

0

您可以在您的Window C#Winforms类中继承WinProc,因为message.LParam和message.RParam是HID句柄,其语义类型与HWND类似。

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

命名空间WindowsFormsApplicationJoyStick { 公共类节目: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); 
    } 

} 

}