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
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. :-
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;
}