cyapi来自cyapi类库。即,使用已有的代码访问cysub.sys驱动
cyusb.sys
通过DeviceIoControl()访问cyusb.sys
获取设备名子:
DWORD dwBytes=0;
ULONG len=256;
UCHAR *buf=new UCHAR[len];
BOOL cyusbStatus=DeviceIoControl(hDevice,IOCTL_ADAPT_GET_DEVICE_NAME,
buf,len,buf,len,&dwBytes,NULL);
delete [] buf;
DWORD BytesXfered;
SET_TRANSFER_SIZE_INFO SetTransferInfo;
SetTransferInfo.EndpointAddress=0x02;
DeviceIoControl(hDevice,IOCTL_ADAPT_GET_TRANSFER_SIZE,
&SetTransferInfo,sizeof(SET_TRANSFER_SIZE_INFO),
&SetTransferInfo,sizeof(SET_TRANSFER_SIZE_INFO),
&BytesXfered,NULL);
枚举usb设备:
///enumerate the possible cyusb devices
static GUID CYUSBDRV_GUID = {0xae18aa60, 0x7f6a, 0x11d4, 0x97, 0xdd, 0x0, 0x1, 0x2, 0x29, 0xb9, 0x59};
DWORD modules_size=0;
// Handle , 设备信息集,包含被请求设备信息元素,在本地电脑上。
HDEVINFO hwDeviceInfo = SetupDiGetClassDevs((LPGUID) &CYUSBDRV_GUID, //设备安装类或接口类GUID指针。
NULL, ///Pnp枚举器标识符. 'USB"
NULL, /// 顶层窗口句柄.安装设备实例关联的用户接口的窗口
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE ///i当前存在的设备
);
if (hwDeviceInfo != INVALID_HANDLE_VALUE)
{
// devInterfaceData
SP_DEVICE_INTERFACE_DATA devInterfaceData;
devInterfaceData.cbSize = sizeof(devInterfaceData);
while( (modules_size< 250) && (SetupDiEnumDeviceInterfaces(hwDeviceInfo, 0, (LPGUID) &CYUSBDRV_GUID,modules_size, &devInterfaceData)))
{
// deviceInfoData Struktur vorbereiten
SP_DEVINFO_DATA deviceInfoData;
memset(&deviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
// deviceInterfaceDetailData
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData;
ULONG requiredLength = 0, reservedLength = 0, realLength = 0;
if (!SetupDiGetDeviceInterfaceDetail(hwDeviceInfo,
&devInterfaceData,
NULL,
0,
&requiredLength,
NULL)
)
{
int errorCode = GetLastError();
if (errorCode != ERROR_INSUFFICIENT_BUFFER)
{
perror("ERROR: SetupDiGetDeviceInterfaceDetail()[1] failed - ");
printf("ERROR CODE %d (0x%X)\n", errorCode, errorCode);
//FreeModuleList();
delete deviceInterfaceDetailData;
//return ERR_XXX_SETUPAPI;
return 0;
}
}
/ the above code was intended to get the buffer size for which the next api need.//
reservedLength = requiredLength;
deviceInterfaceDetailData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) new char[reservedLength];
memset(deviceInterfaceDetailData, 0, reservedLength);
deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
// Details , c++ formal, is there any another reason?
realLength = reservedLength;
if (SetupDiGetDeviceInterfaceDetail(hwDeviceInfo, &devInterfaceData, deviceInterfaceDetailData, reservedLength, &realLength, &deviceInfoData))
{
// Modul
//XXXModule* newModule = new XXXModule((int)modules.size(), deviceInterfaceDetailData->DevicePath);
//modules.push_back(newModule);
dwCount++;
pCommList[n].nPort=(USHORT)n + 384 +1;
_tcscpy(pCommList[n].szDevicePath,deviceInterfaceDetailData->DevicePath); /// no error checking!
n++;
} else
{
int error = ::GetLastError();
// std::cerr << "XXXEnumerator::UpdateModuleList: SetupDiGetDeviceInterfaceDetail failed with code " << error << " \"" << ErrorMessage(error) << "\"" << std::endl;
//FreeModuleList();
//return ERR_XXX_SETUPAPI;
return 0;
}
delete deviceInterfaceDetailData;
deviceInterfaceDetailData = NULL;
modules_size++;
}
SetupDiDestroyDeviceInfoList(hwDeviceInfo);
//return ERR_XXX_OK;
return (USHORT)dwCount;
} else
{
//return ERR_XXX_NODRIVER;
return 0;
}