// JP opened flex table

Click to See Complete Forum and Search --> : Problems while attaching a volume.


Liuzq
September 2nd, 2008, 05:20 AM
I write a function to attach a volume.
See the codes:

BOOLEAN FEAttachVolumeByName( PDRIVER_OBJECT pDriverObject , PUNICODE_STRING pStrVolumeDeviceName)
{
NTSTATUS ntStatus ;
OBJECT_ATTRIBUTES objectAttributes ;
IO_STATUS_BLOCK ioStatus ;
HANDLE ntFileHandle ;
FILE_OBJECT fileObject;
PDEVICE_OBJECT pFileSysDevice;
PDEVICE_OBJECT pVolumeDeviceObject;
PFileEncriptDevExt pExt = NULL;
InitializeObjectAttributes( &objectAttributes, pStrVolumeDeviceName,
OBJ_CASE_INSENSITIVE, NULL, NULL );
ntStatus = ZwCreateFile( &ntFileHandle, SYNCHRONIZE|FILE_ANY_ACCESS,
&objectAttributes, &ioStatus, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE,
NULL, 0 );

if( !NT_SUCCESS( ntStatus ) )
{
return FALSE;
}
ntStatus = ObReferenceObjectByHandle( ntFileHandle, FILE_READ_DATA,
NULL, KernelMode, (PVOID*)&fileObject, NULL );
if( !NT_SUCCESS( ntStatus )) {

ZwClose( ntFileHandle );
return FALSE;
}

pFileSysDevice = IoGetRelatedDeviceObject( &fileObject );

if( ! pFileSysDevice ) {

ObDereferenceObject( &fileObject );
ZwClose( ntFileHandle );
return FALSE;
}

ntStatus = IoCreateDevice( pDriverObject,
sizeof(FileEncriptDevExt),
NULL,
pFileSysDevice->DeviceType,
0,
FALSE,
&pVolumeDeviceObject );
if( !NT_SUCCESS(ntStatus) ) {

ObDereferenceObject( (PVOID*)&fileObject );
ZwClose( ntFileHandle );

return FALSE;
}
pVolumeDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

////////////////////test code
g_pDeviceVolumeC = pVolumeDeviceObject ;
/////////////end
pExt = (PFileEncriptDevExt)pVolumeDeviceObject->DeviceExtension ;
ASSERT( NULL != pExt) ;
pExt->pAttachTo = IoAttachDeviceToDeviceStack( pVolumeDeviceObject, pFileSysDevice );
if( NULL == pExt->pAttachTo )
{
//DbgPrint(("Filemon: Connect with Filesystem failed: %c (%x) =>%x\n",
// 'C', fileSysDevice, (LONG)ntStatus ));

// Derefence the object and get out
ObDereferenceObject( &fileObject );
ZwClose( ntFileHandle );

return FALSE;
}
ObDereferenceObject( &fileObject );
ZwClose( ntFileHandle );
return TRUE;

}

It is called in the DriverEntry as:

RtlInitUnicodeString( &uStrAttachTo , L"\\Device\\HarddiskVolume1");
if (!FEAttachVolumeByName( g_pDriverObject , &uStrAttachTo))
{
_KDPRINT(("Couldn't IoAttachDevice to c: \n"));
}

I debug it in WinDbg and find it cash at this line in the function:
pFileSysDevice = IoGetRelatedDeviceObject( &fileObject );
I think the argument fileObject is OK.
Why?

//JP added flex table