如何让device生成kenerl name?
我直到的有两种,一种使用GUID,在驱动当中使用IoRegisterDeviceInterface函数注册接口,在App当中通过枚举使用该接口,可以看一看Cris Cant的
GetDeviceViaInterface(GUID *pGUID, DWORD Instance)
{
HDEVINFO hInfo=SetupDiGetClassDevs(pGUID,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if(hInfo==INVALID_HANDLE_VALUE)
{
AfxMessageBox(/"NO HDEVINFO available for this GUID/");
}
SP_INTERFACE_DEVICE_DATA IfData;
IfData.cbSize=sizeof(IfData);
if(!SetupDiEnumDeviceInterfaces(hInfo,NULL,pGUID,Instance,&IfData))
{
AfxMessageBox(/"NO SP_INTERFACE_DEVICE_DATA availabel for this GUID Instance/");
SetupDiDestroyDeviceInfoList(hInfo);
return NULL;
}
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(hInfo,&IfData,NULL,0,&ReqLen,NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA pIfDetail=(PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
if(pIfDetail==NULL)
{
SetupDiDestroyDeviceInfoList(hInfo);
return NULL;
}
pIfDetail->cbSize=sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetDeviceInterfaceDetail(hInfo,&IfData,pIfDetail,ReqLen,NULL,NULL))
{
SetupDiDestroyDeviceInfoList(hInfo);
delete(pIfDetail);
return NULL;
}
HANDLE hFile= CreateFile(pIfDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hInfo);
delete(pIfDetail);
return NULL;
}
else
{
SetupDiDestroyDeviceInfoList(hInfo);
delete(pIfDetail);
return hFile;
}
return NULL;
}
另一种直接IoCreateSymbolicLink创建符号连接,在App当中直接用CreateFile象文件名一样使用,以下是该种用法的AddDevice Pnp 函数:
#define NT_DEVICE_NAME L/"DeviceWTCtrl/"
#define SYM_LINK_NAME L/"DosDevicesWTCtrl/"
NTSTATUS WTCtrlAddDevice(IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT pdo)
{
DebugPrint(/"WTCtrl Add Device/");
NTSTATUS status;
PDEVICE_OBJECT fdo;
UNICODE_STRING DeviceName, LinkName;
RtlInitUnicodeString( &DeviceName, NT_DEVICE_NAME);
RtlInitUnicodeString( &LinkName, SYM_LINK_NAME);
// Create our Functional Device Object in fdo
status = IoCreateDevice (DriverObject,
sizeof(WTCTRL_DEVICE_EXTENSION),
&DeviceName,
FILE_DEVICE_UNKNOWN,
0,
FALSE, // Not exclusive
&fdo);
if( !NT_SUCCESS(status))
return status;
// Remember fdo in our device extension
PWTCTRL_DEVICE_EXTENSION pdx = (PWTCTRL_DEVICE_EXTENSION)fdo->DeviceExtension;
pdx->fdo = fdo;
DebugPrint(/"FDO is %x/",fdo);
IoSetDeviceInterfaceState(&pdx->ifSymLinkName, TRUE);
// DebugPrint(/"Symbolic Link Name is %T/",&pdx->ifSymLinkName);
DebugPrint(/"------%T/",&LinkName);
status = IoCreateSymbolicLink( &LinkName, &DeviceName);
if( !NT_SUCCESS(status))
{
DebugPrintMsg(/"Could not create symbolic link/");
IoDeleteDevice(pdo);
return status;
}
else
{
DebugPrintMsg(/"Create symbolic link successful/");
}
// Attach to the driver stack below us
pdx->LowerStackDevice = IoAttachDeviceToDeviceStack(fdo,pdo);
// Set fdo flags appropriately
fdo->Flags |= DO_BUFFERED_IO|DO_POWER_PAGABLE;
fdo->Flags &= ~DO_DEVICE_INITIALIZING;
PHYSICAL_ADDRESS PhysicalAddress=RtlConvertLongToLargeInteger(0xF0000);
pdx->pMemBase=MmMapIoSpace(
PhysicalAddress,
0xF0000,
MmNonCached
);
if(!pdx->pMemBase)
{
DebugPrint(/"WTCTRL MmMapIoSpace Failed!/");
}
else
{
DebugPrint(/"WTCTRL MmMapIoSpace Succeded!/");
}
return STATUS_SUCCESS;
}
[编辑 - 1/5/02 作者: abeh]