kisshujinwen
驱动牛犊
驱动牛犊
  • 注册日期2012-04-27
  • 最后登录2012-06-01
  • 粉丝0
  • 关注0
  • 积分4分
  • 威望31点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1624回复:2

请教下sfilter查询绑定卷是否为USB总线类型

楼主#
更多 发布于:2012-04-28 14:30
用了很多驱网上的代码,查询函数:

ULONG GetStorageDeviceBusType(IN PDEVICE_OBJECT DeviceObject)
{
    PIRP NewIrp;
    PSTORAGE_DEVICE_DESCRIPTOR Descriptor;
    STORAGE_PROPERTY_QUERY Query;
    UCHAR Buffer[sizeof(STORAGE_DEVICE_DESCRIPTOR)];
    KEVENT WaitEvent;
    NTSTATUS Status;
    IO_STATUS_BLOCK IoStatus;

    // first set the query properties
    Query.PropertyId = StorageDeviceProperty;
    Query.QueryType = PropertyStandardQuery;

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

    // we should build the query irp ourselves
    NewIrp = IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY, DeviceObject,
        (PVOID)&Query, sizeof(Query), (PVOID)Buffer, sizeof(Buffer), FALSE, &WaitEvent, &IoStatus);

    if (NULL == NewIrp)   // can't create new irp
    {
        DbgPrint("[%s] [%u] I can't create a new irp to query the property of device (%p)!\n",
            __FILE__, __LINE__, DeviceObject);
        return BusTypeUnknown;
    }

    // send this irp to the storage device
    Status = IoCallDriver(DeviceObject, NewIrp);

    if (Status == STATUS_PENDING)
    {
        Status = KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, NULL);
        Status = IoStatus.Status;
    }

    if (!NT_SUCCESS(Status))
    {
        DbgPrint("[%s] [%u] Query IOCTL_STORAGE_QUERY_PROPERTY of device (%p) failed, Status=0x%08X!\n",
            __FILE__, __LINE__, DeviceObject, Status);
        return BusTypeUnknown;
    }

    Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)Buffer;
    return Descriptor->BusType;
}
我是在SfFsControlMountVolumeComplete里:

if (!SfIsAttachedToDevice( vpb->DeviceObject, &attachedDeviceObject )) {
            //KdPrint(("卷还没有绑定"));
            //
            //  Attach to the new mounted volume.  The file system device
            //  object that was just mounted is pointed to by the VPB.
            //
            status = SfAttachToMountedDevice( vpb->DeviceObject,
                                              NewDeviceObject );

            if (!NT_SUCCESS( status )) {
                //KdPrint(("卷绑定失败"));
                //
                //  The attachment failed, cleanup.  Since we are in the
                //  post-mount phase, we can not fail this operation.
                //  We simply won't be attached.  The only reason this should
                //  ever happen at this point is if somebody already started
                //  dismounting the volume therefore not attaching should
                //  not be a problem.
                //

                SfCleanupMountedDevice( NewDeviceObject );
                IoDeleteDevice( NewDeviceObject );
            }
            KdPrint(("查询卷设备类型..."));
            deviceType = GetStorageDeviceBusType(vpb->RealDevice);
            KdPrint(("devicetype:%d",deviceType));
            ASSERT( NULL == attachedDeviceObject );
这里调用的,请问我这样正确吗?为什么我不管放在哪儿都是错的,请求帮助下吧,两天没解决了,谢谢
znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
沙发#
发布于:2012-04-28 14:59
你需要有下面的设备对象,对这个设备发送控制命令查询。
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
mrchenlei
驱动牛犊
驱动牛犊
  • 注册日期2007-05-28
  • 最后登录2012-06-12
  • 粉丝0
  • 关注0
  • 积分21分
  • 威望114点
  • 贡献值0点
  • 好评度13点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2012-05-04 11:38
在SfFsControlMountVolumeComplete中
if(status==STATUS_SUCCESS)
{
                     if(newDevExt->StorageStackDeviceObject==NULL)
    {
        goto _Exit;
    }

    if(DeviceObject->DeviceType!=FILE_DEVICE_DISK_FILE_SYSTEM)
    {
        goto _Exit;
    }

    if(IsUSBDsk(newDevExt->StorageStackDeviceObject)==1)
    {
        goto _Exit;
    }
}

static int IsUSBDsk(PDEVICE_OBJECT pstDskDvc)
{
    int iRet=0;
    NTSTATUS nFunRet;
    IO_STATUS_BLOCK stIOStatus;
    KEVENT stEvent;
    PIRP pstIOCtlIRP;
    PSTORAGE_DEVICE_DESCRIPTOR pstDvcDsc=NULL;
    STORAGE_PROPERTY_QUERY stDvcQuery;

    pstDvcDsc=(PSTORAGE_DEVICE_DESCRIPTOR)ExAllocatePool( NonPagedPool,sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512-1 );
    pstDvcDsc->Size=sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;

    stDvcQuery.PropertyId=StorageDeviceProperty;
    stDvcQuery.QueryType=PropertyStandardQuery;

    KeInitializeEvent(&stEvent,NotificationEvent,FALSE);

    if( (pstIOCtlIRP=IoBuildDeviceIoControlRequest(
                    IOCTL_STORAGE_QUERY_PROPERTY,
                    pstDskDvc,
                    &stDvcQuery,sizeof(STORAGE_PROPERTY_QUERY),
                    pstDvcDsc,pstDvcDsc->Size,
                    FALSE,
                    &stEvent,
                    &stIOStatus))==NULL )
    {
        SYCLCMN_LOG( (pszInf,"IsUSBDsk:IoBuildDeviceIoControlRequest ret NULL!!!") );
        nFunRet=STATUS_INSUFFICIENT_RESOURCES;
        goto _Exit;
    }

    nFunRet=IoCallDriver(pstDskDvc,pstIOCtlIRP);
    if(nFunRet==STATUS_PENDING)
    {
        KeWaitForSingleObject(&stEvent,Executive,KernelMode,FALSE,NULL);
        nFunRet=stIOStatus.Status;
    }

    if( !NT_SUCCESS(nFunRet) )
    {
        SYCLCMN_LOG( (pszInf,"IsUSBDsk:IoCallDriver ret %X!!!",nFunRet) );
        goto _Exit;
    }

    if(pstDvcDsc->BusType==BusTypeUsb)
    {
        iRet=1;
    }

_Exit:
    if(pstDvcDsc!=NULL)
    {
        ExFreePool(pstDvcDsc);
    }

    return iRet;
}
游客

返回顶部