阅读:1307回复:1
D12测试应用程序Device.cpp中两次用到CreateFile各是什么作用?(付源码)
两个CreateFile函数的作用是什么?我想应该是得到每个管道的句柄,那位大侠能说的具体点?函数是如何利用\"pipename\"找到相应的管道句柄的?急盼指教!!
char completeDeviceName[256] = \"\"; //generated from the GUID registered by the driver itself HANDLE OpenOneDevice ( IN HDEVINFO HardwareDeviceInfo, IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData, IN char *devName ) { PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL; ULONG predictedLength = 0; ULONG requiredLength = 0; HANDLE hOut = INVALID_HANDLE_VALUE; SetupDiGetInterfaceDeviceDetail ( HardwareDeviceInfo, DeviceInfoData, NULL, // probing so no output buffer yet 0, // probing so output buffer length of zero &requiredLength, NULL); // not interested in the specific dev-node predictedLength = requiredLength; // sizeof (SP_FNCLASS_DEVICE_DATA) + 512; functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc (predictedLength); functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA); // // Retrieve the information from Plug and Play. // if (! SetupDiGetInterfaceDeviceDetail ( HardwareDeviceInfo, DeviceInfoData, functionClassDeviceData, predictedLength, &requiredLength, NULL)) { return INVALID_HANDLE_VALUE; } strcpy( devName,functionClassDeviceData->DevicePath) ; hOut = CreateFile ( functionClassDeviceData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // No special attributes NULL); // No template file if (INVALID_HANDLE_VALUE == hOut) { } free(functionClassDeviceData); return hOut; } HANDLE OpenUsbDevice( LPGUID pGuid, char *outNameBuf) { ULONG NumberDevices; HANDLE hOut = INVALID_HANDLE_VALUE; HDEVINFO hardwareDeviceInfo; SP_INTERFACE_DEVICE_DATA deviceInfoData; ULONG i; BOOLEAN done; PUSB_DEVICE_DESCRIPTOR usbDeviceInst; PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst; *UsbDevices = NULL; NumberDevices = 0; // // Open a handle to the plug and play dev node. // SetupDiGetClassDevs() returns a device information set that contains info on all // installed devices of a specified class. // hardwareDeviceInfo = SetupDiGetClassDevs ( pGuid, NULL, // Define no enumerator (global) NULL, // Define no (DIGCF_PRESENT | // Only Devices present DIGCF_INTERFACEDEVICE)); // Function class devices. // // Take a wild guess at the number of devices we have; // Be prepared to realloc and retry if there are more than we guessed // NumberDevices = 4; done = FALSE; deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA); i=0; while (!done) { NumberDevices *= 2; if (*UsbDevices) { *UsbDevices = (PUSB_DEVICE_DESCRIPTOR)realloc (*UsbDevices, (NumberDevices * sizeof (USB_DEVICE_DESCRIPTOR))); } else { *UsbDevices = (PUSB_DEVICE_DESCRIPTOR)calloc (NumberDevices, sizeof (USB_DEVICE_DESCRIPTOR)); } if (NULL == *UsbDevices) { // SetupDiDestroyDeviceInfoList destroys a device information set // and frees all associated memory. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return INVALID_HANDLE_VALUE; } usbDeviceInst = *UsbDevices + i; for (; i < NumberDevices; i++) { // SetupDiEnumDeviceInterfaces() returns information about device interfaces // exposed by one or more devices. Each call returns information about one interface; // the routine can be called repeatedly to get information about several interfaces // exposed by one or more devices. if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 0, // We don\'t care about specific PDOs pGuid, i, &deviceInfoData)) { hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, outNameBuf); if ( hOut != INVALID_HANDLE_VALUE ) { done = TRUE; break; } } else { if (ERROR_NO_MORE_ITEMS == GetLastError()) { done = TRUE; break; } } } } NumberDevices = i; // SetupDiDestroyDeviceInfoList() destroys a device information set // and frees all associated memory. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); if (*UsbDevices) free(*UsbDevices); return hOut; } BOOL GetUsbDeviceFileName( LPGUID pGuid, char *outNameBuf) { HANDLE hDev = OpenUsbDevice( pGuid, outNameBuf ); if ( hDev != INVALID_HANDLE_VALUE ) { CloseHandle( hDev ); return TRUE; } return FALSE; } HANDLE open_dev() { HANDLE hDEV = OpenUsbDevice( (LPGUID)&GUID_CLASS_D12_BULK, completeDeviceName); return hDEV; } HANDLE open_file( char *pipename) { HANDLE h; if ( !GetUsbDeviceFileName( (LPGUID) &GUID_CLASS_D12_BULK, completeDeviceName) ) { return INVALID_HANDLE_VALUE; } strcat (completeDeviceName, \"\\\\\" ); strcat (completeDeviceName, pipename ); h = CreateFile(completeDeviceName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, // FILE_FLAG_OVERLAPPED, 0, NULL); return h; } |
|
沙发#
发布于:2004-06-09 09:15
两个CreateFile函数的作用是什么?我想应该是得到每个管道的句柄,那位大侠能说的具体点?函数是如何利用"pipename"找到相应的管道句柄的?急盼指教!!
****************************** 我的理解:CreateFile仅仅是打开设备,管道句柄是在D12的设备驱动中StartDevice例程里获得的,保存在DeviceObject结构中,同时设备驱动在StartDevice例程中调用了D12_BuildPipeList,为每个被发现的管道依次取了名字(pipename),应用程序利用pipename指定管道,设备驱动根据这个pipename调出相应的管道句柄. |
|