Author Topic: cant get handle to the USB HID??  (Read 31308 times)

jain1.anuj

  • Member
  • ***
  • Posts: 28
Re: cant get handle to the USB HID??
« Reply #15 on: January 12, 2012, 03:56:55 am »
ok. i took the help from your application and managed to get the handle to the device. but i am unable to read/write on my device. it is giving me message saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".

i am using the
Code: [Select]
GUID_GUID_DEVINTERFACE_KEYBOARD = new Guid("884b96c3-56ef-11d1-bc8c-00a0c91405dd");
Moreover, i am unable to get the capabilities and attributes too. the code after retrieving the handle to the device is as follows. :-

Code: [Select]
deviceHandle = CreateFile(devicePathName[0], GENERIC_WRITE /*| GENERIC_READ*/ , FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,
            OPEN_EXISTING, 0, IntPtr.Zero);

                int error = GetLastError();
                MessageBox.Show("create file error code: " + error.ToString());

              
                MessageBox.Show("create file handle value : " + deviceHandle.DangerousGetHandle().ToString());

                if (!deviceHandle.IsInvalid)
                {
                    MessageBox.Show("valid handle");
                    HIDD_ATTRIBUTES attribs = new HIDD_ATTRIBUTES();
                    attribs.Size = Marshal.SizeOf(attribs);
                    MessageBox.Show("attributes size: " + attribs.Size.ToString());
                    HidD_GetAttributes(deviceHandle, ref attribs);


                    MessageBox.Show("vendor id " + Convert.ToString(attribs.VendorID, 16));

                }
                else
                {
                    //int error = GetLastError();
                    MessageBox.Show("in else ");
                }
                

                Capabilities = GetDeviceCapabilities(deviceHandle);

                MessageBox.Show("capabilities: " + Capabilities.ToString());

                hidUsage = GetHidUsage(Capabilities);

                MessageBox.Show("usage : " + hidUsage);

                Int32 numberOfInputBuffers = 0;
                Boolean result;

                if (!((IsWindows98Gold())))
                {
                    result = HidD_GetNumInputBuffers(deviceHandle, ref numberOfInputBuffers);
                    MessageBox.Show("number of input buffer: " + numberOfInputBuffers.ToString());
                }
                else
                {
                    //  Under Windows 98 Gold, the number of buffers is fixed at 2.

                    numberOfInputBuffers = 2;
                    success = true;
                }

                readHandle = CreateFile(devicePathName[0], 0, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

                MessageBox.Show("read handle : " + readHandle.DangerousGetHandle().ToString());
                if (readHandle.IsInvalid)
                {
                    exclusiveAccess = true;
                }
                else
                {
                    writeHandle = CreateFile(devicePathName[0], GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                    MessageBox.Show("write handle : " + writeHandle.DangerousGetHandle().ToString());
                }

                String byteValue = null;
                Int32 count = 0;
                
                Byte[] outFeatureReportBuffer = null;

                MessageBox.Show("byte length : " + Capabilities.FeatureReportByteLength.ToString());
                if ( ( Capabilities.FeatureReportByteLength > 0 ) )
                {                    
                        
                    outFeatureReportBuffer = new Byte[ Capabilities.FeatureReportByteLength ];

                    outFeatureReportBuffer[ 0 ] = 0;
  
                    outFeatureReportBuffer[ 1 ] = Convert.ToByte( 29 );
                    
                    if ( Microsoft.VisualBasic.Information.UBound( outFeatureReportBuffer, 1 ) > 1 )
                    {
                        outFeatureReportBuffer[ 2 ] = Convert.ToByte( 80);
                    }  
                    
                    success = Write( outFeatureReportBuffer, deviceHandle );

                    if (success)
                    {
                        MessageBox.Show("A Feature report has been written.");

                        MessageBox.Show(" Feature Report ID: " + String.Format("{0:X2} ", outFeatureReportBuffer[0]));
                        MessageBox.Show(" Feature Report Data:");

                        
                        for (count = 0; count <= outFeatureReportBuffer.Length - 1; count++)
                        {

                            byteValue = String.Format("{0:X2} ", outFeatureReportBuffer[count]);

                            MessageBox.Show(" " + byteValue);
                        }
                    }
                    else
                    {
                        MessageBox.Show("The attempt to write a Feature report failed.");
                    }
            }
            }

            return deviceFound;

        }


        internal String GetHidUsage(HIDP_CAPS MyCapabilities)
        {
            Int32 usage = 0;
            String usageDescription = "";

            try
            {
                MessageBox.Show("page : " + Capabilities.UsagePage.ToString());
                usage = MyCapabilities.UsagePage * 256 + MyCapabilities.Usage;

                MessageBox.Show("usage in method : " + usage.ToString());

                if (usage == Convert.ToInt32(0X102))
                {
                    usageDescription = "mouse";
                }

                if (usage == Convert.ToInt32(0X106))
                {
                    usageDescription = "keyboard";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            MessageBox.Show("description : " + usageDescription);
            return usageDescription;
            
        }        


            internal Boolean Write( Byte[] outFeatureReportBuffer, SafeFileHandle hidHandle )
            {                
                Boolean success = false;
                
                try
                {                      
                      success = HidD_SetFeature(hidHandle, outFeatureReportBuffer, outFeatureReportBuffer.Length);
                      MessageBox.Show( "HidD_SetFeature success = " + success );
                      
                }
                catch ( Exception ex )
                {
                    MessageBox.Show(ex.Message.ToString());
                  
                }
                return success;
            }            
        

        internal HIDP_CAPS GetDeviceCapabilities(SafeFileHandle hidHandle)
        {
            IntPtr preparsedData = new System.IntPtr();
            Int32 result = 0;
            Boolean success = false;
          
            try
            {
                success = HidD_GetPreparsedData(hidHandle, ref preparsedData);

                result = HidP_GetCaps(preparsedData, ref Capabilities);
                if ((result != 0))
                {
                    MessageBox.Show("");
                    MessageBox.Show("  Usage: " + Convert.ToString(Capabilities.Usage, 16));
                    MessageBox.Show("  Usage Page: " + Convert.ToString(Capabilities.UsagePage, 16));
                    MessageBox.Show("  Input Report Byte Length: " + Capabilities.InputReportByteLength);
                    MessageBox.Show("  Output Report Byte Length: " + Capabilities.OutputReportByteLength);
                    MessageBox.Show("  Feature Report Byte Length: " + Capabilities.FeatureReportByteLength);
                    MessageBox.Show("  Number of Link Collection Nodes: " + Capabilities.NumberLinkCollectionNodes);
                    MessageBox.Show("  Number of Input Button Caps: " + Capabilities.NumberInputButtonCaps);
                    MessageBox.Show("  Number of Input Value Caps: " + Capabilities.NumberInputValueCaps);
                    MessageBox.Show("  Number of Input Data Indices: " + Capabilities.NumberInputDataIndices);
                    MessageBox.Show("  Number of Output Button Caps: " + Capabilities.NumberOutputButtonCaps);
                    MessageBox.Show("  Number of Output Value Caps: " + Capabilities.NumberOutputValueCaps);
                    MessageBox.Show("  Number of Output Data Indices: " + Capabilities.NumberOutputDataIndices);
                    MessageBox.Show("  Number of Feature Button Caps: " + Capabilities.NumberFeatureButtonCaps);
                    MessageBox.Show("  Number of Feature Value Caps: " + Capabilities.NumberFeatureValueCaps);
                    MessageBox.Show("  Number of Feature Data Indices: " + Capabilities.NumberFeatureDataIndices);

                    Int32 vcSize = Capabilities.NumberInputValueCaps;
                    Byte[] valueCaps = new Byte[vcSize];

                    result = HidP_GetValueCaps(HidP_Input, valueCaps, ref vcSize, preparsedData);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                
            }
            finally
            {
              

                if (preparsedData != IntPtr.Zero)
                {
                    success = HidD_FreePreparsedData(preparsedData);
                }
            }

            return Capabilities;
        }        




          internal Boolean IsWindows98Gold()
        {
            Boolean result = false;
            
                OperatingSystem myEnvironment = Environment.OSVersion;
                
                //  Windows 98 Gold is version 4.10 with a build number less than 2183.
                
                System.Version version98SE = new System.Version( 4, 10, 2183 );

                if (myEnvironment.Version < version98SE)                
                {
                    MessageBox.Show( "The OS is Windows 98 Gold." );
                    result = true;
                }
                else
                {
                   MessageBox.Show( "The OS is more recent than Windows 98 Gold." );
                    result = false;
                }
                return result;
            }

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: cant get handle to the USB HID??
« Reply #16 on: January 12, 2012, 09:30:35 am »
Did you modify the descriptors to make the device a generic HID?

Don't use the keyboard GUID. See my example code.

Jan

jain1.anuj

  • Member
  • ***
  • Posts: 28
Re: cant get handle to the USB HID??
« Reply #17 on: January 15, 2012, 11:00:21 pm »
i did change the DESCRIPTOR to GUID_DEVINTERFACE_USB but the device does not get detected.

Code: [Select]
GUID_DEVINTERFACE_USB = new Guid("4D1E55B2-F16F-11CF-88CB-001111000030");

I even tried it with GUID_DEVINTERFACE_HID. it gives me ERROR 31:- "A device attached to the system is not functioning." when it it executes CreateFile() method.

Code: [Select]
GUID_DEVINTERFACE_HID = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");

jain1.anuj

  • Member
  • ***
  • Posts: 28
Re: cant get handle to the USB HID??
« Reply #18 on: January 15, 2012, 11:45:42 pm »
Let me explain you about my device once again so that you must have a better understanding about it.

My device is an ultra-sonic proximity detector functioning as a USB keyboard. The primary purpose is for detecting users stepping away from a computer terminal they have un-locked (log-in active), and issuing keystrokes to lock or log-off the session on the computer. Now when you walk away from your system, you will benefit from a hands-free means of locking your computer.

I need both read/write operation on my device.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: cant get handle to the USB HID??
« Reply #19 on: January 16, 2012, 12:08:36 pm »
For read/write access other than keypresses and keyboard LEDs, make the device a generic HID, not a system keyboard. Define a report that indicates that the person has walked away. Write an application that logs off on receiving that report. I don't think you need keypresses to do that. Here is one example:

http://forums.techarena.in/software-development/1104154.htm

Use Output or Feature reports to send data of any kind to the device.

Jan

jain1.anuj

  • Member
  • ***
  • Posts: 28
Re: cant get handle to the USB HID??
« Reply #20 on: February 13, 2012, 11:01:00 pm »
Can you please elaborate your above post a little bit more ?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: cant get handle to the USB HID??
« Reply #21 on: February 14, 2012, 10:27:26 am »
My generic HID example code is here:

http://www.lvr.com/hidpage.htm#MyExampleCode

Jan