阅读:1279回复:2
各位兄弟看过来,怎么解决这个问题?(windriver)
我在做一个项目,usb key-市面上的那种电子钥匙,在作上层应用时,需要频繁插拔key,此时存在一个查找key地过程,我直接调用windriver生成的一个函数,如下:
KEY_HANDLE KEY_LocateAndOpenDevice(BOOL fUseDefault) { KEY_HANDLE hKEY; DWORD uniqueId = 0, configNum = 0, dwInterface = 0, dwAlternate = 0; WD_USB_SCAN_DEVICES usbScan; WD_USB_CONFIGURATION config; HANDLE hWD; DWORD scanIndex; KEY_RegisterWinDriver(); if (!USB_Get_WD_handle(&hWD)) return NULL; BZERO(usbScan); if (fUseDefault) { usbScan.searchId.dwVendorId = KEY_DEFAULT_VENDOR_ID; usbScan.searchId.dwProductId = KEY_DEFAULT_PRODUCT_ID; } WD_UsbScanDevice(hWD, &usbScan); if (!usbScan.dwDevices) { printf (\"No deviced found\\n\"); WD_Close(hWD); return NULL; } if (usbScan.dwDevices>1 && !fUseDefault) { printf (\"Please enter the unique ID of the device: \"); fgets(line, sizeof(line), stdin); sscanf (line, \"%d\", &uniqueId); if (!uniqueId) return NULL; for (scanIndex=0; scanIndex<usbScan.dwDevices; scanIndex++) { if (usbScan.uniqueId[scanIndex]==uniqueId) break; } if (scanIndex==usbScan.dwDevices) { printf (\"Unique ID not found\\n\"); WD_Close(hWD); return NULL; } } else { uniqueId = usbScan.uniqueId[0]; scanIndex = 0; } if (fUseDefault) { configNum = KEY_DEFAULT_CONFIG; dwInterface = KEY_DEFAULT_INTERFACE; dwAlternate = KEY_DEFAULT_ALTERNATE; } else { configNum = 0; if (usbScan.deviceGeneralInfo[scanIndex].dwConfigurationsNum>1) { printf (\"Please enter the index of the configuration: (0-%d)\"), usbScan.deviceGeneralInfo[scanIndex].dwConfigurationsNum - 1; fgets(line, sizeof(line), stdin); sscanf (line, \"%d\", &configNum); } BZERO(config); config.uniqueId = uniqueId; config.dwConfigurationIndex = configNum; WD_UsbGetConfiguration(hWD, &config); if (config.dwInterfaceAlternatives==0) { printf (\"No interfaces found\\n\"); WD_Close(hWD); return NULL; } if (config.dwInterfaceAlternatives==1) { dwInterface = config.Interface[0].Interface.dwNumber; dwAlternate = config.Interface[0].Interface.dwAlternateSetting; } else { printf (\"Select \'Display Device Configurations\' in the main menu to learn\\n\"); printf (\"about the interfaces of the selected device\\n\"); printf (\"Please enter interface number: \"); fgets(line, sizeof(line), stdin); sscanf (line, \"%d\", &dwInterface); printf (\"Please enter interface alternate: \"); fgets(line, sizeof(line), stdin); sscanf (line, \"%d\", &dwAlternate); } } hKEY = KEY_Open (uniqueId, configNum, dwInterface, dwAlternate); if (!hKEY) { printf (\"%s\\n\", KEY_ErrorString); WD_Close(hWD); return NULL; } WD_Close(hWD); return hKEY; } 通过看hKEY是否为空来判断是否有KEY,但在2000系统中运行时,如果我插拔KEY时正好调用该函数就会造成兰屏,请问我该怎么办?事情紧急,请各位兄弟帮个忙,重谢加分 |
|
沙发#
发布于:2002-05-28 22:15
key拔去时,hKEY不一定为空,有可能是一个无效的句柄呀。
|
|
板凳#
发布于:2002-05-29 18:48
key拔去时,hkey不为空,怎么把释放掉?
|
|