Z
zouduanke
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是一段C写的USB接口ID卡读卡器的读卡的程序,不知道转换成Delphi应该怎么做const USHORT DEVICE_VID = 0x0471;const USHORT DEVICE_PID = 0xA119;HANDLE GetDeviceViaInterface(const GUID *pGuid, DWORD instance){ HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); if(info == INVALID_HANDLE_VALUE) { SetLastError(ERROR_NO_MORE_ITEMS); return INVALID_HANDLE_VALUE; } SP_INTERFACE_DEVICE_DATA ifdata; ifdata.cbSize = sizeof(ifdata); if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata)) { SetupDiDestroyDeviceInfoList(info); SetLastError(ERROR_NO_MORE_ITEMS); return INVALID_HANDLE_VALUE; } DWORD ReqLen; SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]); if(!ifDetail) { SetupDiDestroyDeviceInfoList(info); SetLastError(ERROR_NO_MORE_ITEMS); return INVALID_HANDLE_VALUE; } ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if(!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) { delete ifDetail; SetupDiDestroyDeviceInfoList(info); SetLastError(ERROR_NO_MORE_ITEMS); return INVALID_HANDLE_VALUE; } HANDLE rv = CreateFile( ifDetail->DevicePath, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); delete ifDetail; SetupDiDestroyDeviceInfoList(info); return rv;}long InstallCommContext(){ GUID guidHid; DWORD i; HANDLE hDev; HidD_GetHidGuid(&guidHid); for(i=0; i<0xFFFFFFFF; ++i) { hDev = GetDeviceViaInterface(&guidHid, i); if(hDev == INVALID_HANDLE_VALUE) { if(GetLastError() == ERROR_NO_MORE_ITEMS) { break; } } else { HIDD_ATTRIBUTES clsAttr = {0}; clsAttr.Size = sizeof(HIDD_ATTRIBUTES); if(HidD_GetAttributes(hDev, &clsAttr)) { if((clsAttr.VendorID == DEVICE_VID) && (clsAttr.ProductID == DEVICE_PID)) { //找到了设备可以在此进行操作 } else { CloseHandle(hDev); } } } } return 0;}DWORD WINAPI fnThread(LPVOID lpParameter){ CDevice *p = (CDevice *)lpParameter; USHORT FeatureReportByteLength; PHIDP_PREPARSED_DATA pHpd; if(!HidD_GetPreparsedData(p->m_hDev, &pHpd)) { return 0; } HIDP_CAPS caps; if(HidP_GetCaps(pHpd, &caps) != HIDP_STATUS_SUCCESS) { HidD_FreePreparsedData(pHpd); return 0; } FeatureReportByteLength = caps.FeatureReportByteLength; HidD_FreePreparsedData(pHpd); unsigned char *pBuffer = new unsigned char[FeatureReportByteLength]; while(!InterlockedCompareExchange(&p->lExitThread, 1, 1)) { Sleep(100); pBuffer[0] = 0; if(HidD_GetFeature(p->m_hDev, pBuffer, FeatureReportByteLength)) { if((pBuffer[1] == 0xAA) && (pBuffer[2] == 5 /* 0 */ )) { // pBuffer[3] ~ pBuffer[7] 为ID字节 } } }}