读书人

DriverStudio 跟 WDF驱动 通过GUID获

发布时间: 2013-10-30 12:56:21 作者: rapoo

DriverStudio 和 WDF驱动 通过GUID获取设备句柄的差别

DriverStudio

/*****************************************************************************
* 功能: 通过GUID打开设备,获得设备句柄
* 参数:
*****************************************************************************/
HANDLE lOpenByInterface(
GUID* pClassGuid, // points to the GUID that identifies the interface class
DWORD instance, // specifies which instance of the enumerated devices to open
PDWORD pError // address of variable to receive error status
)
{
HANDLE hDev=0;

CDeviceInterfaceClass DevClass(pClassGuid, pError);

if (*pError != ERROR_SUCCESS)
return INVALID_HANDLE_VALUE;

CDeviceInterface DevInterface(&DevClass, instance, pError);

if (*pError != ERROR_SUCCESS)
return INVALID_HANDLE_VALUE;

hDev = CreateFile(
DevInterface.DevicePath(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if (hDev == INVALID_HANDLE_VALUE)
*pError = GetLastError();

return hDev;
}

//WDF驱动

HANDLE CxDriverInterface::SetupDevInterfaces(DWORD dev_interface_index, int board_type)
{
HANDLE hd_invalid = INVALID_HANDLE_VALUE;
DWORD required_buf_size;
GUID *pGuid = (LPGUID)&GUID_DEVINTERFACE_ATHENA;

if ( board_type == Athena_EVK )
{
pGuid = (LPGUID)&GUID_DEVINTERFACE_ATHENA;
}
else if( (board_type == Atlas_Plus_EVK) || (board_type == Atlas_EVK) )
{
pGuid = (LPGUID)&GUID_DEVINTERFACE_ATLAS;
}

//Get handle to our device information set that contains requested device information elements
HDEVINFO devInfoSetHandle = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if ( devInfoSetHandle == INVALID_HANDLE_VALUE )
{
TRACE("SetupDiGetClassDevs failed to find device GUID in system! \n");
return hd_invalid;
}

SP_INTERFACE_DEVICE_DATA devInterfaceInfo;
devInterfaceInfo.cbSize = sizeof(devInterfaceInfo);

// Try to enumerate the device interfaces that are contained in device information set just retrieved
if( !SetupDiEnumDeviceInterfaces( devInfoSetHandle, NULL, pGuid, dev_interface_index, &devInterfaceInfo ) )
{
TRACE("SetupDiEnumDeviceInterfaces failed to enumerate device GUID! \n");
SetupDiDestroyDeviceInfoList(devInfoSetHandle);
return hd_invalid;
}

// Get the required buffer size and allocate proper sized buffer
SetupDiGetDeviceInterfaceDetail( devInfoSetHandle, &devInterfaceInfo, NULL, 0, &required_buf_size, NULL );
PSP_INTERFACE_DEVICE_DETAIL_DATA devInterfaceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (required_buf_size);

if( devInterfaceDetail == NULL )
{
TRACE("SetupDiGetDeviceInterfaceDetail failed! \n");
SetupDiDestroyDeviceInfoList( devInfoSetHandle );
return hd_invalid;
}

// Get details for the device interface
devInterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if( !SetupDiGetDeviceInterfaceDetail( devInfoSetHandle, &devInterfaceInfo, devInterfaceDetail, required_buf_size, NULL, NULL) )
{
TRACE("SetupDiGetDeviceInterfaceDetail failed to set SP_INTERFACE_DEVICE_DETAIL_DATA! \n");
SetupDiDestroyDeviceInfoList( devInfoSetHandle );
delete devInterfaceDetail;
return hd_invalid;
}
//前面这部分在Driverworks中用两个类来完成
// Get device handle
LPCWSTR dev_path = devInterfaceDetail->DevicePath;
HANDLE dev_hd = CreateFile( devInterfaceDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if( dev_hd == INVALID_HANDLE_VALUE )
{
TRACE("Failed to create device handleB!");
exit(1);
}

delete devInterfaceDetail;

if ( devInfoSetHandle )
SetupDiDestroyDeviceInfoList( devInfoSetHandle );

return dev_hd;
}

读书人网 >编程

热点推荐