2014-04-11 206 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); 
    } 

} 

}