Thanks JAN for your reply. If i use RAWINPUT than i will not be able to write data to the device. I want to perform both read/write operations.
here is the code for creating handle to the device with GUID as:-
GUID_DEVINTERFACE_KEYBOARD = new Guid("884b96c3-56ef-11d1-bc8c-00a0c91405dd");
create file method as :-
deviceHandle = CreateFile(devicePathName[0], 0/*GENERIC_WRITE | GENERIC_READ*/ , FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,
OPEN_EXISTING, 0, IntPtr.Zero);
code for getting capabilities and HID Usage is as follows:-
Capabilities = GetDeviceCapabilities(deviceHandle);
MessageBox.Show("capabilities: " + Capabilities.ToString());
hidUsage = GetHidUsage(Capabilities);
MessageBox.Show("usage : " + hidUsage);
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.StackTrace.ToString());
}
finally
{
if (preparsedData != IntPtr.Zero)
{
success = HidD_FreePreparsedData(preparsedData);
}
}
return Capabilities;
}
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;
}
code for read and write handle is as follows:-
readHandle = CreateFile(devicePathName[0],0 /*GENERIC_READ*/, 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());
Boolean res = HidD_FlushQueue(deviceHandle);
MessageBox.Show("flush quese: " + res.ToString());
}