以前从没有做过USB,头给我一个Cypress的Ezusb的控制面板程序,让我读然后模仿写一个自己的应用程序,我看的头大了,当我我模仿写到获取字符串信息时,原程序发送了两次DeviceIoControl命令,为什么呢要两次呢?原代码如下:
char temp[64] = " ";
GET_STRING_DESCRIPTOR_IN input;
pvBuffer = malloc (sizeof (Usb_String_Descriptor) + 128);
if (bOpenDriver (&hDevice, pcDriverName) != TRUE) // Open the driver
{
显示信息
}
#ifdef USING_MFC
input.Index = (UCHAR) pTh-> index;
input.LanguageId = 27; // NOTE: frameworks ignores it anyway
wsprintf (temp, "GetStringDescripter: Index=%d ", input.Index);
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)emp);
#endif
// Get the first bytes of the descriptor to determine the size of
// the entire descriptor.
if (hDevice != NULL)
{// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_STRING_DESCRIPTOR,
&input,
sizeof (GET_STRING_DESCRIPTOR_IN),
pvBuffer,
sizeof (Usb_String_Descriptor),
(unsigned long *)&nBytes,
NULL);
}/* if valid driver handle */
if (bResult!=TRUE)
{// This will happen with no string defined
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM) "Get String Descriptor Failed ");
//ShowSystemError(hOutputBox);
CloseHandle (hDevice);
free (pvBuffer);
}/* if */
ulLength = GET_STRING_DESCRIPTOR_LENGTH(pvBuffer);
assert (ulLength > = 0);
// Now get the entire descriptor
pvBuffer = realloc (pvBuffer, ulLength);
if (pvBuffer)
{
// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_STRING_DESCRIPTOR,
&input,
ulLength,
pvBuffer,
ulLength,
(unsigned long *)&nBytes,
NULL);
if (bResult==TRUE)
{
显示信息
} |