How to Retrieve PNP DeviceId in file system driver?

  • Thread starter Thread starter CodeworkDEV
  • Start date Start date
C

CodeworkDEV

Guest
Hi All,

We have a file system driver which will be used to identify USB device uniquely, for that we are planning to use PNPDeviceId as a unique identifier. We have tried to retrieve it using STORAGE_DEVICE_DESCRIPTOR but this doesn't give any information about the PNPDeviceID. If anyone knows how to retrieve PNPDeviceID for storage devices using the device object, please share your sample code. Here is our sample code:


NTSTATUS GetDeviceTypeAndUniqueID(IN PDEVICE_OBJECT StorageStackDeviceObject, cwDevices *<g class="gr_ gr_623 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="623" id="623">lDeviceType</g>, char *pszUniqueID)
{
STORAGE_PROPERTY_QUERY Query;
char szBuff[1024];
IO_STATUS_BLOCK IoStatus;
KEVENT WaitEvent;

NTSTATUS Status = STATUS_SUCCESS;
ULONG uBusType=BusTypeUnknown;
char *szSptr="_";
SIZE_T szLength = 0;
PSTORAGE_DEVICE_DESCRIPTOR Descriptor=NULL;
PIRP NewIrp;
*lDeviceType=cwUNKNOWN;
pszUniqueID[0]=0;
memset(szBuff,0,1024);
Query.PropertyId = StorageDeviceProperty;// first set the query properties
Query.QueryType = PropertyStandardQuery;

if (KeGetCurrentIrql() > PASSIVE_LEVEL)
goto ERROR_HANDLER;

KeInitializeEvent(&WaitEvent, NotificationEvent, FALSE);// initialize the waitable event

//we should build the query irp ourselves, Irp sent object here is the real need of equipment
NewIrp = IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY,
StorageStackDeviceObject,
(PVOID)&Query,
sizeof(STORAGE_DEVICE_DESCRIPTOR),
szBuff,
1024,
FALSE,
&WaitEvent,
&IoStatus);

if (NewIrp==NULL) //can't create new irp.
goto ERROR_HANDLER;

IoSetCompletionRoutine(NewIrp, MyOnIrpComplete, (PVOID)&WaitEvent, TRUE, TRUE, TRUE);
Status = IoCallDriver(StorageStackDeviceObject, NewIrp);// send this irp to the storage device
if (Status == STATUS_PENDING)
{
LARGE_INTEGER TimeOut;

TimeOut.QuadPart=-50000000;//5 Seconds
Status = KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, &TimeOut);

if(Status==STATUS_TIMEOUT)
{
IoCancelIrp(NewIrp);
KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, NULL);
}
}

KeClearEvent(&WaitEvent);

IoCompleteRequest(NewIrp, IO_NO_INCREMENT);

if (NT_SUCCESS(Status))
{
if(szBuff!=NULL)
{
try
{
Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)szBuff;
Descriptor->Size = sizeof(Descriptor);
uBusType = Descriptor->BusType;//Get the bus type.
}
except (EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
}
}

if (NT_SUCCESS(Status))
{
if(Descriptor !=NULL)
{
char pszStart[255];
if(Descriptor->VendorIdOffset!=0)//Is Valid VendorIDOffset, append it to the ID
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(unsigned long)Descriptor+Descriptor->VendorIdOffset),255);
if(pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart,255,&szLength);
if(szLength > 0)
{
RtlStringCopyWorkerA(pszStart,255,pszStart,255);
RtlStringCopyWorkerA(pszUniqueID,255,pszStart,255);
}
}
}
if(Descriptor->ProductIdOffset!=0)//Is Valid ProductIDOffset, append it to the ID
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(unsigned long)Descriptor+Descriptor->ProductIdOffset),255);
if(pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart,255,&szLength);
if(szLength > 0)
{
RtlStringCbCatExA(pszUniqueID,255,szSptr);
RtlStringCopyWorkerA(pszStart,255,(cwTrimLeft(pszStart)),255);
RtlStringCbCatExA(pszUniqueID,255,(cwTrimRight(pszStart)));
}
}
}

if(Descriptor->ProductRevisionOffset!=0)//Is Valid ProductRevisionOffset, append it to the ID
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(unsigned long)Descriptor+Descriptor->ProductRevisionOffset),255);
if(pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart,255,&szLength);
if(szLength > 0)
{
RtlStringCbCatExA(pszUniqueID,255,szSptr);
RtlStringCopyWorkerA(pszStart,255,(cwTrimLeft(pszStart)),255);
RtlStringCbCatExA(pszUniqueID,255,(cwTrimRight(pszStart)));
}
}
}

if(Descriptor->SerialNumberOffset!=0)//Is Valid SerialNumberOffset, append it to the ID
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(unsigned long)Descriptor+Descriptor->SerialNumberOffset),255);
if(pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart,255,&szLength);
if(szLength > 0)
{
RtlStringCbCatExA(pszUniqueID,255,"(");
RtlStringCopyWorkerA(pszStart,255,(cwTrimLeft(pszStart)),255);
RtlStringCbCatExA(pszUniqueID,255,(cwTrimRight(pszStart)));
RtlStringCbCatExA(pszUniqueID,255,")");
}
}
}
//pszUniqueID is a combination VendorIDOffset ProductIDOffset ProductRevisionOffset and SerialNumberOffset
}
}

EXIT_ROUTINE:

return Status;


ERROR_HANDLER:
Status=STATUS_SEVERITY_ERROR;
goto EXIT_ROUTINE;
}



Thanks.

Continue reading...
 
Back
Top