lovehunterboy
驱动小牛
驱动小牛
  • 注册日期2008-05-29
  • 最后登录2010-04-16
  • 粉丝2
  • 关注0
  • 积分67分
  • 威望463点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1096回复:1

哈哈,大家别烦啊,我又来问了!

楼主#
更多 发布于:2008-09-08 11:33
先提问题:SFILTER在枚举VDO时定义了一个PDEVICE_OBJECT newDeviceObject,在循环中调用IoCreateDevice创造它,这样的话,每次循环都会创建一次这个对像,而不是再创造一个对像,我是不是可以理解为实际是每次循环都增加一次这个对像的引用计数?我想如果是创建多个设备对像应该用PDEVICE_OBJECT 数组,不知道对不对?
请看代码(XP SFILTER):
NTSTATUS
SfEnumerateFileSystemVolumes (
    IN PDEVICE_OBJECT FSDeviceObject,
    IN PUNICODE_STRING Name
    )
/*++
Routine Description:
    Enumerate all the mounted devices that currently exist for the given file
    system and attach to them.  We do this because this filter could be loaded
    at any time and there might already be mounted volumes for this file system.
Arguments:
    FSDeviceObject - The device object for the file system we want to enumerate
    Name - An already initialized unicode string used to retrieve names
           This is passed in to reduce the number of strings buffers on
           the stack.
Return Value:
    The status of the operation
--*/
{
    PDEVICE_OBJECT newDeviceObject;
    PSFILTER_DEVICE_EXTENSION newDevExt;
    PDEVICE_OBJECT *devList;
    PDEVICE_OBJECT diskDeviceObject;
    NTSTATUS status;
    ULONG numDevices;
    ULONG i;
    PAGED_CODE();
    //
    //  Find out how big of an array we need to allocate for the
    //  mounted device list.
    //
    status = IoEnumerateDeviceObjectList(
                    FSDeviceObject->DriverObject,
                    NULL,
                    0,
                    &numDevices);
    //
    //  We only need to get this list of there are devices.  If we
    //  don't get an error there are no devices so go on.
    //
    if (!NT_SUCCESS( status )) {
        ASSERT(STATUS_BUFFER_TOO_SMALL == status);
        //
        //  Allocate memory for the list of known devices
        //
        numDevices += 8;        //grab a few extra slots
        devList = ExAllocatePoolWithTag( NonPagedPool,
                                         (numDevices * sizeof(PDEVICE_OBJECT)),
                                         SFLT_POOL_TAG );
        if (NULL == devList) {
            return STATUS_INSUFFICIENT_RESOURCES;
        }
        //
        //  Now get the list of devices.  If we get an error again
        //  something is wrong, so just fail.
        //
        status = IoEnumerateDeviceObjectList(
                        FSDeviceObject->DriverObject,
                        devList,
                        (numDevices * sizeof(PDEVICE_OBJECT)),
                        &numDevices);
        if (!NT_SUCCESS( status ))  {
            ExFreePool( devList );
            return status;
        }
        //
        //  Walk the given list of devices and attach to them if we should.
        //
        for (i=0; i < numDevices; i++) {
            //
            //  Do not attach if:
            //      - This is the control device object (the one passed in)
            //      - We are already attached to it
            //
            if ((devList != FSDeviceObject) &&
                !SfIsAttachedToDevice( devList, NULL )) {
                //
                //  See if this device has a name.  If so, then it must
                //  be a control device so don't attach to it.  This handles
                //  drivers with more then one control device.
                //
                SfGetBaseDeviceObjectName( devList, Name );
                if (Name->Length <= 0) {
                    //
                    //  Get the real (disk) device object associated with this
                    //  file  system device object.  Only try to attach if we
                    //  have a disk device object.
                    //
                    status = IoGetDiskDeviceObject( devList, &diskDeviceObject );
                    if (NT_SUCCESS( status )) {
                        //
                        //  Allocate a new device object to attach with
                        //
                        status = IoCreateDevice( gSFilterDriverObject,
                                                 sizeof( SFILTER_DEVICE_EXTENSION ),
                                                 NULL,
                                                 devList->DeviceType,
                                                 0,
                                                 FALSE,
                                                 &newDeviceObject );//就是这里了,不知道我的理解对不对
                        if (NT_SUCCESS( status )) {
                            //
                            //  Set disk device object
                            //
                            newDevExt = newDeviceObject->DeviceExtension;
                            newDevExt->DiskDeviceObject = diskDeviceObject;
                    
                            //
                            //  Set Device Name
                            //
                            RtlInitEmptyUnicodeString( &newDevExt->DeviceName,
                                                       newDevExt->DeviceNameBuffer,
                                                       sizeof(newDevExt->DeviceNameBuffer) );
                            SfGetObjectName( diskDeviceObject,
                                             &newDevExt->DeviceName );
                            //
                            //  Attach to volume.
                            //
                            status = SfAttachToMountedDevice( devList,
                                                              newDeviceObject );
                            if (!NT_SUCCESS( status )) {
                                //
                                //  The attachment failed, cleanup.  Note that
                                //  we continue processing so we will cleanup
                                //  the reference counts and try to attach to
                                //  the rest of the volumes.
                                //
                                SfCleanupMountedDevice( newDeviceObject );
                                IoDeleteDevice( newDeviceObject );
                            }
                        }
                        //
                        //  Remove reference added by IoGetDiskDeviceObject.
                        //  We only need to hold this reference until we are
                        //  successfully attached to the current volume.  Once
                        //  we are successfully attached to devList, the
                        //  IO Manager will make sure that the underlying
                        //  diskDeviceObject will not go away until the file
                        //  system stack is torn down.
                        //
                        ObDereferenceObject( diskDeviceObject );
                    }
                }
            }
            //
            //  Dereference the object (reference added by
            //  IoEnumerateDeviceObjectList)
            //
            ObDereferenceObject( devList );
        }
        //
        //  We are going to ignore any errors received while mounting.  We
        //  simply won't be attached to those volumes if we get an error
        //
        status = STATUS_SUCCESS;
        //
        //  Free the memory we allocated for the list
        //
        ExFreePool( devList );
    }
    return status;
}
xx_qiang
驱动小牛
驱动小牛
  • 注册日期2004-07-30
  • 最后登录2017-02-27
  • 粉丝2
  • 关注1
  • 积分31分
  • 威望249点
  • 贡献值0点
  • 好评度171点
  • 原创分0分
  • 专家分0分
  • 社区居民
沙发#
发布于:2008-09-08 14:08
很明显,你理解的不对。
DeviceObject
    Pointer to a variable that receives a pointer to the newly created DEVICE_OBJECT structure. The DEVICE_OBJECT structure is allocated from nonpaged pool.
这个是IoCreateDeviceObject中关于最后一个参数的解说,newDeviceObject 只作为一个变量来接收IoCreateDeviceObject函数中分配的一个DEVICE_OBJECT结构的指针
游客

返回顶部