马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
下面是访问 USB 口必备的函数: #include <vcl.h>
#include <dir.h> #include <setupapi.h> #include "C:/WINDDK/3790/inc/ddk/w2k/usbdi.h" #include "C:/WINDDK/3790/inc/ddk/w2k/devioctl.h" #include <initguid.h> //--------------------------------------------------------------------------- // 下面必须为驱动程序的 GUID 值, 这里我乱写的数 DEFINE_GUID(USB_DRIVER_GUID, 0x12345678,0xabcd,0x1122,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00); //--------------------------------------------------------------------------- HANDLE OpenOneDevice(HDEVINFO hDvcInfo, PSP_INTERFACE_DEVICE_DATA DvcInfoData, char *sDevNameBuf) { HANDLE hOut = INVALID_HANDLE_value; ULONG iReqLen = 0; SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, NULL, 0, &iReqLen, NULL); ULONG iDevDataLen = iReqLen; //sizeof(SP_FNCLASS_DEVICE_DATA) + 512; PSP_INTERFACE_DEVICE_DETAIL_DATA pDevData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(iDevDataLen); pDevData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if(SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, pDevData, iDevDataLen, &iReqLen, NULL)) { strcpy(sDevNameBuf, pDevData->DevicePath); hOut = CreateFile(pDevData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); } free(pDevData); return hOut; } //--------------------------------------------------------------------------- HANDLE OpenUsbDevice(const GUID *pGuid, char *sDevNameBuf) { HANDLE hOut = INVALID_HANDLE_value; HDEVINFO hDevInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); SP_INTERFACE_DEVICE_DATA deviceInfoData; deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA); ULONG nGuessCount = MAXLONG; for(ULONG iDevIndex=0; iDevIndex<nGuessCount; iDevIndex++) { if(SetupDiEnumDeviceInterfaces(hDevInfo, 0, pGuid, iDevIndex, &deviceInfoData)) { if((hOut=OpenOneDevice(hDevInfo, &deviceInfoData, sDevNameBuf)) != INVALID_HANDLE_value) break; } else if(GetLastError() == ERROR_NO_MORE_ITEMS) //No more items { break; } } SetupDiDestroyDeviceInfoList(hDevInfo); return hOut; } //--------------------------------------------------------------------------- bool GetUsbDeviceFileName(const GUID *pGuid, char *sDevNameBuf) { HANDLE hDev = OpenUsbDevice(pGuid, sDevNameBuf); if(hDev != INVALID_HANDLE_value) { CloseHandle(hDev); return true; } return false; } //--------------------------------------------------------------------------- HANDLE OpenMyDevice() { char DeviceName[MAXPATH] = ""; return OpenUsbDevice(&USB_DRIVER_GUID, DeviceName); } //--------------------------------------------------------------------------- HANDLE OpenMyDevPipe(const char *PipeName) { char DeviceName[MAXPATH] = ""; if(GetUsbDeviceFileName(&USB_DRIVER_GUID, DeviceName)) { strcat(DeviceName,"\\"); strcat(DeviceName,PipeName); return CreateFile(DeviceName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); } return INVALID_HANDLE_value; } //--------------------------------------------------------------------------- 有了上面的函数就可以访问 USB 口了://打开 USB 口读写, 由驱动程序的 Pipe 名确定 HANDLE hPipe = OpenMyDevPipe("MyPipe1"); //驱动程序里面的 Pipe 名, 对应访问某个端点的 I/O, 这里我乱写的, 需要与驱动一致 if(hPipe != INVALID_HANDLE_value) //打开 Pipe 成功 { ReadFile(hPipe, Buffer, BufSize, &nBytesRead, NULL); //从 hPipe 里读取数据到 Buffer 里 //WriteFile(hPipe, Buffer, BytesToWrite, &nBytesWritten, NULL); //把 Buffer 里面的 BytesToWrite 字节写入 hPipe CloseHandle(hPipe); } //使用 DeviceIoControl 访问 USB 设备 HANDLE hDevice = OpenMyDevice(); if(hDevice != INVALID_HANDLE_value) //打开设备成功 { //这些 DeviceIoControl 功能 |