olivia527
驱动牛犊
驱动牛犊
  • 注册日期2004-05-27
  • 最后登录2004-11-16
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1534回复:4

为什么USB应用程序中不能对设备进行写的操作

楼主#
更多 发布于:2004-07-22 11:47
Result = WriteFile(hiddevice, SendBuffer(0), CLng(Capabilities.OutputReportByteLength), _
                   NumberOfBytesWritten, 0)

参数都有效,但结果就是失败,NumberOfBytesWritten为0
是不是与写入设备的数据格式有关啊,
USB KEYBOARD有专门的格式吗?
zhongsion
驱动小牛
驱动小牛
  • 注册日期2002-09-29
  • 最后登录2010-02-01
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望11点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-07-27 10:39
关注中。。。。
billy2004
驱动牛犊
驱动牛犊
  • 注册日期2004-07-03
  • 最后登录2005-01-13
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-07-27 10:21
不好意思,我问你个问题,好吗?

我想问你是怎么得到 USB 键盘的,为何我用下面的代码 老是返回错误呢?

 

好象是GUID获取的不对
leadphone
驱动牛犊
驱动牛犊
  • 注册日期2002-11-28
  • 最后登录2008-09-04
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-07-26 10:16
如果是WIN2K,USB鼠标和键盘好像不能通过访问普通USB设备那样打开
Deauty
驱动牛犊
驱动牛犊
  • 注册日期2002-11-27
  • 最后登录2008-10-12
  • 粉丝0
  • 关注0
  • 积分5分
  • 威望17点
  • 贡献值0点
  • 好评度13点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2004-07-22 14:28
不好意思,我问你个问题,好吗?

我想问你是怎么得到 USB 键盘的,为何我用下面的代码 老是返回错误呢?

BOOL OpenUserKeyboard ()
{
// Search through the attached HID devices for the User Keyboard
// I used a keyboard with a VID = 046E and PID = 6782,  your's may be different
// See TKFILTER.SYS documentation which describes how to create a "User Keyboard"

// Declare the local data structures used
struct _GUID HidGuid;
SP_INTERFACE_DEVICE_DATA DeviceInterfaceData;
struct
{
DWORD cbSize;
char DevicePath[256];

} FunctionClassDeviceData;

int Success, HidDevice;
BOOL Openned;
HANDLE PnPHandle;
unsigned long BytesReturned;
struct _HIDD_ATTRIBUTES Attributes;
struct _HIDP_PREPARSED_DATA *PreparsedData;
struct _HIDP_CAPS Capabilities;

// First, get my class identifier
HidD_GetHidGuid(&HidGuid);
// Get a handle for the Plug and Play node and request currently active HID devices
PnPHandle = SetupDiGetClassDevs(&HidGuid,0,0,0x12);

if ((int)PnPHandle == -1)
return DisplayError("Could not attach to PnP node");
Openned = false;

// Lets look for a maximum of 20 HID devices
for (HidDevice = 0; (HidDevice < 20) && !Openned; HidDevice++)
{
// Give the user some feedback
MainWindow[MainWindowIndex++] = 45;  // "-"
InvalidateRect(hWnd, NULL, 1);
// Initialize our Data
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA ); // Length of data structure in bytes

// Is there a HID device at this table entry
Success = SetupDiEnumDeviceInterfaces(PnPHandle, 0, &HidGuid, HidDevice, &DeviceInterfaceData);
if (Success == 1)
{
// There is a device here, get it's name
FunctionClassDeviceData.cbSize = 5;
ZeroMemory(FunctionClassDeviceData.DevicePath,sizeof(char)*256);

Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, &DeviceInterfaceData,
PSP_INTERFACE_DEVICE_DETAIL_DATA(&FunctionClassDeviceData), 256, &BytesReturned, 0);

if (Success == 0)
return DisplayError("Could not find the system name for this HID device");

char atemp[256];
strcpy(atemp,FunctionClassDeviceData.DevicePath);

ZeroMemory(FunctionClassDeviceData.DevicePath,sizeof(char)*256);

strcpy(FunctionClassDeviceData.DevicePath,"\\");
strcat(FunctionClassDeviceData.DevicePath,"\\");
strcat(FunctionClassDeviceData.DevicePath,"\\");
strcat(FunctionClassDeviceData.DevicePath,"\\");
strcat(FunctionClassDeviceData.DevicePath,".");
strcat(FunctionClassDeviceData.DevicePath,"\\");
strcat(FunctionClassDeviceData.DevicePath,"\\");

for(int iCount=0;iCount<240;iCount++)
{
FunctionClassDeviceData.DevicePath[7+iCount]=atemp[4+iCount];
}

// Can now open this HID device
HidHandle = CreateFile(FunctionClassDeviceData.DevicePath,
GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

if (HidHandle == INVALID_HANDLE_VALUE)
return DisplayError("Could not open HID device");//到这个地方程序返回错误,为何?

// Is it OUR HID device?
if (!HidD_GetAttributes(HidHandle, &Attributes))
return DisplayError("Could not get VID/PID");
if ((Attributes.VendorID == 0x046e) && (Attributes.ProductID == 0x6782))
{
// Note that most keyboards are composite desktop devices,
//must find the "keyboard" usage (set to 0 by TKFILTER.SYS)
if (!HidD_GetPreparsedData(HidHandle, &PreparsedData))
return DisplayError("Could not get Preparsed Data");

if (!HidP_GetCaps(PreparsedData, &Capabilities))
return DisplayError("Could not get device capabilities");

if ((Capabilities.UsagePage == 1) && (Capabilities.Usage == 0))
Openned = true;
else
CloseHandle(HidHandle);
HidD_FreePreparsedData(PreparsedData);
}
} // if (SetupDiEnumDeviceInterfaces . .
} // for (HidDevice = 0; (HidDevice < 20) && !Openned; HidDevice++)
CloseHandle(PnPHandle);
return Openned;
}
游客

返回顶部