cyapi理解

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值