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
    评论
CyAPI原版资料, CyAPI.lib provides a simple, powerful C++ programming interface to USB devices. More specifically, it is a C++ class library that provides a high-level programming interface to the CyUsb3.sys device driver. The library is only able to communicate with USB devices that are served by (i.e. bound to) this driver. Rather than communicate with the driver via Windows API calls such as SetupDiXxxx and DeviceIoControl, applications can call simpler CyAPI methods such as Open, Close, and XferData to communicate with these USB devices. To use the library, you need to include the header file, CyAPI.h, in files that access the CCyUSBDevice class. In addition, the statically linked CyAPI.lib file must be linked to your project. Versions of the .lib files are available for use with Microsoft Visual Studio 2008. The library employs a Device and EndPoints use model. To use the library you must create an instance of the CCyUSBDevice class using the new keyword. A CCyUSBDevice object knows how many USB devices are attached to the CyUsb3.sys driver and can be made to abstract any one of those devices at a time by using the Open method. An instance of CCyUSBDevice exposes several methods and data members that are device-specific, such as DeviceName, DevClass, VendorID, ProductID, and SetAltIntfc. When a CCyUSBDevice object is open to an attached USB device, its endpoint members provide an interface for performing data transfers to and from the device's endpoints. Endpoint-specific data members and methods such as MaxPktSize, TimeOut, bIn, Reset and XferData are only accessible through endpoint members of a CCyUSBDevice object. In addition to its simplicity, the class library facilitates creation of sophisticated applications as well. The CCyUSBDevice constructor automatically registers the application for Windows USB Plug and Play event notification. This allows your application to support "hot plugging" of devices. Also, the asynchronous BeginDataXfer/WaitForXfer/FinishDataXfer methods allow queuing of multiple data transfer requests on a single endpoint, thus enabling high performance data streaming from the application level
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值