Hi All,
I have created an application(using legacy file system driver) to control the exteranal storage device .
We mainly use this for blocking and allowing external storage devices.The application uniquelly identifies devices using its product id,vendor id,revision id and serial number.
This works fine, but with few devices we are getting the serial numer as aspecial character.
For example we have a device with pnpid “USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_2.0&REV_1.00\60A44C413BF2F0718628B8E0&0”
whie trying to get the serial number it returns a special character “6”, but its acutal serial no is “60A44C413BF2F0718628B8E0”.
The result was the same when we tried with WMI query.
here is the sample code code
NTSTATUS GetDeviceTypeAndUniqueID(IN PDEVICE_OBJECT StorageStackDeviceObject, char* pszUniqueIDArg)
{
char pszUniqueID[255];
char szBuff[1024];
STORAGE_PROPERTY_QUERY Query;
//STORAGE_DEVICE_DESCRIPTOR Buffer[4];
NTSTATUS Status = STATUS_SUCCESS;
PSTORAGE_DEVICE_DESCRIPTOR Descriptor = NULL;
PIRP NewIrp2 = NULL;
PIO_STACK_LOCATION NextIrpStack = NULL;
IO_STATUS_BLOCK IoStatus;
char szSptr[2] = { '_', '\0' };
SIZE_T szLength = 0;
Query.PropertyId = StorageDeviceProperty;// first set the query properties
Query.QueryType = PropertyStandardQuery;
memset(pszUniqueID, 0, 255);
if (KeGetCurrentIrql() > PASSIVE_LEVEL)
{
return STATUS_SUCCESS;
}
if (StorageStackDeviceObject == NULL)
{
return STATUS_SUCCESS;
}
KeInitializeEvent(&WaitEvent_newIrp, NotificationEvent, TRUE);// initialize the waitable event
__try
{
memset(szBuff, 0, 1024);
NewIrp2 = IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY, StorageStackDeviceObject,
(PVOID) & Query, sizeof(STORAGE_PROPERTY_QUERY),
szBuff, 1024,
FALSE, &WaitEvent_newIrp, &IoStatus);
if (NewIrp2 == NULL)
{
return STATUS_SUCCESS;
}
Status = IoCallDriver(StorageStackDeviceObject, NewIrp2);// send this irp to the storage device
if (Status == STATUS_PENDING)
{
KeWaitForSingleObject(&WaitEvent_newIrp, Executive, KernelMode, FALSE, NULL);
Status = IoStatus.Status;
}
}
__finally
{
//if(NT_SUCCESS(Status))
if (szBuff != NULL)
{
try
{
Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)szBuff;
Descriptor->Size = sizeof(szBuff);
uBusType = Descriptor->BusType; //Get the bus type.
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
}
if (NT_SUCCESS(Status))
{
if (Descriptor != NULL)
{
char pszStart[255];
try
{
if (Descriptor->VendorIdOffset != 0)
{
memset(pszStart, 0, 255);
RtlStringCopyWorkerA(pszStart, 255, ((char*)(UINT_PTR)Descriptor + (DWORD32)Descriptor->VendorIdOffset), 255);
if (pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart, 255, &szLength);
if (szLength > 0)
{
RtlStringCopyWorkerA(pszStart, 255, (TrimLeft(pszStart)), 255);
RtlStringCopyWorkerA(pszUniqueID, 255, (ReplaceBlankto_(TrimRight(pszStart))), 255);
}
}
}
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
try
{
if (Descriptor->ProductIdOffset != 0)//Is Valid ProductIDOffset, append it to the ID
{
memset(pszStart, 0, 255);
RtlStringCopyWorkerA(pszStart, 255, ((char*)(UINT_PTR)Descriptor + (DWORD32)Descriptor->ProductIdOffset), 255);
if (pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart, 255, &szLength);
if (szLength > 0)
{
RtlStringCbCatExA(pszUniqueID, 255, szSptr);
RtlStringCopyWorkerA(pszStart, 255, (TrimLeft(pszStart)), 255);
RtlStringCbCatExA(pszUniqueID, 255, (TrimRight(pszStart)));
}
}
}
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
try
{
if (Descriptor->ProductRevisionOffset != 0)//Is Valid ProductRevisionOffset, append it to the ID
{
memset(pszStart, 0, 255);
RtlStringCopyWorkerA(pszStart, 255, ((char*)(UINT_PTR)Descriptor + (DWORD32)Descriptor->ProductRevisionOffset), 255);
if (pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart, 255, &szLength);
if (szLength > 0)
{
RtlStringCbCatExA(pszUniqueID, 255, szSptr);
RtlStringCopyWorkerA(pszStart, 255, (TrimLeft(pszStart)), 255);
RtlStringCbCatExA(pszUniqueID, 255, (TrimRight(pszStart)));
}
}
}
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
try
{
if (Descriptor->SerialNumberOffset != 0)//Is Valid ProductRevisionOffset, append it to the ID
{
memset(pszStart, 0, 255);
RtlStringCopyWorkerA(pszStart, 255, ((char*)(UINT_PTR)Descriptor + (DWORD32)Descriptor->SerialNumberOffset), 255);
if (pszStart != ANSI_NULL)
{
RtlStringLengthWorkerA(pszStart, 255, &szLength);
if (szLength > 0)
{
RtlStringCbCatExA(pszUniqueID, 255, "(");
RtlStringCopyWorkerA(pszStart, 255, (TrimLeft(pszStart)), 255);
RtlStringCbCatExA(pszUniqueID, 255, (TrimRight(pszStart)));
RtlStringCbCatExA(pszUniqueID, 255, ")");
}
}
}
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
try
{
c_upper_string(pszUniqueID);
RtlStringCopyWorkerA(pszUniqueIDArg, 255, pszUniqueID, 255);
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
}
NewIrp2 = NULL;
}
}
return Status;
}
Thanks in advance. Can you suggest any solution for this.